in reply to Re: Interview Prepration
in thread Interview Prepration

"2. Explain the difference between my and local.
I'm not sure but I think I heard someone say it was kind of the same thing, internally... I
There is actually a significant difference. "my" creates lexically scoped variables (e.g. existing within a block.) "local" variables have run-time scoping; "local" saves the (previously defined) values of arguments on a stack and later restores them, using the "locally defined" values during some containing scope. Changes made by "local" to global variables can be visible outside the lexical scope of the "local" variables e.g. in nested subroutine calls. I learned about this from the book "Effective Perl Programming" by Joseph Hall, and Randal Schwartz (Addison Wesley Publ.) Highly recommended!

Replies are listed 'Best First'.
Re^3: Interview Prepration
by cog (Parson) on Apr 03, 2005 at 20:47 UTC
    I learned about this from the book "Effective Perl Programming"

    I'm in the middle of a bunch of Perl books (I always am).

    Coincidentally, that's one of them :-) Guess I didn't reach that part yet... :-)

      Me too, and I often go back and read over the ones I've read in the past. It's like watching a movie the second time - you pick up things you missed before. I have one of the first versions of "Effective Perl Programming". The discussion about "my" and "local" is in the chapter on subroutines. Along with typeglobs, it seems one of the more subtle less understood notions in Perl. (My understanding is only partial, for sure.)