in reply to Re: Bad file descriptor error
in thread Bad file descriptor error

It is also an example where consistent indentation would have helped to spot the problem, as well as wider indentation of 4 spaces instead of 2 and comments:
sub getdir { my $dir = shift; # Open the directory handle, reporting the reason ($!) opendir (my $dh, $dir) || die qq(Cannot opendir: $dir: $!); while( my $file = readdir($dh)) { next if ($file =~ m[^\.{1,2}$]); # Ignore . and .. my $path = $dir .'/'. $file; if (-e $path) { # do some stuff } elsif (-d $path) { getdir($path); } closedir ($dh); # Close the directory handle - OOPS } }

Replies are listed 'Best First'.
Re^3: Bad file descriptor error
by jethro (Monsignor) on Mar 22, 2010 at 11:51 UTC
    The 4 spaces look much better.

    But the comments you proposed don't add anything of value. "open the directory handle ..." is as easy or difficult to understand as "opendir(...) or ...". Even more obvious with the closedir line, the comment is just redundant noise.

    Comments should add information not repeat it IMHO. A good place for comments is at the start of a subroutine, to exactly specify what parameters are expected and what is returned. Or at really complicated lines to sum up what is done there

      Thank you, everyone.

      I supose the misplacement of the closedir statement was also due to the late hour and lack of sleep, but proper indentation could have avoided that ID10T error.
      File::Find was a godsend in this case. Thank you for the advice.