Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: local() for scalar file slurping

by revdiablo (Prior)
on Jul 29, 2006 at 15:44 UTC ( [id://564539]=note: print w/replies, xml ) Need Help??


in reply to local() for scalar file slurping

How did I never read anything about the usefulness of local() for such purposes before this? Am I living under a rock?

Indeed, I was blind to local for much of my early Perl years (yes, I literally mean years) also. I read the warnings to never use it, and I took them dead seriously. Once I started toying more, though, I found that there are indeed some good uses of local. I consider what you've shown one of them.

Another one is that you can localize a particular element in an array or a hash. Much as with local in general, this has many bad uses and a few good ones, but it's still a neat trick. Example:

my %hash = qw(one 1 two 2); { local $hash{one} = $hash{one}; $_++ for values %hash; print "$hash{one} $hash{two}\n"; # prints "2 3" } print "$hash{one} $hash{two}\n"; # prints "1 3"

Notice the change to $hash{two} is permanent, but the change to $hash{one} is localized. Pretty cool, eh?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2024-03-19 10:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found