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(...)'.


In reply to Re: what is the difference between 'my' and 'local'? by dimmesdale
in thread what is the difference between 'my' and 'local'? by kiseok7

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.