Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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.

In reply to Re: Can figure out a way to get File::Grep's fgrep to chomp matches by haukex
in thread Can figure out a way to get File::Grep's fgrep to chomp matches by Lady_Aleena

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others rifling through the Monastery: (8)
    As of 2019-09-23 15:21 GMT
    Find Nodes?
      Voting Booth?
      The room is dark, and your next move is ...

      Results (280 votes). Check out past polls.