http://www.perlmonks.org?node_id=665501

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

Hello my monkitudinous friends,

Why is it that when I do:
my $FOO; print join("\n",sort(keys(%main::)) );
I don't see "FOO" in the output? Shouldn't 'my' put it in the main:: symbol table?


next, why does:
my $main::FOO; print join("\n",sort(keys(%main::)) );
do this:
"my" variable $main::FOO can't be in a package at ./footest.pl line...

But if I use:
local $FOO; print join("\n",sort(keys(%main::)) );
it works?

And, no, the lines are not in any sub blocks or anything. They're the only lines in the test script.


Just doin' my best to waste my time and yours...

-heady

Replies are listed 'Best First'.
Re: Ask a Silly Question...
by Util (Priest) on Feb 01, 2008 at 03:05 UTC

    Perl has a strict separation between "lexical" variables and "package" variables.

    Package vars live in the symbol table, can be declared with "our()" or "use vars qw()" (or just spring into existence when first used if "use strict vars" is not in effect), and can be remotely manipulated with things like Exporter or direct package references like "$Data::Dumper::Sortkeys=1". They always *exist* globally (although our() can control where they can be *referenced*). They are generally used only when you *need* some other package to manipulate your data remotely.

    Lexical vars are *not* accessible from the symbol table; they live in the "lexical PAD" that exists in every lexical block. They are never truly global; at best (worst?) they can be scoped to their entire declaring file. These are the vars that are always to be used, unless you have a specific need for a package var.

    So, in your three examples:

    1. You declared $FOO as lexical using my(), so it will not exist in the symbol table.
    2. You tried to declare a package var by using my(), which is illegal because it is a contradiction of the lexical/package meanings.
    3. You created package var $main::FOO by simply referring to $FOO while inside package main::.
    And ++Fletch - Coping with Scoping is a great article for this subject.

Re: Ask a Silly Question...
by Fletch (Bishop) on Feb 01, 2008 at 02:35 UTC

    Lexical variables (declared with my) don't live in the symbol table. You need to check out Coping with Scoping.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: Ask a Silly Question...
by ikegami (Patriarch) on Feb 01, 2008 at 04:05 UTC

    Unlike package variables, Perl can have multiple lexical (my) variables with the same name in existence at the same time (Upd: so such a lookup mechanism wouldn't work ).

    Nested scopes.

    my $x; { my $x; ... }

    Closures.

    { my $x; sub f { ++$x } } { my $x; sub g { ++$x } }

    Recursion.

    sub f { my $x; ... f(); ... }

    Each exist as a different pad entry on the pad of the function that encapsulates it.

Re: Ask a Silly Question...
by roboticus (Chancellor) on Feb 01, 2008 at 14:26 UTC
    ++headybrew:

    Wow! I learned a lot from your question. I finally had to start learning more about perl's scoping rules and such. And in the meantime, I learned another couple of tricks. I'll have to cogitate on them for a while and see how I can put 'em to use.

    Thanks!

    ...roboticus