Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: local vs my

by JupiterCrash (Monk)
on Sep 02, 2002 at 14:56 UTC ( #194596=note: print w/ replies, xml ) Need Help??


in reply to local vs my

A difference between the 'my' function and the 'local' function that is not immediately obvious after reading their descriptions is illustrated by running the below snippet. It shows that 'my' is a bit more powerful, it keeps the variable's value completely within the block. While local allows calling subs to also access the variable's value within that block. Unless there are areas in your program that _expect_ this behavior, you should be fine changing everything to 'my'.

sub a { print "localVar is : $localVar \n"; print "myVar : $myVar \n" } sub b { local ($localVar); my ($myVar); $localVar = "I am localVar."; $myVar = "I am myVar."; &a; } # main &b;


Comment on Re: local vs my
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://194596]
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-24 01:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (135 votes), past polls