http://www.perlmonks.org?node_id=77832
dpatrick has asked for the wisdom of the Perl Monks concerning the following question:

leaving no error message. I ran it from the command line and it runs into trouble when universedAction=Start is sent in the query string. I used the debugger to step into the if statement but I still can't see what the problem is...
#!/usr/local/bin/perl use strict; no strict "refs"; use CGI; use Proc::ProcessTable; our $statusCGI = new CGI; # Configuration variables our $acmePath = "/usr/local/acme"; our $universedBin = "universed"; our $worldBin = "world"; our %universed = (); our %world = (); sub queryProcesses { my $server = shift; my $found = 0; my @found; my $process = new Proc::ProcessTable(); my $processTableRecord; foreach $processTableRecord (@{$process->table}) { if ($processTableRecord->fname eq $server) { $found++; push @found, $processTableRecord; } } if ($found == 0) { %{$server} = ( status => "<font color=\"red\" face=\"Arial, Helvetica +, sans serif\">Down</font>", server => $server, action => "Start" ); } elsif ($found == 1) { %{$server} = ( status => "<font color=\"green\" face=\"Arial, Helveti +ca, sans serif\">Up</font>", PID => $found[0]->pid, server => $found[0]->fname, action => "Stop" ); #print $processTableRecord->uid, "\n"; } else { } } sub printStatusHTML { print $statusCGI->header( "text/html" ), $statusCGI->start_html( "chat.educ.foo.edu Administration" ); print <<EOHTML; <table align="center" border="0" cellspacing="1" cellpadding="2"> <tr><td colspan="5"> <font color="#000000" face="Arial, Helvetica, sans serif" size="+1"><b +>Server Stats</b></font> <font color="#000000" face="Arial, Helvetica, sans serif"> as of</font +> </td></tr> <tr><td bgcolor="#000077"> <form action="status.cgi" method="POST"> <font color="#FFFFFF" face="Arial, Helvetica, sans serif">Status</font +> </td><td bgcolor="#000077"> <font color="#FFFFFF" face="Arial, Helvetica, sans serif">Server</font +> </td><td bgcolor="#000077"> <font color="#FFFFFF" face="Arial, Helvetica, sans serif">Uptime</font +> </td><td bgcolor="#000077"> <font color="#FFFFFF" face="Arial, Helvetica, sans serif">User</font> </td><td bgcolor="#000077"> <font color="#FFFFFF" face="Arial, Helvetica, sans serif">PID</font> </td><td bgcolor="#000077"> <font color="#FFFFFF" face="Arial, Helvetica, sans serif">Action</font +> </td></tr> <tr><td bgcolor="#EEEEEE"> <font color="#000000" face="Arial, Helvetica, sans serif">$universed{s +tatus}</font> </td><td bgcolor="#EEEEEE"> <font color="#000000" face="Arial, Helvetica, sans serif">$universed{s +erver}</font> </td><td bgcolor="#EEEEEE"> <font color="#000000" face="Arial, Helvetica, sans serif"></font> </td><td bgcolor="#EEEEEE"> <font color="#000000" face="Arial, Helvetica, sans serif"></font> </td><td bgcolor="#EEEEEE"> <font color="#000000" face="Arial, Helvetica, sans serif">$universed{P +ID}</font> <input type="hidden" name="universedPID" value="$universed{PID}"> </td><td align="center" bgcolor="#EEEEEE"> <input type="hidden" name="universedAction" value="$universed{action}" +> <input type="submit" value="$universed{action}"> </form> </td></tr> <tr><td bgcolor="#f2eedb"> <form action="status.cgi" method="POST"> <font color="#000000" face="Arial, Helvetica, sans serif">$world{statu +s}</font> </td><td bgcolor="#f2eedb"> <font color="#000000" face="Arial, Helvetica, sans serif">$world{serve +r}</font> </td><td bgcolor="#f2eedb"> <font color="#000000" face="Arial, Helvetica, sans serif"></font> </td><td bgcolor="#f2eedb"> <font color="#000000" face="Arial, Helvetica, sans serif"></font> </td><td bgcolor="#f2eedb"> <font color="#000000" face="Arial, Helvetica, sans serif">$world{PID}< +/font> <input type="hidden" name="worldPID" value="$world{PID}"> </td><td align="center" bgcolor="#f2eedb"> <input type="hidden" name="worldAction" value="$world{action}"> <input type="submit" value="$world{action}"> </form> </td></tr> <tr><td colspan="5"> <font color="#000000" face="Arial, Helvetica, sans serif"><a href="htt +p://www.activeworlds.com/help/aw31/uniserver.html" target="_blank">Un +iserver Documentation</a> | <a href="http://www.activeworlds.com/help +/aw31/world.html" target="_blank">Worlds Server Documentation</a></fo +nt> </td></tr> </table> EOHTML print $statusCGI->end_html; } our $universedAction = $statusCGI->param("universedAction"); our $worldAction = $statusCGI->param("worldAction"); if ( $statusCGI->query_string eq "" ) { queryProcesses($universedBin); queryProcesses($worldBin); printStatusHTML(); } elsif ( $statusCGI->param("universedAction") eq "start") { die(print STDERR "Got to first elsif.") unless system("/usr/local/ +bin/universed start"); sleep 5; queryProcesses($universedBin); queryProcesses($worldBin); printStatusHTML(); ; } elsif ($statusCGI->param("universedAction") eq "stop") { die unless system("/usr/local/bin/universed stop"); sleep 5; queryProcesses($universedBin); queryProcesses($worldBin); printStatusHTML(); } elsif ($statusCGI->param("worldAction") eq "start") { die unless system("/usr/local/bin/world start"); sleep 5; queryProcesses($universedBin); queryProcesses($worldBin); printStatusHTML(); } elsif ($statusCGI->param("worldAction") eq "stop") { die unless system("/usr/local/bin/world stop"); sleep 5; queryProcesses($universedBin); queryProcesses($worldBin); printStatusHTML(); }
Thanks in advance to everyone.
dpatrick

Replies are listed 'Best First'.
Re: This fails mysteriously everytime...
by dws (Chancellor) on May 04, 2001 at 03:38 UTC
    ...it runs into trouble when universedAction=Start is sent in the query string

    The script is doing case-dependent string comparisons. Since "start" ne "Start", the script will fall off the bottom.

    Either send "start", or change   our $universedAction = $statusCGI->param("universedAction");
    to   our $universedAction = lc($statusCGI->param("universedAction"));
    and use $universedAction in your comparisions (instead of re-getting the parameter each time).

      Thanks! I didn't notice my lowercast starts. I must study closer before I post.
      dpatrick
Re (tilly) 1: This fails mysteriously everytime...
by tilly (Archbishop) on May 04, 2001 at 22:26 UTC
    A tip for future reference.

    In every control structure, if you have a limited number of options put an "else" case in that dies with an informative message. That will make this kind of error much easier to notice and track down...