Reputation: 164
I am executing my script this way:
./script.pl -f files*
I looked at some other threads (like How can I open a file in Perl using a wildcard in the directory name?)
If i hard code the file name like it is written in this thread I get my desired result. If I take it from the command line it does not.
My options subroutine should save all the files I get this way in an array.
my @file;
sub Options{
my $i=0;
foreach my $opt (@ARGV){
switch ($opt){
case "-f" {
$i++;
### This part does not work:
@file= glob $ARGV[$i];
print Dumper("$ARGV[$i]"); #$VAR1 = 'files';
print Dumper(@file); #$VAR1 = 'files';
}
}
$i++;
}
}
It seems the execution is interpreted in advance and the wildcard (*) is dropped in the process.
Desired result: All files beginning with files are saved in an array, after execution from the command line.
I hope you get my problem. If not feel free to ask.
Thank you.
Upvotes: 0
Views: 1067
Reputation: 54381
Since your shell is already expanding the glob files*
into a list of filenames, that's what the Perl program gets.
$ perl -E 'say @ARGV' files*
files1files2files3
There's no need to do that in Perl, if your shell can do it for you. If all you want is the filenames in an array, you already have @ARGV
which contains those.
Upvotes: 0
Reputation: 53508
Well, first I'd suggest using a module to do args on command line: Getopt::Long for example.
But otherwise your problem is simpler - your shell is expanding the 'file*' before perl gets it. (shell glob
is getting there first).
If you do this with:
-f 'file*'
then it'll work properly. You should be able to see this - for example - if you just:
use Data::Dumper;
print Dumper \@ARGV;
I expect you'll see a much longer list than you thought.
However, I'd also point out - perl has a really nice feature you may be able to use (depending what you're doing with your files).
You can use <>
, which automatically opens and reads all files specified on command line (in order).
Upvotes: 2