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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |