As already mentioned, Perl will evaluate strings only as you wrote them, using eval to call the compiler at runtime. You can use links as mentioned in some other answers, but this changes the look of the code ( $$a vs $a ). However, this is Perl, there is a way to hide extended functionality behind a simple variable using tie .
{package Lazy; sub TIESCALAR {bless \$_[1]}
You can find documentation for tie , but in a nutshell it allows you to define your own implementation of a variable (for scalars, arrays, hashes, or files). Thus, this code creates a new variable $a with an implementation that gets or sets the current value of $b (by storing the reference to $b internally). The new method is not strictly necessary (the constructor is actually TIESCALAR ), but is provided as syntactic sugar to avoid using tie directly in the calling code.
(which would be tie my $a, 'Lazy', $b; )
Eric Strom
source share