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 (Saint) on May 14, 2017 at 21:43 UTC |