Solved

Reading a directory through HTML?

Posted on 2000-03-20
10
180 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

910 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now