jmeek has asked for the wisdom of the Perl Monks concerning the following question:

In Damian Conway's "Abbot and Costello Who's on First'" Parse::RecDescent example code in "Computer Science & Perl Programming" he starts the code with the line

use vars qw( %base %man @try_again);

and then in the action blocks in the grammars uses those variables with the syntax, e.g., $::man{$item[4]} and @::try_again.

As I thought the point of the 'use vars' pragma was to allow the variables to be used globally without writing the fully qualified name, I'm puzzled as to why the double colons are used.

Replies are listed 'Best First'.
Re: What does '::' mean in constructs '$::foo' and '@::foo'?
by Athanasius (Archbishop) on Oct 06, 2015 at 08:04 UTC

    Hello jmeek, and welcome to the Monastery!

    In Perl, the only truly global variables are those with predefined “punctuation” names: $/, $|, etc. (ok, also $a and $b). Other “global” variables (i.e., those with user-supplied names) are actually package-globals, meaning they are global within the package in which they are defined. In modern Perl, these variables are typically accessed in other packages via suitable our declarations, but the older mechanism of use vars is still available. Compare the following one-liners:

    18:05 >perl -Mstrict -wE "package Foo; sub foo { say $x; } package mai +n; $x = 42; Foo::foo;" Global symbol "$x" requires explicit package name (did you forget to d +eclare "my $x"?) at -e line 1. Global symbol "$x" requires explicit package name (did you forget to d +eclare "my $x"?) at -e line 1. Bareword "Foo::foo" not allowed while "strict subs" in use at -e line +1. Execution of -e aborted due to compilation errors. 18:06 >perl -Mstrict -wE "use vars qw( $x ); package Foo; sub foo { sa +y $x; } package main; $x = 42; Foo::foo;" Global symbol "$x" requires explicit package name (did you forget to d +eclare "my $x"?) at -e line 1. Bareword "Foo::foo" not allowed while "strict subs" in use at -e line +1. Execution of -e aborted due to compilation errors. 18:06 >perl -Mstrict -wE "use vars qw( $x ); package Foo; sub foo { sa +y $::x; } package main; $x = 42; Foo::foo;" 42 18:06 >perl -Mstrict -wE "package Foo; sub foo { say $::x; } package m +ain; $x = 42; Foo::foo;" Variable "$x" is not imported at -e line 1. Global symbol "$x" requires explicit package name (did you forget to d +eclare "my $x"?) at -e line 1. Execution of -e aborted due to compilation errors. 18:06 >

    In all cases, $x is actually $main::x; a use vars declaration doesn’t change that. In the third case, supplying the package name removes the error and allows package Foo to access the variable although it’s from a different package. (The final case is given to illustrate the role use vars is playing in preventing errors from use strict.)

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      A very fine explanation of a difficult (to me, anyway) Perl concept.   Thanks for sharing.   ++
Re: What does '::' mean in constructs '$::foo' and '@::foo'?
by Discipulus (Canon) on Oct 06, 2015 at 07:01 UTC
    that syntax means the default main package.
    Infact normally you can specify the package where a variable lives as in $package::varname and when the first part is null is read as $main::varname

    update: probably there are other package definitions when you encounter such syntaxes: inside the scope of a package pack_name if you write $varname it reads $pack_name::varname while $::varname reads $main::varname

    L*
    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
      Nit: It actually refers the var in the root namespace, but main is aliased to the root namespace, so the following all refer to the same var:
      $::var $main::var $main::main::var $main::main::main::var etc
      So @::foo is just shorthand for @main::foo, and the need for this syntax has been mostly rendered obsolete since it became possible to use our @foo; instead of use vars ( @foo );. (Right?) Many thanks.

        No. Assuming the purpose is to "defeat" use strict;, it was made obsolete by use vars qw( @foo );.

        perl -e' use strict; @foo = 123; print "ok\n"; ' Global symbol "@foo" requires explicit package name at -e line 3. Execution of -e aborted due to compilation errors. $ perl -e' use strict; use vars qw( @foo ); @foo = 123; print "ok\n"; ' ok

        But since strict was introduced in Perl 5.000 and vars was introduced in Perl 5.002, it's safe to say that was not the book's reason for using @::foo.

        In fact, it's not obsolete. You still need to use fully qualified names to refer to variables in namespaces other than the current one.

        use Data::Dumper qw( Dumper ); local $Data::Dumper::Useqq = 1; local $Data::Dumper::Sortkeys = 1; print(Dumper($data));
Re: What does '::' mean in constructs '$::foo' and '@::foo'?
by ikegami (Patriarch) on Oct 06, 2015 at 15:20 UTC

    $Foo::Bar::baz refers to the package variable $baz in the namespace Foo::Bar.

    Similarly, $::baz refers to the package variable $baz in the root namespace.

    The default namespace, main, is an alias for the root namespace. So unless you have a package declaration changing the current namespace from the default, $::baz refers to package variable $baz in the current namespace.

    Unless he used a package statement, I'm not sure why he used %::man instead of %man. My only guess is that it was to avoid the possibility of referencing a lexical (my) variable named %man by accident.