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,


In reply to Re: What does '::' mean in constructs '$::foo' and '@::foo'? by Athanasius
in thread What does '::' mean in constructs '$::foo' and '@::foo'? by jmeek

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.