Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^2: Modification of a read-only value

by Sprad (Hermit)
on Aug 21, 2004 at 16:03 UTC ( [id://384809]=note: print w/replies, xml ) Need Help??


in reply to Re: Modification of a read-only value
in thread Modification of a read-only value

Yes! This was the problem. I'm not sure how far back I'm picking up constant values, but I changed the "while (<PIPE>)" to "while ($foo = <PIPE>)" and that took care of it. Thank you!

I didn't realize that whiles treated $_ differently. I'll watch that in the future...

---
A fair fight is a sign of poor planning.

  • Comment on Re^2: Modification of a read-only value

Replies are listed 'Best First'.
Re^3: Modification of a read-only value
by Aristotle (Chancellor) on Aug 21, 2004 at 16:10 UTC

    :)

    I first saw this in a question in the monastery a long time ago. That was a far nastier case, though: someone was getting spurious action at a distance. Turns out one of his map blocks was calling a sub containing a while(<FH>), and because map aliases $_, the function modified the values in his source array. Ouch.

    Ever since, while(<FH>) used without a local has been a red flag in my mind. Always localize $_ when you use this construct. (Or use a different variable, as you did, of course.)

    Makeshifts last the longest.

Re^3: Modification of a read-only value
by ikegami (Patriarch) on Aug 22, 2004 at 01:36 UTC
    while (<PIPE>)
    is equivalent to
    while (defined($_ = <PIPE>))
    so you might want to use
    while (defined($foo = <PIPE>))
    instead of
    while ($foo = <PIPE>)
    What you are using will stop when you don't expect it (such as when you read in a blank line).

      Perl's one step ahead of you:

      $ perl -MO=Deparse -e'while($foo=<>){}' while (defined($foo = <ARGV>)) { (); } -e syntax OK

      This is a relatively recent change from two or so years ago, though; I don't rememeber which version introduced it off-hand.

      Even without the definedness test it wouldn't stop on a blank line either, btw. Don't forget the record separator. A blank line is not an empty string; it's "\n". What that construct would fail to process is the very last line of a file, if it contains only a 0 and is not terminated by a record separator. (It can't be blank, obviously, because a blank line with no record separator is, well, err, no line at all. :-))

      Makeshifts last the longest.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2025-05-22 06:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.