Solved

Reading a directory through HTML?

Posted on 2000-03-20
10
189 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
ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

 
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 50 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

809 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