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

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

( #2598=categorized question: print w/ replies, xml ) Need Help??
Contributed by Anonymous Monk on Jan 30, 2000 at 03:59 UTC
Q&A  > subroutines


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

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.

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

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

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

See What's the difference between dynamic and lexical (static) scoping? Between local() and my()?, the FAQ answer.

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

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
Answer: What is the difference between 'local' and 'my'?
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

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others wandering the Monastery: (14)
    As of 2014-07-30 08:42 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite superfluous repetitious redundant duplicative phrase is:









      Results (229 votes), past polls