Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 865637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T09:40:06+00:00 2026-05-15T09:40:06+00:00

Consider the following Perl code. #!/usr/bin/perl use strict; use warnings; $b=1; my $a=${b}; $b=2;

  • 0

Consider the following Perl code.

#!/usr/bin/perl

use strict;
use warnings;

$b="1";

my $a="${b}";

$b="2";

print $a;

The script obviously outputs 1. I would like it to be whatever the current value of $b is.

What would be the smartest way in Perl to achieve lazy evaluation like this? I would like the ${b} to remain “unreplaced” until $a is needed.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T09:40:07+00:00Added an answer on May 15, 2026 at 9:40 am

    I’m more interested in knowing why you want to do this. You could use a variety of approaches depending on what you really need to do.

    You could wrap up the code in a coderef, and only evaluate it when you need it:

    use strict; use warnings;
    
    my $b = '1';
    my $a = sub { $b };
    $b = '2';
    print $a->();
    

    A variant of this would be to use a named function as a closure (this is probably the best approach, in the larger context of your calling code):

    my $b = '1';
    sub print_b
    {
        print $b;
    }
    
    $b = '2';
    print_b();
    

    You could use a reference to the original variable, and dereference it as needed:

    my $b = '1';
    my $a = \$b;
    $b = '2';
    print $$a;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following code and its output: Code #!/usr/bin/perl -w use strict; use Data::Dumper;
consider the following code: perl -wne 'chomp;print if m/[^(?:test)]/' I was surprised to see
Consider the following perl script ( read.pl ): my $line = <STDIN>; print Perl
Consider the following perl code: $schema->txn_begin(); my $r = $schema->resultset('test1')->find({id=>20}); my $n = $r->num;
Consider the following silly Perl program: $firstarg = $ARGV[0]; print $firstarg; $input = <>;
How can I use C++ enum types like C#? consider following definition in c++
Consider the following ruby code test.rb: begin puts thisFunctionDoesNotExist x = 1+1 rescue Exception
Consider the following code: $(a).attr(disabled, disabled); In IE and FF, this will make anchors
Consider the following code: void Handler(object o, EventArgs e) { // I swear o
I'd like to inspect and manipulate code of arbitrary Perl procedures (got by coderefs)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.