Why does this print 42:
$answer = 42;
$variable = "answer";
print ${$variable} . "\n";
but this doesn't:
my $answer = 42;
my $variable = "answer";
print ${$variable} . "\n";
Only package variables (the kind declared in your first example) can be targeted via symbolic references. Lexical (my
) variables, cannot be, which is why your second example fails.
See the excellent article Coping with Scoping for how the two separate variable systems in Perl operate. And see the also excellent Why it's stupid to use a variable variable name for why that's stupid. :)
Perl has two entirely separate but largely compatible variable systems, package variables, as in your first example, and lexical variables, as in the second. There are a few things that each can do but the other cant:
Package variables are the only ones that can be:
local
)Lexical variables are the only ones that can be closed over (used in a lexical closure).
Using strict would help by forcing you to declare package variables with our
, making the difference clearer.
There are several times where symbolic references are useful in Perl, most center around manipulating the symbol table (like writing your own import
in a module rather than using Exporter
, monkey-patching modules at runtime, various other meta-programming tasks). All of these are advanced topics.
For other tasks, there is usually a better way to do it such as with a hash. The general rule of thumb is to always run under use warnings; use strict;
unless you know there isn't any other way but to disable a portion of the pragma (such as using no strict 'refs';
in as small a scope as possible).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With