Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

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 ./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

Comment on Ask a Silly Question...
Select or Download Code
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 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 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; ... }

    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 (Canon) 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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://665501]
Approved by kyle
Front-paged by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (5)
As of 2014-11-25 23:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (160 votes), past polls