c has asked for the wisdom of the Perl Monks concerning the following question:

I am getting the following error:

syntax error at ./viewlog.cgi line 54, near "while" syntax error at ./viewlog.cgi line 57, near "}" Execution of ./viewlog.cgi aborted due to compilation errors.

for this code:

#!/usr/bin/perl -wT use strict; use Cwd; ## taint environmentals delete @ENV{qw(IFS CDPATH ENV BASH_ENV)}; $ENV{'PATH'} = "/usr/bin:/usr/local/bin"; ## cgi.pm use CGI; $CGI::DISABLE_UPLOADS = 1; $CGI::POST_MAX = 1024; my $query = new CGI; ## config vars my $rcs = (qw$Revision: 1.13 $)[-1]; my $cwd = cwd(); my $domain = (split(/\//,$cwd))[-1]; my %logfile = ( wwwlog => "/var/log/www/www-xfer.$domain", wwwerr => "/var/log/www/www-error.$domain", ftplog => "/var/log/ftp/ftp-xfer.$domain", ftperr => "/var/log/ftp/ftp-error.$domain", ssllog => "/var/log/ssl/ssl-xfer.$domain", sslerr => "/var/log/ssl/ssl-error.$domain" ); ## get form input my $log = $query->param('log') || ''; ## create html print <<HTML Content-type: text/html\n\n <html> <head><title>View Log Files</title></head> <body> <base href="https://www.$domain/"> <form action="controlpanel/viewlog.cgi" method="post"> <select name="log"> <option value="wwwlog">HTTP Traffic Log <option vlaue="wwwerr">HTTP Error Log <option value="ssllog">SSL Traffic Log <option value="sslerr">SSL Error Log <option value="ftplog">FTP Transfer Log <option value="ftperr">FTP Error Log </select> <input type="submit" value="Display Log"> </form> <p> HTML if ($log) { open(LOG, ($logfile{$log})); while (<LOG>) { chomp $_; print "$_\n"; } close(LOG); } print <<HTML <p> </body> </html> HTML

The errors seem to come up from within the whileloop, however I am skeptical that this is actually the root of the cause. When I comment out the entire loop, I see another error complaining about the last HERE doc. However when, I comment everything out from the while statement to the end of the file, the script seems to be happy. Any help is very appreciated.

humbly -c

Replies are listed 'Best First'.
Re: Opening a file called by a hash value
by Corion (Patriarch) on Aug 13, 2001 at 21:08 UTC

    The error is the missing ; at the end of the print statement - an error that's bitten me far too often, but I still like to use here-documents, and I still can't get them right.

    print <<HTML; ... some html HTML

    Later on, you don't seem to be checking the result of your open call. You might want to put either an

    open(LOG,"< $log") or die "Can't open $log : $!";
    or, if that's revealing too much information, handle the failure with an entry in your httpd logfiles and return some other message to the user.

Re: Opening a file called by a hash value
by nehlwyn (Acolyte) on Aug 13, 2001 at 23:14 UTC
    Greetings fellow monk, you seem to be already an experienced programmer so i wonder if what is following will be of any help , but since it won't take me too long to write ... You have already been perfectly answered so i won't add any thing this topic . I will rather tell you how to uncover what was your mistake when you have that kind of error message . It couldn't be a problem with the opening of the file , as you could have been sure of with using the useful checking tip of "or die $!;". Tell yourself that with Perl flexibility , anything that could receive , as an argument , a string between two quotes can be replaced by a variable holding a string : open (FILE,"/root/blahblah"); is litteraly equivalent to $file="/root/blahblah"); open (FILE,$file); So your line hadn't any problem . What could it be then ? Nine times out of ten , it's a problem of a mispelling or missing semicolon . So , as the two lines mentionned didn't and couldn't have any problem , it had to be a problem that existed before and that had been reported from one line to the following until it became critical . So as the other monk did certainly , i looked upwards line after line to find the guilty one ... And bingo !
    <HTML><BODY> the little one. </BODY></HTML>