3

The following excerpt is taken from perldoc perlmod:

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";
Chudong
  • 73
  • 5
  • It could also be a lexical variable if declared by `my $v1` above the `package` statement in the same file as the snippet. See the documentation for [package](http://perldoc.perl.org/functions/package.html) and [Scope of variables in Perl](http://perlmaven.com/scope-of-variables-in-perl) for more information – Håkon Hægland Nov 13 '16 at 22:14
  • @Chudong: I think the idea of *dynamic* is being misused here. It's unclear whether it is the *identifier* that is dynamic (all Perl identifiers are static) or the *value* that the identifier refers to (most Perl values are dynamic). What is it that you need to know? Does [*What is the difference between `my` and `our` in Perl?*](http://stackoverflow.com/questions/845060/what-is-the-difference-between-my-and-our-in-perl/845382?s=7) help you? – Borodin Nov 13 '16 at 22:34
  • @Borodin, as you indicated, I'd like to know what term "dynamic" means here. – Chudong Nov 14 '16 at 04:36

2 Answers2

3

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?

Dave Sherohman
  • 43,013
  • 12
  • 61
  • 98
  • Dave, in your answer above, in the sentence, "In Perl, variables declared with my are lexical and any other variables are lexical", did you actually mean "dynamic", and not "lexical", in the latter? – Chudong Nov 14 '16 at 13:42
  • @Chudong Yes, you understood it correctly. Thanks for catching that! I've updated the answer to fix it. – Dave Sherohman Nov 15 '16 at 08:35
1

The best way to see the difference is in code:

our $dynamic = 'outside';
my  $static  = 'outside';

sub show {
  print "\tin sub: dynamic $dynamic\n";
  print "\tin sub: static  $static\n";
  print"\n";
}

{
  local $dynamic = 'inside';
  my    $static = 'inside';

  print "In block\n";
  print "\tinline: dynamic $dynamic\n";
  print "\tinline: static  $static\n";
  show();
}

print "In main\n";
print "\tinline: dynamic $dynamic\n";
print "\tinline: static  $static\n";
show();

Output:

In block
    inline: dynamic inside
    inline: static  inside
    in sub: dynamic inside
    in sub: static  outside

In main
    inline: dynamic outside
    inline: static  outside
    in sub: dynamic outside
    in sub: static  outside

Notice that the sub always sees the same (outside) $static.

shawnhcorey
  • 3,442
  • 1
  • 13
  • 17