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

I wish to go thru a multi level $PATH starting at the file system root, and checking that each sub-level exists, then progressing to the next sub-level. I have searched the net, but found mostly how to find a file. I just need a starting direction, or overview as to the best approch. Your time and effort are much appreciated Thanx

  • Comment on traverse DIR checking each level exists

Replies are listed 'Best First'.
Re: traverse DIR checking each level exists
by nemesdani (Friar) on Mar 10, 2012 at 15:22 UTC
Re: traverse DIR checking each level exists
by TJPride (Pilgrim) on Mar 11, 2012 at 06:48 UTC
    use strict; use warnings; my @dirs = '/'; ### Starting directory my ($dir, $file, $path); while ($dir = shift(@dirs)) { opendir(DIR, $dir); while ($file = readdir(DIR)) { next if $file =~ /^\./; next if $file eq 'dir.pl'; ### Name of your script $path = $dir . $file; if (-d $path) { push (@dirs, "$path/"); next; } ### Do something with file path } }
Re: traverse DIR checking each level exists
by Anonymous Monk on Mar 11, 2012 at 07:36 UTC
    #!/usr/bin/perl -- use Path::Class; my $fi = file( $ENV{TEMP}, 'notexist' ); my @fo = $fi; do { unshift @fo, $fi = $fi->parent; } while $fi ne $fi->parent; print $_,$/ for @fo; __END__ / /some /some/where /some/where/over /some/where/over/temp /some/where/over/temp/notexist