Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^2: Closure producing 'Modification of read-only value' error

by nobull (Friar)
on Jun 11, 2005 at 11:04 UTC ( [id://465787]=note: print w/replies, xml ) Need Help??


in reply to Re: Closure producing 'Modification of read-only value' error
in thread Closure producing 'Modification of read-only value' error

No, don't do that.

Never local $_ in a subroutine. If you want to use the magic assignment you should do local *_. Of course you have to unpack anything you want from @_ first.

If you get into the habit of doing local $_ inside you subroutines it will seem to work fine - maybe for years. Then one day someone will call one of your subroutines inside a for() loop that is iterating over the values of a tied array or hash. Very odd things will start happening in their program.

Replies are listed 'Best First'.
Re^3: Closure producing 'Modification of read-only value' error
by tlm (Prior) on Jun 11, 2005 at 16:39 UTC

    What about the implicit localizations of $_ done by map, grep, for, etc.? Do they do The Right Thing®?

    I think it is surprising (hence bad) that the while ( <FH> ) idiom doesn't do an implicit localization of $_ like the looping constructs listed above. Is there a fundamental reason for this, or is it due to a severe tuit shortage?

    the lowliest monk

      map, grep, for etc do the right thing.

      The magic while construct does not make $_ and alias - it assigns to it. This is fundamentally different. That said, it bugs me that it doesn't do implicit localization.

Re^3: Closure producing 'Modification of read-only value' error
by kaif (Friar) on Jun 13, 2005 at 02:03 UTC

    I'm sorry, maybe I'm dense, but what exactly is different when you iterate over a tied array or hash. What are these "very odd things" and why won't a simple local $_ do? This is very interesting to me.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2024-03-28 21:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found