Dear Perl Monks,

when I first started using perl, I had a friend kickstart me with some scripts that I needed at the time. I then went and kept modifying and rearranging those scripts to fit new problems I wanted to solve with perl. Due to this, I still often have snippets in my code that I just use as a black box, which is fine, until I want the behaviour to change slightly.

So now I have this snippet here:

my @files = grep -f, map "$_/vp.o$id", qw/archi1 archiv/;

My guess what this does is: The qw makes an array, which map then takes to create another array that contains the string with $_ replaced by the entries of the first array. And then grep takes this and magic happens, and I get an array of files, if there are files matching this pattern.

So, first question: Am I on the right path here?

Now, what I want to do is basically this:

my @files = grep -f, map "$_/*vp.o$id", qw/ archiv/;

But perl does not understand me, which I also did not really expect in this case. (I hope you do, but just in case, I want all files that match this pattern, just as the shell would work. I realise just now that of course grep in the shell would not work like this, so in the shell I would just grep the first and the second part separately...)

So my second and third questions are: What exactly does this grep -f do? I couldn't find any documentation on this. And how do I have to modify the line so that it finds all files in the directory archiv that match the pattern ".e$id"?

Here's more of my code for context:

#!/usr/bin/perl use strict; use warnings; my ($id) = @ARGV; my $fn2 = do { my @files = grep -f, map "$_/*vp.e$id", qw/ archiv/; print @files . " error files found\nless $files[0]\n" unless @files +== 0; $files[0]; }; my $fn = do { my @files = grep -f, map "$_/*vp.o$id", qw/ archiv/; die @files . " matching files found" unless @files == 1; $files[0]; }; open my $fh, '<', $fn or die qq{Couldn't open "$fn" for reading: $!};

In reply to Need help understanding code snippet with "grep -f" by fifaltra

Title:
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, details, 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, summary, 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.