issue with closedir

S

solaristar

i had this working with close(DIR); but im trying to do some best
practices code here and apparently closedir is more what i should be
using

here's the relevant code snippet


opendir(my $dh, $basedir) || die "can't open $basedir: $!\n";
my $suff_re = qr/\.[mcu]w\./;
my @servers = grep { !/^\./ && m/$suff_re/ } readdir($dh);
...
...
closedir($dh);


the error

closedir() attempted on invalid dirhandle $dh at ./foo20 line 48.
closedir() attempted on invalid dirhandle $dh at ./foo20 line 48.

line 48 is closedir($dh);

thoughts?
 
U

Uri Guttman

s> i had this working with close(DIR); but im trying to do some best
s> practices code here and apparently closedir is more what i should be
s> using

s> here's the relevant code snippet


s> opendir(my $dh, $basedir) || die "can't open $basedir: $!\n";
s> my $suff_re = qr/\.[mcu]w\./;
s> my @servers = grep { !/^\./ && m/$suff_re/ } readdir($dh);
s> ...
s> ...
s> closedir($dh);


s> the error

s> closedir() attempted on invalid dirhandle $dh at ./foo20 line 48.
s> closedir() attempted on invalid dirhandle $dh at ./foo20 line 48.

s> line 48 is closedir($dh);

without seeing more code, it is hard to tell. the only clue is that you
are getting multiple errors. did $dh get modified somehow before this?
was it closed already before this? perldiag shows this error means you
are closing an already closed dirhandle.

uri
 
S

solaristar

  s> i had this working with close(DIR); but im trying to do some best
  s> practices code here and apparently closedir is more what i should be
  s> using

  s> here's the relevant code snippet

  s>     opendir(my $dh, $basedir) || die "can't open $basedir: $!\n";
  s>     my $suff_re = qr/\.[mcu]w\./;
  s>     my @servers = grep { !/^\./ && m/$suff_re/ } readdir($dh);
  s>     ...
  s>     ...
  s>     closedir($dh);

  s> the error

  s> closedir() attempted on invalid dirhandle $dh at ./foo20 line 48.
  s> closedir() attempted on invalid dirhandle $dh at ./foo20 line 48.

  s> line 48 is closedir($dh);

without seeing more code, it is hard to tell. the only clue is that you
are getting multiple errors. did $dh get modified somehow before this?
was it closed already before this? perldiag shows this error means you
are closing an already closed dirhandle.

uri


Uri, thanks for the reply, i ended up realizing what i did wrong,
thats what i get for programming at 4:30am (without sleep)

the syntax was right i just had the closedir inside a loop which was
messing things up, i moved it up to be below the line above on my
snippet and works good

:)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top