Solved

Reading a directory through HTML?

Posted on 2000-03-20
10
170 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
pattern matching in perl 2 100
Perl Sort Question 4 117
Perl script to parse xml file 2 96
remove duplicates from the csv file 13 95
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

707 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

18 Experts available now in Live!

Get 1:1 Help Now