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

Hi All,

been away from Perl for a long time, and am back learning it. I just did a tutorial example, below,

#!/usr/bin/perl use strict; use warnings; sub processFiles { my $path = shift; opendir (DIR, $path) or die "Unable to open $path: !"; my @files = grep { !/^\.{1,2}$/} readdir (DIR); closedir (DIR); foreach my $myfile (@files) { print "Files: $myfile\n"; } }
and I ran the code as,

./direcur .

in a dircetory containing 3 files, and expected the code to return the file names, but, got no output. Any pointers, as I got no errors when running it? Cheers.

coolboarderguy...

Replies are listed 'Best First'.
Re: No Filenames Returned
by Corion (Patriarch) on Mar 23, 2006 at 08:32 UTC

    You never call your subroutine processFiles, so it never gets executed. I think you want to put the following into your script to help tracing what it does:

    print "$0 started at " . localtime; processFiles(@ARGV); print "$0 finished at " . localtime;
      Hi All,

      ah, silly me. Okay, I also added suggested code and get the below,

      racket@ibmlap perl$ ./dirrecur . Unable to open 1: No such file or directory at ./dirrecur line 6. ./dirrecur started at Thu Mar 23 17:49:31 2006racket@ibmlap perl$

      line 6 below,

      opendir (DIR, $path) or die "Unable to open $path: $!";

      Perhaps I'm still missing something, yes? I was following here,

      http://www.perlmonks.org/?node_id=136482

      Cheers.

      coolboarderguy...

        Please use <code> ... </code> around your code, data and console output so it renders and downloads properly (see the Writeup Formatting Tips).

        Please post the complete program you're using and how you are calling it. If you get the error message Unable to open 1, that means that perl tried to open the file 1. So likely, you are setting $path to the wrong value - check where and how you set $path.

        Hm, your code worked for me. It seems that, for some reason, your script is getting 1 as the pathname. So, there are three files in the same directory as the dirrecur script? What are the permissions on those? You might want to do an ls -l . from the prompt to confirm they are there. (I beleive you, but it's occam's razor - eliminate the simple things first) :) You might also try putting the following statement below your use statements:
        use Carp;
        and in the code, instead of die, use croak, like this (I added a carriage return at the end to make it more readable):
        opendir (DIR, $path) or croak "Unable to open $path: $!\n";
        That may give your more verbose information. I find it very useful in my code, as it gives a trace of the subroutine calls, values passed in, etc.

        Update: here's the entire code, as it worked for me. I noticed that you had a typo in your die statement, too - it should be $!, not ! or 1.

        #!/usr/bin/perl use strict; use warnings; use Carp; sub processFiles { my $path = shift; opendir (DIR, $path) or croak "Unable to open $path: $!"; my @files = readdir (DIR); closedir (DIR); foreach my $myfile (@files) { print "Files: $myfile\n"; } } print "$0 started at " . (localtime) . "\n"; processFiles(@ARGV); print "$0 finished at " . (localtime) . "\n";

        -- Burvil

Re: No Filenames Returned
by wulvrine (Friar) on Mar 23, 2006 at 12:17 UTC
    Another way to get the names of files in a directory would be to use the glob function.

    for example:

    #!/usr/bin/perl use strict; use warnings; my @files = glob("*"); map { print "files: $_\n" } @files;