Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

specifying file extension in opendir function

by greatshots (Pilgrim)
on Nov 16, 2006 at 11:11 UTC ( #584430=perlquestion: print w/replies, xml ) Need Help??

greatshots has asked for the wisdom of the Perl Monks concerning the following question:


opendir ( OLD,"/apps/inst2/metrica/ON-SITE/backup/schema/")
Is is possible to call opendir as follows
opendir ( OLD,"/apps/inst2/metrica/ON-SITE/backup/schema/*.tdef")
I don't find such availability anywhere in perl docs.

Replies are listed 'Best First'.
Re: specifying file extension in opendir function
by themage (Friar) on Nov 16, 2006 at 11:21 UTC
    Hi greatshots,

    opendir doesn't allow wildcards, as it is not designed to list filenames, but to open a special file, that is a directory.

    In the perldoc for opendir (perldoc -f opendir) you have an small example filtering filenames from an opendir and readdir, using grep. You can do something similar:

    opendir(DIR, $some_dir) || die "can't opendir $some_dir: $!"; + @tdefs = grep { /\.tdef$/ && -f "$some_dir/$_" } readdir(DIR); closedir DIR;

      thanks a lot themage. most of the time I am using the solution which you have given to acheive my requirement. thanks a lot for spending your valuable time for reading my node and replied for that.
      $some_dir I don't know what $some_dir has to be written, newbie says. :(
Re: specifying file extension in opendir function
by bart (Canon) on Nov 16, 2006 at 11:36 UTC
    No you can't use wildcards in the directory name, but you can use glob to get all matching directory names. Something like this:
    foreach my $dir (grep -d, glob "/apps/inst2/metrica/ON-SITE/backup/sch +ema/*.tdef") { print "Opening directory $dir\n"; opendir(OLD, $dir) or die "Can't open directory $dir: $!"; while(my $file = readdir OLD) { next if $file eq '.' or $file eq '..'; print " File: $file\n" if -f $file; print " Dir: $file\n" if -d $file; print " Link: $file\n" if -l $file; } closedir OLD; }
Re: specifying file extension in opendir function
by inman (Curate) on Nov 16, 2006 at 14:19 UTC
    Try the following use of a while loop if it fits in with your application logic. It is a neat short cut that may work for your app. The <pattern> construct uses the glob function internally. Details on the patterns that you can use in File::Glob

    while (</apps/inst2/metrica/ON-SITE/backup/schema/*.tdef>){ print; }

    The while loop gets to process the files that match the pattern.

      Please, PLEASE ... just use glob.

      /me really wishes perl didn't have the <> construct doing such drastically different things because parsing it right is hard, e.g.

      my $wildcard = File::Spec->catfile(File::Spec->rootdir(), qw(apps inst2 metrica), $some_location, qw(backup schema), '*.tdef' ); while (<$wildcard>) { print; }
      doesn't work. Change the while to:
      for (glob $wildcard) {
      and it's fine.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://584430]
Approved by bart
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2019-06-24 23:22 GMT
Find Nodes?
    Voting Booth?
    Is there a future for codeless software?

    Results (100 votes). Check out past polls.