'ls' question, splitting and files with spaces in their name...

Tim Writer tim-s/rLXaiAEBtBDgjK7y7TUQ at public.gmane.org
Mon May 24 16:54:43 UTC 2004


Rick Delaney <rick-h4KjNK7Mzas at public.gmane.org> writes:

> When you get this to compile, please consult the readdir entry in
> perlfunc.  It will explain why it only works for the current directory.

That's a bit misleading.  readdir works on any directory.  However, the
filenames returned are relative to that directory so you should either chdir
there first or prepend the directory name as necessary.  So, in the snippet I
wrote, this:

     ($dev, ....) = stat($_);

should have been:

     ($dev, ....) = stat("$dir/$_");

Alternatively, the loop should have been written:

    chdir($dir) or die "Can't chdir to $dir: $!";
    opendir(DIR, '.') or die "Can't open $dir: $!";
    for (readdir(DIR)) {
        ($dev, ....) = stat($_);
    }
    closedir(DIR);

If you do this, you may want to wrap the whole thing in:

    use Cwd;
    my $saved_dir = cwd();
    # above loop
    chdir($saved_dir);

-- 
tim writer <tim-s/rLXaiAEBtBDgjK7y7TUQ at public.gmane.org>                                  starnix inc.
905.771.0017 ext. 225                           thornhill, ontario, canada
http://www.starnix.com              professional linux services & products
--
The Toronto Linux Users Group.      Meetings: http://tlug.ss.org
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://tlug.ss.org/subscribe.shtml





More information about the Legacy mailing list