Perl Function doesn't work in CGI script 
Author Message
 Perl Function doesn't work in CGI script

The following code using a variablne ($tmp) works in a standard perl
script (as long as the file exists) as it is and a hard-coded
comparison ( if ( -f "/tmp/free2" ) ).

But in a CGI script for our Web Server, only the hard-coded version
works. Does anyone know why?

I can use an OPEN statement with a WHILE loop to print the contents of
a directory in the CGI script, but the code to determine whether the
item is a DIRECTORY or FILE DOES NOT WORK unless the text is
hardcoded.

# *** VARIABLE VERSION ***
!/usr/bin/perl
$tmp = "/tmp/usr";
if ( -d $tmp ) { print "DIRECTORY\n\n"; }
if ( -f $tmp ) { print "FILE\n\n"; }

# *** HARDCODED VERSION ***
!/usr/bin/perl
if ( -d "/tmp/usr"   ) { print "DIRECTORY\n\n"; }
if ( -f "/tmp/free2" ) { print "FILE\n\n"; }



Wed, 02 Feb 2005 00:23:40 GMT  
 Perl Function doesn't work in CGI script

Quote:

> The following code using a variablne ($tmp) works in a standard perl
> script (as long as the file exists) as it is and a hard-coded comparison
> ( if ( -f "/tmp/free2" ) ).

> But in a CGI script for our Web Server, only the hard-coded version
> works. Does anyone know why?

Change:

#!/usr/bin/perl

to:

#!/usr/bin/perl -Tw

Does that tell you anything?



Wed, 02 Feb 2005 00:51:53 GMT  
 Perl Function doesn't work in CGI script

Quote:

> The following code using a variablne ($tmp) works in a standard perl
> script (as long as the file exists) as it is and a hard-coded
> comparison ( if ( -f "/tmp/free2" ) ).

> But in a CGI script for our Web Server, only the hard-coded version
> works. Does anyone know why?

> I can use an OPEN statement with a WHILE loop to print the contents of
> a directory in the CGI script, but the code to determine whether the
> item is a DIRECTORY or FILE DOES NOT WORK unless the text is
> hardcoded.

> # *** VARIABLE VERSION ***
> !/usr/bin/perl
> $tmp = "/tmp/usr";
> if ( -d $tmp ) { print "DIRECTORY\n\n"; }
> if ( -f $tmp ) { print "FILE\n\n"; }

> # *** HARDCODED VERSION ***
> !/usr/bin/perl
> if ( -d "/tmp/usr"   ) { print "DIRECTORY\n\n"; }
> if ( -f "/tmp/free2" ) { print "FILE\n\n"; }

Did you test exactly these snippets as posted? Both versions above work
for me as a CGI script under -wT (with a Content-type header added).
Your question does not make quite clear if you are reading pathnames
from a text file (open() and while(<FILEHANDLE>)) or from an existing
directory (opendir() and for(readdir DIRHANDLE)). If you were reading
from a text file, you perhaps forgot to chomp(), if you were reading
from a directory, you perhaps forgot either to chdir to that directory
before doing the file tests (or to extend to absolute paths). To further
help you it would be necessary to have the code doing the assignments to
$tmp...

Bodo



Wed, 02 Feb 2005 10:00:03 GMT  
 Perl Function doesn't work in CGI script

Quote:

> Did you test exactly these snippets as posted? Both versions above work
> for me as a CGI script under -wT (with a Content-type header added).
> Your question does not make quite clear if you are reading pathnames
> from a text file (open() and while(<FILEHANDLE>)) or from an existing
> directory (opendir() and for(readdir DIRHANDLE)). If you were reading
> from a text file, you p#!/usr/bin/perl

Here is the file.  The -f and -d conditions don't work within the WHILE loop.

#!/usr/bin/perl
use CGI;

CGI::ReadParse;
$query = new CGI;
$count = 0;

$path = '/tmp';

print "Content-Type: text/html\n\n";

$cmd = 'ls ' . $path ;

#############################################
#open(INFILE,"<header.html");
#while (<INFILE>) {
#       print "<CENTER>",$_,"</CENTER/>";
#}
#close(INFILE);
#############################################

print "<FONT SIZE=+1>";

print $cmd;
open(IFILE,$cmd . " | ");
while(<IFILE>) {
        $file = $_;
        print $file;
        if ( -f $file ) { print " 111 FILE"; }    # THESE DON'T WORK
        if ( -d $file ) { print " 222 DIR"; }     # THESE DON'T WORK
        print "<BR>";

Quote:
}

print "------------<br>";
if ( -f "/tmp/orbit-root" ) { print "FILE<br>"; }
if ( -f "/tmp/lost+found" ) { print "FILE<br>"; }
if ( -f "/tmp/tksysv-backup" ) { print "<br>FILE"; }
if ( -f "/tmp/usr" ) { print "<br>FILE"; }
if ( -f "/tmp/linux-backup-count" ) { print "FILE linux-backup-count<br>"; }

print "============<br>";
if ( -d "/tmp/orbit-root" ) { print "Orb DIR<br>"; }
if ( -d "/tmp/lost+found" ) { print "lost DIR<br>"; }
if ( -d "/tmp/tksysv-backup" ) { print "tksys DIR<br>"; }
if ( -d "/tmp/usr" ) { print "USR DIR<br>"; }
if ( -d "/tmp/linux-backup-count" ) { print "linux-backup-count FILE<br>"; }
#############################################
##open(INFILE,"<footer.html");
#while (<INFILE>) {
#       print $_;
#}
#close(INFILE);
#############################################

print "</FORM>";



Fri, 04 Feb 2005 18:15:24 GMT  
 Perl Function doesn't work in CGI script

Quote:

> while(<IFILE>) {

   chomp;

Quote:
>         $file = $_;
>         print $file;

You would have been able to see the problem yourself if you had
printed it so that you could see the problem yourself:

   print "filename is '$file'\n";
                           ^^
                           ^^ betcha see a newline there...

Quote:
>         if ( -f $file ) { print " 111 FILE"; }    # THESE DON'T WORK

--
    Tad McClellan                          SGML consulting

    Fort Worth, Texas


Fri, 04 Feb 2005 20:17:08 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. some html ouput works some doesn't from perl cgi script

2. perl cgi (win95) doesn't work with CGI.pm

3. 1st cgi script doesn't work

4. perl5 doesn't work as cgi script???

5. CGI script : doesn't work!

6. Directory handling script works and doesn't work

7. rand function on Solaris 2.3 doesn't work

8. Print function doesn't seem to work

9. Print function doesn't seem to work

10. Perls remove directory function doesn't work.

11. My function call doesn't seem to work sometimes

12. Crypt function doesn't appear to work

 

 
Powered by phpBB® Forum Software