in reply to Re: subs && typeglobs
in thread subs && typeglobs

OK so you are hand rolling the old recurse the directory tree chestnut. Here is a script that will do that for you putting all the dirs in @dirs and all the files in @files. I presume you already know about File::Find so won't do the use a module mantra. There is an explanation of this code here

#!/usr/bin/perl -w use strict; my $root = 'c:/cluster1/'; my @dirs = ($root); my @files; for my $path (@dirs){ opendir ( DIR, $path ) or next; # skip dirs we can't read while (my $file = readdir DIR) { # skip the dot files next if $file eq '.' or $file eq '..'; # skip symbolic links to avoid infinite loops next if -l $path.$file; if ( -d $path.$file ) { # add the dirs to our dir list (full path) push @dirs, $path.$file.'/'; } else { # add the files to file list push @files, $path.$file; } } closedir DIR; } print "Directory list\n\n"; print "$_\n" for sort @dirs; print "\n\nFile List\n\n"; print "$_\n" for sort @files;

cheers

tachyon

s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Replies are listed 'Best First'.
Re: subs && typeglobs
by s0ttle (Scribe) on Aug 04, 2001 at 13:44 UTC
    tachyon, that is fine and dandy and I appreciate the code :) but I am more into figuring out why the problem exists, therefore I can better appreciate the solution to the problem.
    dir_search(*D,"$path$cont");
    this seems to be where all the trouble lies, everything previous to this line works as expected. Actually the whole script runs without errors under perl -c it just doesn't do the recursion that I was expecting. To clarify what I'm asking: I want to know why my code doesn't work :)
    #!/usr/bin/perl -w # ## use strict; my $Spath='/home/s0ttlen/'; opendir(H, "$Spath")|| die "Error:$!\n"; dir_search(*H,"$Spath"); sub dir_search{ local(*ROOT)=$_[0]; my $path=$_[1]; my $cont; foreach $cont (sort readdir(ROOT)){ next if $cont eq '.' or $cont eq '..'; next if -l "$path$cont"; if (-f "$path$cont"){ &log("$path$cont") if (-T "$path$cont" || -u "$path$cont"); } elsif (-d "$path$cont" && opendir(D,"$path$cont")) { dir_search(*D,"$path$cont"); } } } sub log{ my $file=$_[0]; my $log='/home/s0ttlen/log/slog'; open(LOG,">>$log")|| die "Error:$!\n"; print LOG "$file\n"; }

      OK here are a couple of debugging print statements that show you why your code fails. The reason is you forget to add the directory separator as you recurse - as a result you do not search a valid path on your first recursion. See my code to see this happening - when you get a dir back from readdir it is called mydir not mydir/ as your code expects, add the / as shown and it will work.

      my $Spath='c:/cluster1/'; opendir(H, "$Spath")|| die "Error:$!\n"; dir_search(*H,"$Spath"); sub dir_search{ local(*ROOT)=$_[0]; my $path=$_[1]; print "path $path\n"; my $cont; foreach $cont (sort readdir(ROOT)){ print " $path$cont\n"; next if $cont eq '.' or $cont eq '..'; next if -l "$path$cont"; if (-f "$path$cont"){ &log("$path$cont") if (-T "$path$cont" || -u "$path$cont"); } elsif (-d "$path$cont" && opendir(D,"$path$cont")) { dir_search(*D,"$path$cont"); # dir_search(*D,"$path$cont/"); <<-- this / is what you nee +d } } }

      cheers

      tachyon

      s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

        sweet savior :)
        Thanks guy.