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

I took damndirtyape and co.'s advice, and I was able to fix the problems. Now I added more and have the same problem of when I use strict it tells me it "aborted due to compilation errors". When I comment out the use strict line, it works fine. Also, is there a way to perform an on the fly check of values, i.e. print "$hoursWorked{january}{name} or 0"; as you'll see on the red line. Here's the code:
#!/usr/local/bin/perl print "Content-type: text/html\n\n"; use CGI::Carp qw(fatalsToBrowser); #use strict; use CGI; my $q = new CGI; my ($month, $num, @data, $date, $hours, $name, $member, $member1); my $ttlevnts = 0; my $name1 = $q->param("name"); my %ttlhrs = ("bottum", 0, "budrow", 0, "captan", 0, "hughes", 0, "mar +chand", 0, "medina", 0, "newbrough", 0, "rico", 0, "shelton", 0, "swe +redoski", 0, "tryon", 0, "werner", 0, "williamspearce", 0); my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(t +ime); my @members = ("bottum", "budrow", "captan", "hughes", "marchand", "me +dina", "newbrough", "rico", "shelton", "sweredoski", "tryon", "werner +", "williamspearce"); my $month1 = $mon+1; my $year1 = $year+1900; my $date1 = "$month1\/$mday\/$year1"; my %hoursWorked; open(DATA, "/host/n/b/p/p/o/r/nbpdexplorers.port5.com/cgi-bin/data2.tx +t") or die "Cannot open database file: $!"; while (<DATA>) { chomp($_); @data = split(/\+/, $_); $month = shift(@data); $hours = pop(@data); $ttlevnts++; foreach $name (@data) { $hoursWorked{$month}{$name} += $hours; } } close(DATA); for my $month1 (keys %ttlhrs) { $total{$_} += $ttlhrs{$month1}{$_} for keys %{$ttlhrs{$month1}}; } print $q->start_html( -title => "Hours Worked Status", -bgcolor => "wh +ite", -base => true, -xbase => "http://nbpdexplorers.port5.com/" ), $ +q->br; print "<center>"; print $q->h1("Hours Worked Status"), $q->br, $q->h3("Today's Date: $da +te1"), $q->br, $q->h3("Total Event(s): $ttlevnts"); print "<table border>\n"; print "<colgroup align=\"center\" span=\"27\">\n"; print "<tr><td></td><td><th>January</td><td><th>February</td><td><th>M +arch</td><td><th>April</td><td><th>May</td><td><th>June</td><td><th>J +uly</td><td><th>August</td><td><th>September</td><td><th>October</td> +<td><th>November</td><td><th>December</td><td><th><b>Total Hours</b>< +/td></tr></colgroup><colgroup align=\"left\">\n"; foreach $member (@members) { $member1 = ucfirst($member);
print "<tr><td><th>$member1<td>$hoursWorked{january}{$member}<td><td>$hoursWorked{february}{$member}<td><td>$hoursWorked{march}{$member}<td><td>$hoursWorked{april}{$member}<td><td>$hoursWorked{may}{$member}<td><td>$hoursWorked{june}{$member}<td><td>$hoursWorked{july}{$member}<td><td>$hoursWorked{august}{$member}<td><td>$hoursWorked{september}{$member}<td><td>$hoursWorked{october}{$member}<td><td>$hoursWorked{november}{$member}<td><td>$hoursWorked{december}{$member}<td><td></tr>\n";
} print "</table><center><p><a href=\"http://nbpdexplorers\.port5\.com/c +gi-bin/mainpage.cgi\?name=$name1\"><h3>Back To Administrative Options +</h3></a>\n"; print "</p></center></body></html> \n";

20021119 Edit by Corion : Added READMORE tag

Replies are listed 'Best First'.
Re: use strict again
by hardburn (Abbot) on Nov 19, 2002 at 01:48 UTC

    Heeeerrre, Docs! Heeerrrreeeee, Docs! Come here boy! Heeeerrreee, Docs!

    print<<END; <tr> <td>Stuff</td> <td>Formatted</td> <td>Nicely</td> </tr> END
Re: use strict again
by DamnDirtyApe (Curate) on Nov 19, 2002 at 07:04 UTC
    I took damndirtyape and co.'s advice,

    Ah, but you missed so much! :-)

    • You missed the cue for HERE docs or templates. This will make you code way more legible. Also, if you structure your HTML a little more legibly, you'll have a much easier time fixing it. You've got tags that don't close, tags where they shouldn't be... you need to clean this up.
    • Don't bother declaring your variables until you use them. Otherwise, readers are left hanging in the wind as to why your using a given variable.
    • Simplify your date operations with strftime() from POSIX. Don't declare a bunch of intermediate variables just to compose a string you can do much better in one step.
    • Your $ttlhrs declaration is redundant. Iterate over the @members array when you're populating your hashref. It's less code and more scalability.
    • Take a look at perlstyle for some issues regarding code format.

    _______________
    DamnDirtyApe
    Those who know that they are profound strive for clarity. Those who
    would like to seem profound to the crowd strive for obscurity.
                --Friedrich Nietzsche
Re: use strict again
by pg (Canon) on Nov 19, 2002 at 01:57 UTC
    Check the Data::Dump module, and it comes quite handy when you want to print out some complex data structure. It works great, so you don't need to copy and paste from time to time, to make up those "display" method.
Re: use strict again
by tadman (Prior) on Nov 19, 2002 at 02:30 UTC
    You can always send your variables through the wash with a function like this:
    sub default { foreach (@_) { $_ = '' unless (defined($_)); } }
    So, in practice, this is used in a straight-forward way:
    my $aaa = 'bar'; my $bbb = 0; my $ccc = undef; my %ddd = ( eee => undef, fff => 1 ); my @iii = (undef, 1, undef, 2); default($aaa,$bbb,$ccc,%ddd,@iii); print "aaa=$aaa\n"; print "bbb=$bbb\n"; print "ccc=$ccc\n"; print "ddd=eee($ddd{eee}) fff($ddd{fff})\n"; print "iii=@iii\n";
    No warnings because everything that was undef is now set to an empty string.

    You could always substitute 0 instead of '' in the default routine.
Re: use strict again
by graff (Chancellor) on Nov 19, 2002 at 02:48 UTC
    I took damndirtyape and co.'s advice, and I was able to fix the problems. Now I added more and have the same problem

    Well, maybe you forgot one piece of advice: this is something that you can -- should -- ALWAYS do at the command line prompt before trying anything via a browser:

    perl -cw your_cgi_script
    This will show you whether there are any compilation problems in your current script, and if so, what they are and where they are. Refer to the line numbers listed, fix the problems mentioned, and repeat the cycle until "perl -cw" reports "OKAY". So, given the version of the script you've posted this time, here is the list:
    Global symbol "%total" requires explicit package name at test.perl li +ne 50. Bareword "true" not allowed while "strict subs" in use at test.perl l +ine 53. test.perl had compilation errors.
    Not as bad as the last time, but there is a familiar item here...
Re: use strict again
by Anonymous Monk on Nov 19, 2002 at 04:09 UTC
    While I know that using the command line debugger is the best thing, I don't have access to this with my server not allowing telnet, the browser is the only resource I have.
      You mean you don't have your own computer, where you can install perl locally? You just need to have the perl interpreter on your local machine in order to check any script you write for syntax errors and warnings (perl -cw script.pl), before uploading that script to some remote web server.

      You can install Perl on any common personal computer for free. Do it, and you'll be glad you did.