Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Answer: What is the difference between 'local' and 'my'?

by DigitalKitty (Parson)
on Apr 03, 2002 at 20:49 UTC ( #156435=categorized answer: print w/replies, xml ) Need Help??

Q&A > subroutines > What is the difference between 'local' and 'my'? - Answer contributed by DigitalKitty

Hi.

When you type local variable name, the var is actually global. It can be seen by any part of your script. If you type my varname, the varname is private. It can only be seen by that 'namespace'. Example:

#!/usr/bin/perl -w use strict; my $name; print "Enter your name: "; chomp( $name = <STDIN> ); print "Hi $name\n"; &Greeting; sub Greeting { my $name; print "Hi $name!"; }

The Greeting subroutine has it's own scalar variable called $name. Whatever it contains has nothing to do with the outside 'world'. Using local is a little more complex than that, but hopefully this will help. A great explanation can be found in Learning Perl 3rd edition by O'Reilly publishing.

-DK

Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2017-03-25 03:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should Pluto Get Its Planethood Back?



    Results (310 votes). Check out past polls.