in reply to what is the difference between 'my' and 'local'?
My: as said before, it creates a distinct, i.e., different instance of a variable. For example:
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 ...)$a = "a"; EnclosingBlock: { my $a = "b"; print $a; }; print $a;
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:
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.$var = 'a'; { $tmp = $var; $var = 'whatever_I_want'; ... #at end of scope $var = $tmp; }
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(...)'.
|
|---|