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

Re: is 'my' that much faster than 'local'?

by tadman (Prior)
on Mar 26, 2001 at 21:24 UTC ( [id://67251]=note: print w/replies, xml ) Need Help??


in reply to is 'my' that much faster than 'local'?

It would be difficult to Benchmark a so-called typical script, since the way the variables are used, how many there are, and what kind they are would likely affect performace, gains or otherwise.

The "wrapper" approach seems interesting, but might be missing out on some gains.

Why not this? A 'local' finder program that generates a list of local declarations using a loose regexp. You will have to prune this because some things can't be declared using my, such as '$_' and filehandles, among others. Parsing Perl perfectly (PPP?) is difficult, but this is a pretty specific task, and any failures in parsing should be easy to detect and correct. Perhaps, as a start (quick hack):
$_ = join ('', <STDIN>); s/\blocal(\s*\(?([\$\@\%][a-zA-Z0-9_]*\s*,?\s*)+\)?\s*[;=])/my$1/g; print;
Obviously a more sophisticated example would try and figure out what is being declared, and avoid some traps such as 'my ($_)'. I have tried to avoid 'my-ifying' strings which contain the word 'local' by checking for a '=' or ';' somewhere in there, as well as things that look like variables ($x, @x, %x).

The 'de-localizer' program, for lack of a better name, would output a 'my-ified' version of any source you feed it. You could diff the two, and edit the diff if any errors crop up, or alternatively, write a second utility to analyse the Perl warnings and automatically switch back to local for those particular lines.

Just my $2e-2 worth.

Of course, this program could completely bust your logic because of variable scoping issues that arise from the subtle differences between local() and my(). Be sure to test thoroughly.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (8)
As of 2024-04-24 12:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found