Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Ask a Silly Question...

by headybrew (Beadle)
on Feb 01, 2008 at 02:11 UTC ( #665501=perlquestion: print w/replies, xml ) Need Help??
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 ./ 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...


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 (Chancellor) 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 (Pope) 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; ... }


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


    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

    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.



Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://665501]
Approved by kyle
Front-paged by GrandFather
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2018-06-23 19:58 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (125 votes). Check out past polls.