Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 217
  • Last Modified:

Reading a directory through HTML?

This should be simple.  The following script compiles just fine in Perl but returns an error when run through HTML.  Any ideas?

#!/usr/bin/perl

#file to open, read, and print the contents of a directory...

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

&htmlhead("Live Update Page");

opendir(LOGDIR, "../htdocs/dknr/hogo/") || &dirfail;
#opendir(LOGDIR, "../../htdocs/dknr/hogo/") || &dirfail;
#opendir(LOGDIR, "..") || &dirfail;
@dirListing = readdir(LOGDIR);

foreach $outputvar (@dirListing) {
      if ($outputvar ne "..") {
            print "$outputvar\n";
      }
}

closedir(LOGDIR);

&htmltail;




sub dirfail {
      #add code to return more specific error message
      print "Could not find or open requested directory. \n";
}

sub htmlhead {
      print "<HTML>\n<HEAD><TITLE>$_[0]<\/TITLE><\/HEAD>\n<BODY bgcolor='#FFFFFF'>\n";
      print "<H1>The listing you want is here:<\/H1><P>";
}

sub htmltail {
      print "<\/BODY><\/HTML>\n";
}
0
Raydot
Asked:
Raydot
  • 5
  • 5
1 Solution
 
PC_User321Commented:
Have you tried using absolute directory paths?

>> print "Could not find or open requested directory. \n";
>>
How about
   print "Error: $!";
$! is the error string returned by the operating system.
0
 
PC_User321Commented:
Hopefully the $! will shed light on the problem.

BTW, instead of using dirfail(), you could use in-line code to print the error cause and terminate the program:
Replace each
   &dirfail
with
   die $!;

0
 
RaydotAuthor Commented:
Mmm...I get what you're saying but I'm getting the error with everything commented out!

I wonder what the problem is?  Other CGI scripts in the same path are working!  Maybe I should just pull the whole thing out and start over...
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
RaydotAuthor Commented:
Is there a way to return $! from the command line?
0
 
RaydotAuthor Commented:
Are you ready for what a stupid problem I was having?  HomeSite, which is what I was using to edit the scripts, was set to save files in a PC and not Unix format.  I changed that and the problem went away.  If you could just answer my last q about $!, the points are yours PC.
0
 
PC_User321Commented:
Don't understand.
(You can send $! _to_ the command by printing it.)
0
 
RaydotAuthor Commented:
I guess what I mean is, if I have a file named "myFile.pl," can you do something like this?

perl -e $errorCode = (myFile.pl || die $!);

Well  I can see why that wouldn't work, but that's what I mean.  Have some way to return the error if the application doesn't exit properly.  I guess that's that the -w switch basically does though, huh?

0
 
PC_User321Commented:
Thanks for the points.  I am glad that you managed to solve your problem.

Many unix programs return an exit code when they terminate, which the caller can use to determine the success or failure of the command.
Perl does not seem to do this.  Here is an exerpt of Perl documentation that I download from somewhere.  It makes no mention of returning an exit code:

--------------------------------------------------------------------------------
NAME
perlrun - how to execute the Perl interpreter
--------------------------------------------------------------------------------
SYNOPSIS
perl [ -sTuU ] [ -hv ] [ -V[:configvar] ] [ -cw ] [ -d[:debugger] ] [ -D[number/list] ] [ -pna ] [ -Fpattern ] [ -l[octal] ] [ -0[octal] ] [ -Idir ] [ -m[-]module ] [ -M[-]'module...' ] [ -P ] [ -S ] [ -x[dir] ] [ -i[extension] ] [ -e 'command' ] [ -- ] [ programfile ] [ argument ]...
--------------------------------------------------------------------------------


The -w switch is something different - it increases the amount of complaining that the compiler does, which is usually helpful when you are debugging the script.
0
 
PC_User321Commented:
-w
prints warnings about variable names that are mentioned only once, and scalar variables that are used before being set. Also warns about redefined subroutines, and references to undefined filehandles or filehandles opened read-only that you are attempting to write on. Also warns you if you use values as a number that doesn't look like numbers, using an array as though it were a scalar, if your subroutines recurse more than 100 deep, and innumerable other things.
0
 
RaydotAuthor Commented:
Guess I'll just have to keep playing around.  Thanks for the help (yet again!)

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now