Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

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

by kiseok7 (Beadle)
on Jun 22, 2001 at 04:55 UTC ( [id://90600]=perlquestion: print w/replies, xml ) Need Help??

kiseok7 has asked for the wisdom of the Perl Monks concerning the following question:

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

Replies are listed 'Best First'.
Re: what is the difference between 'my' and 'local'?
by suaveant (Parson) on Jun 22, 2001 at 05:05 UTC
    my makes a clear and distinct instance of a variable, which is only valid within the current scope (i.e. code block or subroutine)...
    Local simply moves the value of the variable to a special place, allows you to do what you want with it in your current scope, and then returns the old value when you leave scope.

    of course, that is just a verbal description, I can't think of a really good way to show it in code off the top of my head, though I have seen a few.

                    - Ant

Re: what is the difference between 'my' and 'local'?
by DBX (Pilgrim) on Jun 22, 2001 at 05:34 UTC
    Another very important point is that "my" variables are not accessible via the symbol table. So if you had:
    package Foo; my $var = 20;
    You could NOT access $var like this:
    print $Foo::var;
Re: what is the difference between 'my' and 'local'?
by dimmesdale (Friar) on Jun 22, 2001 at 05:23 UTC
    Check out the perl manpages, they probably say it better than I can(but then again, maybe not).

    My: as said before, it creates a distinct, i.e., different instance of a variable. For example:

    $a = "a"; EnclosingBlock: { my $a = "b"; print $a; }; print $a;
    The $a within 'EnclosingBlock' is *different* than the one within the general code area. Why's that important? Well, using local, you can get the same effect in this example. . . but in a different way. For example, what if we said my $_? (let us ignore for the moment that we'd get the error: Can't use global $_ in "my" at ...)

    Well, as I said, my creates a different(it helps to think of it that way; this may not be 100% correct in implementation) instance of the variable, so my $_ wouldn't do what we wanted(well, it would do nothing at all).

    That's where local comes into play. Local is almost the same thing as saying this:

    $var = 'a'; { $tmp = $var; $var = 'whatever_I_want'; ... #at end of scope $var = $tmp; }
    All local does is change the value of the variable for the current scope it's in, and upon leaving that scope it changes it back. My, however, makes a variable *distinct* to that scope. The big difference comes when calling subroutines.

    For instance, if you say local $var, then that version of the variable $var would get passed to the subroutines you called.

    There's probably some good nodes on this on the site, so I'd search it if you need more information. Also, the perl manpages document this stuff pretty good. AND, if all else fails, testing out code doesn't hurt, either.

    Sorry, I forgot to mention anything about our. I actually just read about it earlier today when I saw it in another node in meditations. From what I gather, in versions prior to 5.6 its nothing. (though there was a discussion about the error message it would produce.) However, in 5.6+ it is, from what I gather, I don't have 5.6, and what I say is just what I heard, but with that said, it is equivelent to 'use vars(...)'.

Re: what is the difference between 'my' and 'local'?
by andreychek (Parson) on Jun 22, 2001 at 05:30 UTC
    I had trouble with local when I was first getting started too. I actually found it to be a lot simpler then I had made it out to be though. Here are some definitions:
    # Define a global variable, the compatible way use vars qw($foo); # Define a global variable, the Perl 5.6.x way our $foo; # Define a "lexical" variable my $foo;
    Local comes in to play when you have a global/package variable defined, and you want to temporarily use that variable for something else. It often comes up with the special variables, such as $/, which is the variable which defines the input seperator. By default, it is the newline. But what if you would like to suck in an entire file, and not do it line by line? You could do this, not using local:
    undef $/; # Enables whole file mode open (FH, $filename); # Opens the file while(<FH>) { # Contains the whole file blah }
    That works. However, it's not considered "safe". What is considered more safe would be to use local, which would limit the scode of $/ when we undefine it like so:
    { undef local $/; open (FH, $filename); while(<FH>) { blah } }
    When the last closing bracket is reached, the original value of $/ (newline by default) is restored, as the "local" definition goes out of scope.

    Hope that helps!
    -Eric

    Update: Whoops! As Hofmator pointed out, I appear to have forgotten to actually use the local statement :-) It's now fixed. Thanks Hofmator.

      In your last example you missed the localising, it should be:

      { undef local $/; # with localising open (FH, $filename); while(<FH>) { blah } }

      And then - as nobody has done this yet - I add the two definitive links to that topic, both articles from mjd found on his website Coping with Scoping and Seven useful uses of local.

      -- Hofmator

Re: what is the difference between 'my' and 'local'?
by scottstef (Curate) on Jun 22, 2001 at 19:57 UTC
    dominus wrote a brief little explanation on my/local located hereand an article called "Seven Uses for local"here

    "The social dynamics of the net are a direct consequence of the fact that nobody has yet developed a Remote Strangulation Protocol." -- Larry Wall

(tye)Re: what is the difference between 'my' and 'local'?
by tye (Sage) on Jun 22, 2001 at 19:15 UTC

    Go to the "search" box and type in "my local" (without the quotes) and you'll find several discussions on this topic that include links to full-blown tutorials on this topic.

    This... is a recording... (:

            - tye (but my friends call me "Tye")

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-04-24 13:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found