Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

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

by Anonymous Monk
on Jan 30, 2000 at 03:59 UTC ( [id://2598]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question: (subroutines)

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

Originally posted as a Categorized Question.

  • Comment on What is the difference between 'local' and 'my'?

Replies are listed 'Best First'.
Re: What is the difference between 'local' and 'my'?
by Dominus (Parson) on Mar 12, 2000 at 02:33 UTC

    There's a detailed discussion of this question and some related matters in the article Coping with Scoping, which appeared in the Perl Journal a while back.

Re: What is the difference between 'local' and 'my'?
by fpina (Pilgrim) on Apr 04, 2001 at 03:33 UTC
    The difference is between dynamic and static scope. 'my' behaves as the auto variables of C/C++ and most other languages. 'local', on the other way, behaves as bind variables in Lisp. The real difference can be seen when you have several scopes.

    Suppose you have the following code:

    our $a = 3; sub f { print "$a\n"; } sub g { my $a = 7; print "$a\n"; &f (); print "$a\n"; } &g (); print "$a\n";
    you would get: 7 - 3 - 7 - 3

    If you substitute the my in 'g' by local, you get: 7 - 7 - 7 - 3

    Internally, the difference in the implementation is that local stores the variable value in a stack for the duration of the scope, and restores it at the end of the scope, while my actually creates a new variable which hides the outer one for the duration of the scope where it is defined, but not for other scopes which may be invoked. creates

Re: What is the difference between 'local' and 'my'?
by merlyn (Sage) on Apr 04, 2001 at 04:02 UTC
Re: What is the difference between 'local' and 'my'?
by dsb (Chaplain) on Jan 25, 2001 at 00:30 UTC
    When you use 'local' in to localize a variable, what is really happening is that Perl will store the value of any variable that already has the localized variables name when the localized variable is initialized. When the sub-routine ends and the program returns to the main body, Perl will restore the old value of the original variable. Ex:
    $a = "one"; print $a, "\n"; routine(); print $a, "\n"; sub routine { local $a = "two"; print $a, "\n"; }
    The output from this program would be:
    one two one
    -kel
      And the output would be exacly the same if you used my. Check the article mentioned above for an explanation on the difference. Doesnt make much sense thoug. The only real use for local as I see it is this:
      1. You have a subroutine which calls a few others, perhaps in someone else's code(a module perhaps).
      2. Now, you need to change a global variable( turn of warning for pre-5.6.0 perl or perhaps turn of buffered output).
      3. You dont want to remember to switch it back to what it was when you return from your routine. This is feasable if you do some error checking on all your calls, and want to return as soon as one of them fails somehow.
      Solution: use local on the global variable, and set it to what you want. All the routines you call see the new value of this variable, and as soon as you return it is automagically reset to what it was.

      If you had used my instead, only your routine would have seen the change. Any routine you called, would still have seen the old value, since my is lexically scoped. GoldClaw

Re: What is the difference between 'local' and 'my'?
by DigitalKitty (Parson) on Apr 03, 2002 at 20:49 UTC
    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
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://2598]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2024-04-26 00:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found