Perl: Filename expansion?

Hi,

I do not know if my subject is right, but I at least have your attention :slight_smile:
Recently I posted a perl question related to this.

Now when someone starts my program using an asterics:

./dirinfo.pl /orapkg/ora0*/oradata/

It can contain multiple directories. The following command shows me those directories : "ls -ad /orapkg/ora0*/oradata "

But how can I import this array? The following does not work :

$directory = @ARGV[0];
if ( $directory =~ /.*\\*.*/ ) { # This works
@array=`ls -ad $directory`; # This does not work !
@arra1=`ls -lad $directory | awk '{print $9}` # Also does not work
@arra2=`eval ls -lad $directory | awk '{print $9}`# Also does not work
}

Any help will be much apreciated !!

Regs David

I suppose the filenames are expanded by the shell if filenames contain an asterisk from the command line. So for

./dirinfo.pl /home/cbkihong/*

the globbed entry '/home/cbkihong/*' is expanded to the files inside that dir, and get stuffed to the corresponding @ARGV entries. So all you need to do is to read the @ARGV array. That's all.

I don't really very understand your problem, and what you mean by those `` lines do not work. Theoretically they should work, and the lines generated by the shell command are split into individual elements in the @array. If you would like to execute the shell command using `` instead of reading from the command line, then maybe you should give more info as to what you mean by "does not work".

If you would simply like to retrieve file entries I would suggest you to do it natively in Perl, using the opendir/readdir/closedir functions instead of using shell invocation of external programs like `ls` inline in Perl programs. I generally consider this more reliable in practice and I try to refrain from shell-scripting style Perl programming unless a native Perl solution is difficult to implement. Possibly because I don't know much shell scripting myself. But this also avoids the restriction of the limit in the length of the command line I heard of if the number of characters expanded by the shell exceeds the shell size limit.

When your perl script is invoked from the shell with wildcards such as the "*", the SHELL expands the arguments. Suppose your current directory only contains 3 directories named "mydir", "hisdir", "newdir". If your program is invoked as follows:

./dirinfo *dir

then the shell expands the *dir so that your program is REALLY invoked like:

./dirinfo hisdir mydir newdir

So, @ARGV will contain ("hisdir", "mydir", "newdir"). Therefore, you simply need to assign @array to @ARGV. Have I misunderstood you ?

I think you mean to assign @ARGV to @array, not the other way round otherwise you destroyed the things in @ARGV.

By the way, that's just pretty a rewrite of what I wrote just now. :smiley:

yeah, my mistake .... I meant assign @ARGV to @array
clobbering the @ARGV would NOT be good :smiley:

Thank you all for your help. Indeed @array=@ARGV; should have been my solution. Unfortunetly I was expecting ARGV[0] to contain a * and than start working on it.
Thanks a lot for your clear answers, all of you !!

Regs David

if (@ARGV) {
@dirm = @ARGV;
foreach $dirm(@dirm) {
$dirm =~ s/\/$//g;
push(@directory, $dirm);
}
}
else {
usage();
}

5 years old and this post is still helpful, thank you unix.com.