testing for directories/files fails

R

Ravi Parimi

Hi,

I am trying to print the list of files(excluding directories) in a
directory with a Perl/CGI script:

#!/usr/bin/perl -w

print "Content-type:text/html\n\n";
print "<pre>\n";

opendir DH,"/var/www/cgi-bin/results" or die "$!";
foreach my $ls (readdir DH) {
next if -d $ls;
print "$ls\n";
}
print "</pre>\n";

The above script gives the correct output(i.e. does not print directories)
when I run it from the command line. However, when run as a CGI, it even
prints the directory names as files. Is this normal behaviour? Any help is
appreciated.

Thanks,
--ravi
 
R

Ravi Parimi

I am trying to print the list of files(excluding directories) in a
directory with a Perl/CGI script:

#!/usr/bin/perl -w

print "Content-type:text/html\n\n";
print "<pre>\n";

opendir DH,"/var/www/cgi-bin/results" or die "$!";
foreach my $ls (readdir DH) {
next if -d $ls;
print "$ls\n";
}
print "</pre>\n";

The above script gives the correct output(i.e. does not print directories)
when I run it from the command line. However, when run as a CGI, it even
prints the directory names as files. Is this normal behaviour? Any help is
appreciated.

I just realized that the CGI script wouldn't know the absolute path of
each of the directory entries (and interprets all entries as files?).

Upon adding:

use File::Spec;
..
<remaining lines>
..
foreach my $ls (readdir DH) {
my $abs_file = File::Spec::Unix->rel2abs($ls,"/var/www/cgi-bin/results");
 
G

gnari

Ravi Parimi said:
... Can anyone explain why all entries are interpreted as
files?

you assume too much.

just because -d $some_ramdom_string fails does
not mean that -f some_ramdom_string will not

hint: what is the current directory?
hint2: FAQ

gnari
 
T

Tad McClellan

hint0: read the documentation for the function that you are using:

perldoc -f readdir

If you're planning to filetest...
 
M

Michele Dondi

foreach my $ls (readdir DH) {
next if -d $ls; ^^^^^^^^^^^^^^
print "$ls\n";
}
[snip]
foreach my $ls (readdir DH) {
my $abs_file = File::Spec::Unix->rel2abs($ls,"/var/www/cgi-bin/results");

works as expected. Can anyone explain why all entries are interpreted as
files?

Because they are not! You just say to skip over to the next iteration
of the loop if(f) the argument (exists and) is a directory. In the
code above (when run as CGI) $ls doesn't exist, so a priori it is not
a directory and you do not skip over to the next item, period!

In other words

(!-d && -f) != 1;

[In the sense that (! -d && -f) is not guaranteed to be *always* true]

As an aside you may think of passing from "$ls\n" to doing something
useful with $\;


Michele
 

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

Members online

Forum statistics

Threads
474,039
Messages
2,570,375
Members
47,020
Latest member
anuradha

Latest Threads

Top