[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Omaha.pm] Fwd: Perl and recursion...



I'd agree with Theodore's comments...without refering back to documentation, I'm pretty sure you're hitting a scoping issue on your DIR file handle.

As an alternative to frontloading all the filenames into an array, have you looked at the File::Find module? It confused the heck out of me when I first looked at it, but once you figure it out, it can do all sorts of neat stuff with directory trees and their content.

Richard

On Tue, 15 Nov 2005 16:20:40 -0600, Theodore Katseres <tedkat@gmail.com> wrote:

---------- Forwarded message ----------
From: Theodore Katseres <tedkat@gmail.com>
Date: Nov 15, 2005 2:54 PM
Subject: Re: [Omaha.pm] Perl and recursion...
To: dan@linder.org


Sorry, Your code was right!
The problem seems to be with while test. It keeps DIR descriptor open
thru the recursion. I think this is why it dives and then quits.

By loading up your files and directories into memory before recursion
you no longer depend on the File descriptor to keep its previous
state.

    opendir( DIR, $dir ) or die $!;
    my @arr = grep { !/^\.+$/ } readdir(DIR); ## just ignore . and ..
    close(DIR);

    while .....

On 11/15/05, Daniel Linder <dan@linder.org> wrote:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
 On Tue, November 15, 2005 13:31, Theodore Katseres wrote:
 > It appears that no recursion is happening passed the initial dive. {
 > ProcFiles("/tmp") }

 I agree -- when I take out the '&ProcFiles("$dir/$file");' line, it
continues through the /tmp directory fine.  With the recursive call to
itself, I can watch the script dive into the sub-directory (/tmp/dir1), and it processes all the files in /tmp/dir1, but when it finishes with the files in "dir1" the program quits. Unless I am missing something, I thought it should go back one level to the initial invocation of ProcFiles and continue
where it left off there...

 > Check my comments.
 >
 > <Snip && Replace>
 > #!/bin/perl -w
 > use strict;
 >
 > sub ProcFiles {
 > my $dir = shift;
 >
 > opendir( DIR, $dir ) or die $!;
 >
 > while ( my $file = readdir(DIR) ) {
 >
 > next if ( -d "$dir/$file" && $file =~ /^\./ );
 >
 > # above makes sure this block of code never gets executed

The line you mention ("next if ...") is used to make sure I skip the "." and ".." entries in the subdirectory -- otherwise it would back out of the "/tmp" to "/" and work there! :( (It also has the effect of ignoring files
with a leading "." -- that's also by design.)

 > if ( -d "$dir/$file" ) {
 >
 > printf( "Diving into\"%s/%s\".\n", $dir, $file );
 >
 > &ProcFiles("$dir/$file");
 >
 > next;
 >
 > }
 >
 > # your printing files that don't begin with "."

True -- in my real script, this print line is replaced with a small bit of code to do some real work! In the example I posted to OLUG I wanted to keep
it simple and see if I could make it work...

 > printf( "Adding\"%s/%s\".\n", $dir, $file );
 >
 > }
 >
 > closedir(DIR);
 >
 > }
 >
 > &ProcFiles("/tmp");

 Ted, thanks for your input.

 Dan

 - - - - -
 "Wait for that wisest of all counselors, time." -- Pericles
 "I do not fear computer, I fear the lack of them." -- Isaac Asimov
 GPG fingerprint:6FFD DB94 7B96 0FD8 EADF 2EE0 B2B0 CC47 4FDE 9B68
-----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.1 (GNU/Linux)
iD8DBQFDejqusrDMR0/em2gRAtT2AJ9cL6zR+u/XpmGtjXNCCM/Y9WDcvwCfY1Fp
qXscyNVDA6vb5H8MuKUrEtM= =mFaZ -----END PGP SIGNATURE-----


--
Ted Katseres
      ||=O=||


--
Ted Katseres
      ||=O=||
_______________________________________________
Omaha-pm mailing list
Omaha-pm@pm.org
http://mail.pm.org/mailman/listinfo/omaha-pm



--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/