Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^2: sub that sets $_

by ikegami (Patriarch)
on Jun 28, 2005 at 15:19 UTC ( [id://470668]=note: print w/replies, xml ) Need Help??


in reply to Re: sub that sets $_
in thread sub that sets $_

Don't forget to use local $_ before while ( $_ = $reader->chunk ) {, or you might clobber something without even realizing it. For example, the following snippet replaces the content of @array with false values because $_ is (in turn) a reference to each element of @array:

foreach (@array) { ... my $reader = ...; ... while ($_ = $reader->chunk) { ... } ... }

The following dies with the run-time error "Modification of a read-only value attempted":

for ('a', 'b') { ... my $reader = ...; ... while ($_ = $reader->chunk) { ... } ... }

Replies are listed 'Best First'.
Re^3: sub that sets $_
by tlm (Prior) on Jun 28, 2005 at 15:32 UTC

    Yes. Actually, nobull recently made the case for localizing *_ instead of $_, although this added precaution is probably more important for functions and methods in modules than for top-level code.

    the lowliest monk

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (2)
As of 2024-04-20 03:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found