Chudong Chudong - 22 days ago 8
Perl Question

Which identifiers are considered to be dynamic?

The following excerpt is taken from

:


The
package
statement declares the compilation unit as being in the given namespace. The scope of the
package
declaration is from the declaration itself through the end of the enclosing block,
eval
, or file, whichever comes first (the same scope as the
my
() and
local
() operators). Unqualified dynamic identifiers will be in this namespace, except for those few identifiers that if unqualified, default to the main package instead of the current one as described below.


The term "dynamic" in the "Unqualified dynamic identifiers" phrase above seems to refer to variables that are not prefixed with
my
in a package. That is, in the code snippet below,
$v1
is considered a dynamic identifier. Is that right?

package Package_1;

$v1 = "v1_val";

my $v2 = "v2_val";

Answer

The two general types of variable scope are dynamic and lexical. Basically, the visibility of lexical variables is based on their location in the source code and the visibility of dynamic variables is determined at run-time.

In Perl, variables declared with my are lexical and any other variables are dynamic. The main place that this distinction becomes directly relevant is that local can only be used with dynamic (non-my) variables and not with lexical (my) variables.

See also the Perl FAQ, What's the difference between dynamic and lexical (static) scoping?