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

Theodore Katseres tedkat at gmail.com
Tue Nov 15 14:20:40 PST 2005


---------- Forwarded message ----------
From: Theodore Katseres <tedkat at gmail.com>
Date: Nov 15, 2005 2:54 PM
Subject: Re: [Omaha.pm] Perl and recursion...
To: dan at 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 at 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=||


More information about the Omaha-pm mailing list