Operating System - HP-UX
1833760 Members
2366 Online
110063 Solutions
New Discussion

Perl: Filename expansion?

 
SOLVED
Go to solution
David_246
Trusted Contributor

Perl: Filename expansion?

Hi,

I do not know if my subject is right, but I at least have your attention :)

I have written a perl program that does a "du -sk" for each subdir of the subdir of the ... 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 in perl? 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
@yourservice
7 REPLIES 7
A. Clay Stephenson
Acclaimed Contributor

Re: Perl: Filename expansion?

If I understand your question and you want the argument list to be just like your example of "ls xx*/oradata/ then you need to understand that it's the shell -- not ls or Perl that is doing the filename expansion. Therefore to process the argument list in Perl just as ls does:

#!/usr/bin/perl -w

my $i = 0;
while ($i <= $#ARGV)
{
printf("%3d. '%s'\n",$i,$ARGV[$i]);
++$i;
}
If it ain't broke, I can fix that.
Brian Bergstrand
Honored Contributor

Re: Perl: Filename expansion?

David,

Try using readdir() instead of forking ls.

local(@filenames) = readdir($directory);
closedir($directory);

for (@filenames) {

}

HTH.

BTW, speaking of the du -ks thread. You forgot to assign points to those that answered that question. Thanks.
Mark Grant
Honored Contributor
Solution

Re: Perl: Filename expansion?

I might be misinterpreting your question but if the problem is that you might have multiple arguments because the user has supplied a "*" then you only need to set @array=@ARGV;

If this isn't the question, could you expand it for me a bit please.

Never preceed any demonstration with anything more predictive than "watch this"
Steven Sim Kok Leong
Honored Contributor

Re: Perl: Filename expansion?

Hi,

I believe there is a syntax error. Replace @array=`ls ...` with @array=(`ls`). An array has to be represented by () braces.

Hope this helps. Regards.

Steven Sim Kok Leong
H.Merijn Brand (procura
Honored Contributor

Re: Perl: Filename expansion?

Main problem which that approach is that backticks return strings that are newline ended, so *if* you take that path, use

chomp (my @list = `ls ...`);

Steven, when assigning backtick output to an array, it's automatically split on lines (including the line endings of course)

BUT, just don't use backticks for functions that are builtin in perl

my @list = glob "..."; # perldoc -f glob

Enjoy, have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Sam_83
New Member

Re: Perl: Filename expansion?

Hi David,

Here's a thought, maybe it suits your need:

1.
use Perl "globbing" [see example below]
It will bring you an array containing the matching file names.

example:
print "Enter dir patch:";
chop($dir = );
@files = <*>;
print "Fifth matched entry is: @files[5]\n.";

2.
then do a directory test (if -d $filename)
for each element in the array.

3.
then do your stuff (du -sk) if the directory test is successful


-Sam
David_246
Trusted Contributor

Re: Perl: Filename expansion?

Hi all,

You solved my issue. Clay already pointed me in a direction, but Mark Grant opened my eyes. When starting a perl script with filename expansion the ARGV are given als multiple ARGV[0], ARGV[1], etc. I was still searching in ARGV[0].
Therefor Mark's remark of :
@array=@ARGV;
Was my solution.

Thanks all for your help !!

Regs David


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