Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Re^3: Make my script faster and more efficient

by quester (Vicar)
on Dec 03, 2008 at 06:07 UTC ( #727594=note: print w/ replies, xml ) Need Help??

in reply to Re^2: Make my script faster and more efficient
in thread Make my script faster and more efficient

I've gotten gun-shy of chomp as well. In a mixed Unix/Windows environment, or running Cygwin, it's pretty common for $/ to not be set by default to the same line ending that is used in (some of) the input file(s). Monarch's method is more tolerant of incorrect or inconsistent line endings than chomp.

Comment on Re^3: Make my script faster and more efficient
Replies are listed 'Best First'.
Re^4: Make my script faster and more efficient
by gone2015 (Deacon) on Dec 03, 2008 at 13:28 UTC

    Indeed so: CRs can sneak in and ruin your day.

    Looking at:  $_ =~ s/[\r\n]+\z//s ; makes me twitch a bit, since the values of "\r" and "\n" can vary. However, the best known case of  "\n" ne "\x0A" is (old) Mac systems where  "\n" eq "\x0D" and  "\r" eq "\x0A" , so  [\r\n] appears safe. [Nevertheless, I haven't found anything that guarantees that "\n" and "\r" are duals. perlport touches on systems where they aren't ASCII at all, but that's a whole other world of pain.]

    Anyway, I favour:  $_ =~ s/\s+\z// ; on the basis that it finesses the issue and gets rid of any other trailing whitespace -- two birds, one stone.

    Mind you, I have seen "\s" defined to be [ \t\n\r\f] -- but current perlreref says it's "whitespace".

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2015-11-25 23:46 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (693 votes), past polls