Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Scope surprise

by Nkuvu (Priest)
on Apr 11, 2009 at 00:17 UTC ( #756951=note: print w/ replies, xml ) Need Help??


in reply to Scope surprise

If you had use warnings in the script you'd see something like "my" variable $foo masks earlier declaration in same scope at scriptname.pl line 6.

I'm pretty sure that Perl is parsing the script prior to actually executing it, seeing the second declaration of $foo which masks the first declaration, and using that second variable in the same scope to actually execute the script. So you're calling the subroutine before defining the second $foo, meaning you print an uninitialized value.

You would see the same output if you had a script like

use strict; foo(); my $foo = 2; sub foo { print "foo: $foo\n"; }


Comment on Re: Scope surprise
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (8)
As of 2015-07-08 04:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (94 votes), past polls