Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

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

by Marshall (Canon)
on Jul 09, 2020 at 05:42 UTC ( #11119066=note: print w/replies, xml ) Need Help??


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

Geez. At the first block of code, why not:
my @list = map { s/\r\n$//; $_} <$fh>; my @uc_list = map { s/\r\n$//; uc $_; $_} <$lc_fh>; my @split_list = map { s/\r\n$//; split(/\|/, $_); [@$_] } <$piped_fh> +;
UPDATE: I have found some rare issues using chomp() in multi-platform situations. chomp() does a great job when dealing with files generated upon that particular platform. I have written code that is tested on my Windows box and on a Unix box and then a user uses an old Mac to edit the file. chomp() usually works, but if old Mac formatting is allowed, that is not sufficient. To be honest, the last time I got an error report involving this, I told the old Mac user to set his editor to save files as DOS format. I did not change my code to allow old Mac.

The easiest way to make sure that you delete all of the potential \r \n characters is to remove all white space at the end of the line. I like the suggestion from ikegami of s/\s+\z//. That does have a potential issue with a tab separated line containing a blank field at the end of the line.

Replies are listed 'Best First'.
Re^2: Perl::Critic says don't modify $_ in list functions and other things
by Tux (Abbot) on Jul 09, 2020 at 08:35 UTC

    If you have 5.14.0 or higher, why not use s///r?

    my @list = map { s/\r\n$//r } <$fh>; my @uc_list = map { uc s/\r\n$//r } <$lc_fh>; my @split_list = map {[ split m/\|/ => s/\r\n$//r ]} <$piped_fh>;

    Enjoy, Have FUN! H.Merijn

      From OPs signature: 'My OS is Debian 10 (Buster); my perl versions are 5.28.1 local and 5.8.8 on web host.'

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
    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:  <%-{-{-{-<

      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.)

Re^2: Perl::Critic says don't modify $_ in list functions and other things
by perlfan (Vicar) on Jul 09, 2020 at 16:03 UTC
    This is a good point.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2021-01-17 23:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?