in reply to Re: Re: is 'my' that much faster than 'local'?
in thread is 'my' that much faster than 'local'?
But they often can achieve the same result.Well, let's look at a few other things that people use "without even noticing the difference":
And usually people use them without even noticing the difference...
They're pretty darned close, until you discover the last line of the file you read from doesn't have a newline.
Usually you won't notice a difference, but the first one makes the sub call and passes the current value of @_ as the arguments. Hmm... if you make such a sub call from within another sub and are expecting no arguments to be passed...
This one bites quite a few programmers. It usually doesn't cause a problem, but guess what happens when you do something like @vals[3] = somesub() and &somesub uses wantarray? Then you've got trouble and it can be a nasty bug to find.
My creates a lexical variable that's private to a package (note that this is NOT a package variable). local temporarily replaces the current package variable with another value.
Run the following code:
Guess what? That generates an error similar to the following:use strict; use warnings; print test(); sub test { local $x; $x = 17; }
That's because local does not create variables. Replace the local with a my and it works fine.Global symbol "$x" requires explicit package name at C:\WINNT\Profiles +\Ovid\Desktop\test.pl line 7
For more information, see Coping with Scoping and Seven Useful Uses of local. Both, oddly enough, were written by Dominus, the monk you chose to take to task.
Cheers,
Ovid
Update: After chatting with zenmaster and reading his update, I see that this mostly appears to be a misunderstanding of what he meant to say, but I'll leave the post as it's useful information.
Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.
|
---|