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

Re: Re: where do you put your subs

by Juerd (Abbot)
on Mar 08, 2002 at 10:46 UTC ( [id://150277]=note: print w/replies, xml ) Need Help??


in reply to Re: where do you put your subs
in thread where do you put your subs

The lexical/unintented-global problem needs a little more visualization. In the chatterbox, some are really confused about this part.

# Not using strict, for demonstration. sub at_top { print "Foo is: $foo\n"; } # This is the body of the script, where a head-to-tail flow is in effe +ct my $foo = "I wanted this to be visible only for the body, not all of m +y subs!"; at_top(); # "Foo is: " at_bottom(); # "Foo is: I wanted ..." sub at_bottom { # We're in the same lexical scope!! print "Foo is: $foo\n"; }


Using strict, but not getting a warning? Yep, because $foo is an "unintended global" (thank you for giving the phenomenon a name, demerphq)
use strict; my $foo = "blah"; my $bar = "hello, world!"; sub mistake { print $foo; # Works, and prints blah }

++ vs lbh qrpbqrq guvf hfvat n ge va Crey :)
Nabgure bar vs lbh qvq fb jvgubhg ernqvat n znahny svefg.
-- vs lbh hfrq OFQ pnrfne ;)
    - Whreq

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-04-18 03:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found