Perl, evaluate string lazily

Posted by Mike on Stack Overflow See other posts from Stack Overflow or by Mike
Published on 2010-06-03T17:58:12Z Indexed on 2010/06/03 18:04 UTC
Read the original article Hit count: 433

Filed under:
|
|

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.

© Stack Overflow or respective owner

Related posts about perl

Related posts about string