http://www.perlmonks.org?node_id=175487

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

I can't figure out why my directory listing with an if statement is not working the way I would expect. I want to exclude . and .. from showing up in the list so I did the following:
my $dirname = '.'; opendir(DIR, $dirname) or die "can't opendir $dirname: $!"; while (defined($file = readdir(DIR))) { if ($file ne '.' || $file ne '..'){print "$file\n";} } closedir(DIR);
This prints out a complete list including . and .. I then tried the following to produce only a . and .. listing:
my $dirname = '.'; opendir(DIR, $dirname) or die "can't opendir $dirname: $!"; while (defined($file = readdir(DIR))) { if ($file eq '.' || $file eq '..'){print "$file\n";} } closedir(DIR);
And it works only printing the following:
.
..

Why does it work one way and not the other?

B.T.W. I know that this works great:
opendir (DIR, ".") or die "$!"; @dir = grep !/^\.\.?$/, readdir DIR; closedir DIR;


Sparky
FMTEYEWTK