Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Can figure out a way to get File::Grep's fgrep to chomp matches

by haukex (Chancellor)
on May 14, 2017 at 08:45 UTC ( #1190258=note: print w/replies, xml ) Need Help??


in reply to Can figure out a way to get File::Grep's fgrep to chomp matches

The File::Grep documentation for fgrep says "While you can change $_ if necessary, only the original value of the line will be added to the returned list", so if you really want to use this module you'll have to use one of the workarounds presented by the monks here.

However, the doc also says "File::Grep mimics the functionality of the grep function in perl", which is contradictory since in Perl's grep, modifying $_ does modify the original value. Despite Perl's own documentation saying "This is usually something to be avoided when writing clear code", I'd still argue that if fgrep is supposed to be like grep, it should emulate this behavior too. Even though a look at the source doesn't show any serious problems, personally I probably wouldn't use this module for two additional reasons: it doesn't report any errors by default (and even when they're enabled they're only warnings) and it doesn't allow the setting of open modes like :crlf or :utf8.

I'd probably just go with a good old while(<$fh>) loop.

for my $file (@files) { open my $fh, '<', $file or die "$file: $!"; while (<$fh>) { /^[2-6] .+$/ or next; chomp; push @results, $_; } close $fh; }

Update: Punctuation.

Replies are listed 'Best First'.
Re^2: Can figure out a way to get File::Grep's fgrep to chomp matches
by LanX (Archbishop) on May 14, 2017 at 21:43 UTC
    Furthermore it wasn't maintained for a decade and has a random interface.
    • fmap and fgrep only accept filenames on the RHS, hence they can't be chained.
    • The returned data structure is different in both cases. fgrep a AoH and fmap an array.
    At least that's what I read from the POD and LA's posts.

    I'd call this experimental abandonware.

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (10)
As of 2019-09-17 19:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The room is dark, and your next move is ...












    Results (215 votes). Check out past polls.

    Notices?