readdir and regex

P

poncenby

my perl program accepts arguments which will match a directory under
$path. however this argument may not be specified so all directories
shall be processed under $path.
this is the way i'm doing it:

my $idopt = $ARGV[0];
my $path = "/home/user/";
opendir (ROOT, "$path");

if ($idopt) { $idre = qr/$idopt/o; } elsif ( !$idopt) { $idre =
"!/^\.\.?$/"; }

my @dirid = grep { $idre }, readdir (ROOT);

however the dirid array is populated with every directory under $path,
regardless of what I specify in ARGV[0].

can anyone help?

thanks
 
M

Mirco Wahab

poncenby said:
my perl program accepts arguments which will match a directory under
$path. however this argument may not be specified so all directories
shall be processed under $path.
this is the way i'm doing it:

my $idopt = $ARGV[0];
my $path = "/home/user/";
opendir (ROOT, "$path");

if ($idopt) { $idre = qr/$idopt/o; } elsif ( !$idopt) { $idre =
"!/^\.\.?$/"; }

my @dirid = grep { $idre }, readdir (ROOT);

however the dirid array is populated with every directory under $path,
regardless of what I specify in ARGV[0].

Of course, there is a lot of things that went wrong here ;-)

Try to modify your program step for step
and look what happens.

Here's my first shot at it:

...
my $path = '/home/user/';
my $idre = $ARGV[0] ? qr{$ARGV[0]} : '^(\.(?!\.))?[^\.].+';

opendir ROOT, $path or die "open failed $!";
my @dircont = grep /$idre/, readdir ROOT;
closedir ROOT;

print join "\n", @dircont;


Regards

Mirco
 
A

anno4000

poncenby said:
my perl program accepts arguments which will match a directory under
$path. however this argument may not be specified so all directories
shall be processed under $path.
this is the way i'm doing it:

my $idopt = $ARGV[0];
my $path = "/home/user/";
opendir (ROOT, "$path");

if ($idopt) { $idre = qr/$idopt/o; } elsif ( !$idopt) { $idre =
"!/^\.\.?$/"; }

my @dirid = grep { $idre }, readdir (ROOT);

The line above is a syntax error. It can't be your real code.
Please copy/paste code instead of re-typing it. Typos like that
confuse the issue unnecessarily.

Anno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top