RE: Problem with recursive routine
Thanks. That is a cool observation. -Original Message- From: Tobias Hoellrich [mailto:thoel...@adobe.com] Sent: Monday, November 28, 2011 3:47 PM To: Barry Brevik; perl Win32-users Subject: RE: Problem with recursive routine You are not changing the directory while traversing. Whenever you access "$nxtfile", you'll need to access it as "$targetdir/$nxtfile". So, this (among other things): $fileTotalSize += (-s $nxtfile);# THIS IS LINE 61 REFERRED TO IN THE ERROR MSG. Needs to become: $fileTotalSize += (-s "$targetdir/$nxtfile");# THIS IS LINE 61 REFERRED TO IN THE ERROR MSG. Cheers - Tobias -Original Message- From: perl-win32-users-boun...@listserv.activestate.com [mailto:perl-win32-users-boun...@listserv.activestate.com] On Behalf Of Barry Brevik Sent: Monday, November 28, 2011 4:39 PM To: perl Win32-users Subject: Problem with recursive routine I'm having a problem with a recursive routine that enumerates a directory tree and all of its files. It works well, except when it goes down 1 level from the top directory, I get this message: Use of uninitialized value in addition (+) at test61.pl line 61. I've been fighting this thing for a couple of hours, and I thought that it was a variable scoping problem, but now I'm not so sure. The code: use strict; use warnings; use Cwd; # Target directory is the current directory. For consistency, # convert '\' into '/' and add a trailing '/' to the directory # path if it is missing. (my $targetdir = cwd()) =~ s/\\/\//g; unless ($targetdir =~ /\/$/) {$targetdir .= '/';} my $prefixFactor = 0; enumerateDirectory($targetdir, $prefixFactor); # - # This routine enumerates the files in the target directory # and traverses the directory tree downwards no matter how # far it goes. The routine does this by being RECURSIVE. # # While processing directories, maintain a prefix factor which # controls the indention of the file and directory display. # sub enumerateDirectory($$) { my ($targetdir, $prefixFactor) = @_; my ($filename, $filesize) = ('', 0); my $fileTotalSize = 0; if (opendir(my $currentDir, $targetdir)) { my $nxtfile = ''; # Enumerate each file in the current directory. # while (defined($nxtfile = readdir($currentDir))) { # If the current file is a directory, follow this logic. if (-d $nxtfile) { # If the directory is '.' or '..' then ignore it. if ($nxtfile eq '.' || $nxtfile eq '..') {next;} # If the directory name returned by readdir() is # missing a trailing '/', add it here. unless ($nxtfile =~ /\/$/) {$nxtfile .= '/';} # Display the directory name then increment the prefix factor. print "\n", ' ' x $prefixFactor, "$nxtfile\n"; $prefixFactor += 2; # Call ourself with the directory name that we are following down. enumerateDirectory($targetdir.$nxtfile, $prefixFactor); # Upon return from the recursive call, de-increment the prefix factor. $prefixFactor -= 2 if $prefixFactor > 0; } else { # If here, we have an ordinary file. Display it. $fileTotalSize += (-s $nxtfile);# THIS IS LINE 61 REFERRED TO IN THE ERROR MSG. print ' ' x $prefixFactor, $nxtfile, ' ', (-s $nxtfile), "\n"; } } # After completely enumerating each directory, be sure to # close the directory entity. close $currentDir; print "\n", ' ' x $prefixFactor, "$fileTotalSize)\n"; } } ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: Problem with recursive routine
You are not changing the directory while traversing. Whenever you access "$nxtfile", you'll need to access it as "$targetdir/$nxtfile". So, this (among other things): $fileTotalSize += (-s $nxtfile);# THIS IS LINE 61 REFERRED TO IN THE ERROR MSG. Needs to become: $fileTotalSize += (-s "$targetdir/$nxtfile");# THIS IS LINE 61 REFERRED TO IN THE ERROR MSG. Cheers - Tobias -Original Message- From: perl-win32-users-boun...@listserv.activestate.com [mailto:perl-win32-users-boun...@listserv.activestate.com] On Behalf Of Barry Brevik Sent: Monday, November 28, 2011 4:39 PM To: perl Win32-users Subject: Problem with recursive routine I'm having a problem with a recursive routine that enumerates a directory tree and all of its files. It works well, except when it goes down 1 level from the top directory, I get this message: Use of uninitialized value in addition (+) at test61.pl line 61. I've been fighting this thing for a couple of hours, and I thought that it was a variable scoping problem, but now I'm not so sure. The code: use strict; use warnings; use Cwd; # Target directory is the current directory. For consistency, # convert '\' into '/' and add a trailing '/' to the directory # path if it is missing. (my $targetdir = cwd()) =~ s/\\/\//g; unless ($targetdir =~ /\/$/) {$targetdir .= '/';} my $prefixFactor = 0; enumerateDirectory($targetdir, $prefixFactor); # - # This routine enumerates the files in the target directory # and traverses the directory tree downwards no matter how # far it goes. The routine does this by being RECURSIVE. # # While processing directories, maintain a prefix factor which # controls the indention of the file and directory display. # sub enumerateDirectory($$) { my ($targetdir, $prefixFactor) = @_; my ($filename, $filesize) = ('', 0); my $fileTotalSize = 0; if (opendir(my $currentDir, $targetdir)) { my $nxtfile = ''; # Enumerate each file in the current directory. # while (defined($nxtfile = readdir($currentDir))) { # If the current file is a directory, follow this logic. if (-d $nxtfile) { # If the directory is '.' or '..' then ignore it. if ($nxtfile eq '.' || $nxtfile eq '..') {next;} # If the directory name returned by readdir() is # missing a trailing '/', add it here. unless ($nxtfile =~ /\/$/) {$nxtfile .= '/';} # Display the directory name then increment the prefix factor. print "\n", ' ' x $prefixFactor, "$nxtfile\n"; $prefixFactor += 2; # Call ourself with the directory name that we are following down. enumerateDirectory($targetdir.$nxtfile, $prefixFactor); # Upon return from the recursive call, de-increment the prefix factor. $prefixFactor -= 2 if $prefixFactor > 0; } else { # If here, we have an ordinary file. Display it. $fileTotalSize += (-s $nxtfile);# THIS IS LINE 61 REFERRED TO IN THE ERROR MSG. print ' ' x $prefixFactor, $nxtfile, ' ', (-s $nxtfile), "\n"; } } # After completely enumerating each directory, be sure to # close the directory entity. close $currentDir; print "\n", ' ' x $prefixFactor, "$fileTotalSize)\n"; } } ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Problem with recursive routine
I'm having a problem with a recursive routine that enumerates a directory tree and all of its files. It works well, except when it goes down 1 level from the top directory, I get this message: Use of uninitialized value in addition (+) at test61.pl line 61. I've been fighting this thing for a couple of hours, and I thought that it was a variable scoping problem, but now I'm not so sure. The code: use strict; use warnings; use Cwd; # Target directory is the current directory. For consistency, # convert '\' into '/' and add a trailing '/' to the directory # path if it is missing. (my $targetdir = cwd()) =~ s/\\/\//g; unless ($targetdir =~ /\/$/) {$targetdir .= '/';} my $prefixFactor = 0; enumerateDirectory($targetdir, $prefixFactor); # - # This routine enumerates the files in the target directory # and traverses the directory tree downwards no matter how # far it goes. The routine does this by being RECURSIVE. # # While processing directories, maintain a prefix factor which # controls the indention of the file and directory display. # sub enumerateDirectory($$) { my ($targetdir, $prefixFactor) = @_; my ($filename, $filesize) = ('', 0); my $fileTotalSize = 0; if (opendir(my $currentDir, $targetdir)) { my $nxtfile = ''; # Enumerate each file in the current directory. # while (defined($nxtfile = readdir($currentDir))) { # If the current file is a directory, follow this logic. if (-d $nxtfile) { # If the directory is '.' or '..' then ignore it. if ($nxtfile eq '.' || $nxtfile eq '..') {next;} # If the directory name returned by readdir() is # missing a trailing '/', add it here. unless ($nxtfile =~ /\/$/) {$nxtfile .= '/';} # Display the directory name then increment the prefix factor. print "\n", ' ' x $prefixFactor, "$nxtfile\n"; $prefixFactor += 2; # Call ourself with the directory name that we are following down. enumerateDirectory($targetdir.$nxtfile, $prefixFactor); # Upon return from the recursive call, de-increment the prefix factor. $prefixFactor -= 2 if $prefixFactor > 0; } else { # If here, we have an ordinary file. Display it. $fileTotalSize += (-s $nxtfile);# THIS IS LINE 61 REFERRED TO IN THE ERROR MSG. print ' ' x $prefixFactor, $nxtfile, ' ', (-s $nxtfile), "\n"; } } # After completely enumerating each directory, be sure to # close the directory entity. close $currentDir; print "\n", ' ' x $prefixFactor, "$fileTotalSize)\n"; } } ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs