Perl has two completely separate, but largely compatible variable systems, package variables, as in the first example, and lexical variables, as in the second. There are a few things that everyone can do, besides the other:
Package variables are the only ones that can be:
- localized (with
local ) - used as the target for a symbolic link (the reason the second OP example is not working)
- used as simple words (auxiliary definitions, file descriptors)
- used with typeglobs (because this is what the character really is under the hood)
Lexical variables are the only ones that can be closed (used in lexical closure).
Using a string value will help you declare package variables with our , making the difference more clear.
There are several times when symbolic links are useful in Perl, most of which are centered around manipulating a symbol table (for example, writing your own import in a module, rather than using Exporter , monkey-fixing modules at run time, -programming tasks). These are all advanced topics.
For other tasks, there is usually a better way to do this, for example with a hash. The general rule is to always work under use warnings; use strict; use warnings; use strict; if you do not know that there is no other way but to disable a part of the pragma (for example, using no strict 'refs'; as small a scope as possible).
Eric Strom
source share