barathbr has asked for the wisdom of the Perl Monks concerning the following question:

A friend of mine just pointed me to this rather odd behavior on active perl. Just wondering if anybody else knows about why this happens. The code segment presented below executes fine on A Perl 5.6 but on 5.8 it doesn't seem to hit the "if" at all, anybody else observe this or know why it might happen ??
opendir(ODIR,"c:/") || die "Cannot open this directory: $!"; while($name=readdir(ODIR)) { print "## inside while\n"; if(-d $name) { print "## inside if \n"; # ... # ... } }
Pl. note that when I try to read the same directory with something like this, everything is fine
while (<c:/*>) { if (-d) { print "$_ is a directory\n" } else { print "$_ is a file\n"; } }
Thanks in advance ..

Replies are listed 'Best First'.
Re: odd behaviour with opendir()
by gellyfish (Monsignor) on Nov 04, 2004 at 10:02 UTC

    You should bear in mind that in $name you only have the names of the directory entries within the directory you opened with opendir(); in order to use that entry you will need to put the the full path back, so you might have something like:

    while(my $name = readdir(ODIR)) { if(-d "C:/$name") { print "## inside if \n"; } }

    /J\

      Ah!! thanks. Guess should have read the POD before getting here. Thanks a lot ...