Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Reading a directory through HTML?

Posted on 2000-03-20
10
Medium Priority
?
214 Views
Last Modified: 2010-03-05
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
Comment
Question by:Raydot
  • 5
  • 5
10 Comments
 
LVL 5

Expert Comment

by:PC_User321
ID: 2638149
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
 
LVL 5

Expert Comment

by:PC_User321
ID: 2638272
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
 
LVL 3

Author Comment

by:Raydot
ID: 2638417
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 3

Author Comment

by:Raydot
ID: 2638484
Is there a way to return $! from the command line?
0
 
LVL 3

Author Comment

by:Raydot
ID: 2638501
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
 
LVL 5

Accepted Solution

by:
PC_User321 earned 200 total points
ID: 2638506
Don't understand.
(You can send $! _to_ the command by printing it.)
0
 
LVL 3

Author Comment

by:Raydot
ID: 2638583
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
 
LVL 5

Expert Comment

by:PC_User321
ID: 2638657
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
 
LVL 5

Expert Comment

by:PC_User321
ID: 2638663
-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
 
LVL 3

Author Comment

by:Raydot
ID: 2639639
Guess I'll just have to keep playing around.  Thanks for the help (yet again!)

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question