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

Re^2: Perl::Critic says don't modify $_ in list functions and other things

by AnomalousMonk (Bishop)
on Jul 09, 2020 at 14:22 UTC ( #11119088=note: print w/replies, xml ) Need Help??


in reply to Re: Perl::Critic says don't modify $_ in list functions and other things
in thread Perl::Critic says don't modify $_ in list functions and other things

my @list = map { s/\r\n$//; $_} <$fh>;

Would any line produced by evaluating the <$fh> expression ever end in \r\n? (This might happen if the file handle had been opened in binmode, but then there would only ever be a single "line" read from the handle and the list processing code would seem superfluous.)

I think s{ $/ \z }{}xms would work, but I also think that's an exact replacement for chomp; no improvement if so. If the /r modifier is available, s{ $/ \z }{}xmsr seems a very attractive alternative.


Give a man a fish:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^3: Perl::Critic says don't modify $_ in list functions and other things
by ikegami (Pope) on Jul 12, 2020 at 14:51 UTC

    Would any line produced by evaluating the <$fh> expression ever end in \r\n?

    On Windows? It would require CR CR LF in the file (assuming default $/ and IO layers). Extremely unlikely.

    Elsewhere? It would require CR LF in the file (assuming default $/ and IO layers). Possible.

    I think s{ $/ \z }{}xms would work

    Well, that should be s{ \Q$/\E \z }{}xms (and /m and /s are useless) to be equivalent to the chomp.

    And if $/ hasn't been changed, s/\n\z// could be used (since $/ defaults to LF on all systems).

    But if I was going with a regex pattern, I'd go with s/\s+\z//. Handles \n, \r\n and other trailing whitespace. (TSV files being an exception.)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2021-01-27 21:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?