Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Variable scoping outside subs

by Roger (Parson)
on Oct 27, 2003 at 23:44 UTC ( [id://302573]=note: print w/replies, xml ) Need Help??


in reply to Variable scoping outside subs

Line 6 is never evaluated.

When you uncomment line 3, you get the following compilation (not run-time) error:
Global symbol "$var" requires explicit package name at p03.pl line 3. Execution of p03.pl aborted due to compilation errors.
Because you have use strict; turned on, the Perl interpreter checks that a variable is created with my keyword before it is used, during the compilation stage.

If you move the line my $var = 1; to the end of the code (after sub test), you will get a compilation error.

Back to your original code with line 3 commented out, when the Perl compiler gets to the line my $var = 1;, it create an entry in the package's global symbolic/variable table for $var, with the value being undef. And when the compiler gets to sub test later, it checks to see if $var exists in the symbol table, and found it. So the compilation succeeds.

my $var = 1; has two components:
my $var; # compile time component inserts variable into scratchpad # of the scope $var = 1; # run-time component assigns the value
The value of the $var is set at run-time. Because line 6 is never executed, the initial value of $var is undef when you call the subroutine test.

use strict; test(); #print "$var\n"; # Global symbol "$var" requires explicit... exit; my $var = 1; sub test { print "var was undef\n" if $var eq undef; print "var was '$var'\n"; $var++; print "var is now '$var'\n"; }
And the output is -
var was undef var was '' var is now '1'
Updated: Thanks to welchavw for pointing out that the lexical variables are not stored in the package symbol tables. Instead, the lexical variables are stored in the scratchpad of the scope, file scope in this case.

Replies are listed 'Best First'.
Re: Re: Variable scoping outside subs
by welchavw (Pilgrim) on Oct 28, 2003 at 01:39 UTC

    Roger,

    I really like most of your explanation, which is much clearer than my attempt, but I am not sure that I agree completely. Lexicals are not inserted into any symbol table as far as I know, so I think that portion of the explanation may be erroneous.

    ,welchavw

      Hi welchavw, thanks for pointing out that my variables are not inserted into symbol table, instead they are inserted into scratchpads. There are a few excellent references on this topic:

      1. Advanced Perl Programming (Oreilly)

      The my variables are inserted into the 'scratchpad' of the scope, not the package symbol table.

      2. Perl lexical my variables.

      The my operator declares the listed variables to be lexically confined to the enclosing block, ...

      ...

      This doesn't mean that a my variable declared in a statically enclosing lexical scope would be invisible. Only dynamic scopes are cut off. For example, the bumpx() function below has access to the lexical $x variable because both the my and the sub occurred at the same scope, presumably file scope.

      my $x = 10; sub bumpx { $x++ }
Re: Re: Variable scoping outside subs
by xiper (Friar) on Oct 28, 2003 at 00:09 UTC
    Excellent reply, thanks! ++
    Minor nitpick... $var eq undef is the same as $var eq '':
    perl -e'$var="";print "ooh" if $var eq undef'

    - ><iper (who recently decided to log in...)

    use japh; print;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://302573]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-03-29 06:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found