in reply to readdir return nul string

Your problem is getting a count of 7 when there are only 5 files, so try moving $number++ AFTER you check to see if the entry is '.' or '..'. Since you're counting BEFORE you get to that logic, you get 7, not 5. If you want to count only the '*.lip' files, move it to after that check.
opendir( DIR, $directory ) or die "open dir error"; my $number = 0; while(defined($name = readdir (DIR))) { if($name eq "") { print "Value -$name* is null string." } print "Loop $number-->$name**\n"; next if $name eq "." || $name eq ".."; $number++; next unless $name =~ /(.*)\.lip$/; push @names, $name; }
Thanks oha for the clean version of the code.

Replies are listed 'Best First'.
Re^2: readdir return nul string
by marscld (Beadle) on Sep 26, 2007 at 13:15 UTC
    Thank you guys. There is another scenario, if the Perl file is accessed through web(apache http server is started), that is to use http://localhost/test/test.pl, problem will raise. In each loop,the statement $name = readdir (DIR) will get '', instead of filename.However, if run the file as "perl -w test.pl" in linux, everything ok. Could you please gimme some advice on this issue? Thank you.