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

Greetings, esteemed monks!

I'm embarrassed to admit I'm totally stumped by the problems I'm having so I'm airing it out in the hopes that your help can solve my problem.

So my goal is to take this script that I found on the Geohashing wiki (http://wiki.xkcd.com/geohashing/User:AeroIllini/GeoHistory) that will create a KML file to show the history of Geohash points for a given graticule. I am adapting it so that one can enter data and view the results online.

So I divided it into three steps:

  1. Data Entry form
  2. KML Generation Step
  3. Display results on Google Maps
The reason why I separated the second and third is that I'm using a redirect, which the documentation says can't be used if any other HTML has already been printed to the page. Anyway, I got the first and second part working, but when I added the last part, I started getting code 500 Server errors after submitting the data. Then I started trying to fix it and I started getting the Server Errors on the first page. Now I'm getting a blank page on initial load and I'm completely flummoxed and have been driving myself crazy while working on this for the past two days. This is only my second CGI script since laying off of Perl for several years so please be gentle--any advice and guidance is appreciated.

Without further ado, the code, which I'm nesting in a readmore tag:
#!/usr/bin/perl -w use strict; use Date::Calc qw(Add_Delta_Days Delta_Days); use Time::Local; use lib '/var/chroot/home/content/17/10541917/html/lib/Geo-Hashing-0.0 +6/lib'; use Geo::Hashing; use CGI qw(warningsToBrowser fatalsToBrowser); $\ = "\n"; my $t = localtime; my @u = split /\s/,$t; $t=join '-',@u; my $filename = "$t.kml"; #can also use $t=time; for number of seconds since the epoch. my $q = CGI->new; # create new CGI object if ((! $q->param('submit')) && (! $q->param('submitToGoogle'))){ print $q->header; print $q->start_html('Geohashing History'), # start the HTML h1('Welcome to the Geohashing History Calculator.'); print $q->start_form(); #Latitude print $q->p("Latitude for the graticule you're interested in:"); print $q->textfield(-name=>'lat', -value=>'39', -size=>3, -maxlength=>3); print $q->br; #Longitude print $q->p("Longitude for the graticule you're interested in:"); print $q->textfield(-name=>'lon', -value=>'-77', -size=>3, -maxlength=>3); print $q->br; #Begin date (must be after XXXX) print $q->p("Begin date of date range (in YYYY-MM-DD format)"); print $q->textfield(-name=>'begindate', -value=>'2013-03-21', -size=>10, -maxlength=>10); print $q->br; #End date # Grab the current date so we can set the default my ($a,$b,$c,$todayday,$todaymonth,$todayyear,$d,$e,$f) = localtim +e(time); # Do some interim math. $todayyear = $todayyear+1900; $todaymonth = $todaymonth+1; print $q->p("End date of date range (in YYYY-MM-DD format)"); print $q->textfield(-name=>'enddate', -value=>sprintf("$02d-%02d-%04d",$todayyear,$t +odaymonth,$todayday), -size=>10, -maxlength=>10); print $q->br; #Filename? #Submit button print $q->submit(-name=>'submit', -value=>'submit information'); print $q->end_form(); print $q->end_html; # end the HTML }elsif (param('submit')){ #print $q->header, # create the HTTP header # start_html('Geohashing History Results'), # start the HTML # h3('Geohashing History Results'); # level 1 header #Latitude #print $q->start_form; ##print $q->h3("You submitted the form."); #my $startdate=param('begindate'); #my ($year, $month, $day) = split /-/ , $startdate; #my ($startyear, $startmonth, $startday) = split /-/ , $startdate; #my $enddate=param('enddate'); #my ($endyear, $endmonth, $endday) = split /-/ , $enddate; #my $delta = Delta_Days(($startyear, $startmonth, $startday),($end +year,$endmonth,$endday)); # #print $q->h6("You have $delta days in your date range."); # #my $lat = param('lat'); #my $lon=param('lon'); ## Translate months for placemark titles. #my @months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); ## Output file header xml stuff # #open FH, "> $filename" || die "Can't open $filename: $!"; #print $q->FH "<?xml version=\"1.0\" encoding=\"UTF-8\"?> #<kml xmlns=\"http://www.google.com/earth/kml/2\"> #<Document> #<name>Historical Geohash Points for ",$lat,",",$lon,"</name> #"; # ## Main loop #for (my $i = 0; $i <= $delta; $i++) { # # # format date for Geo::Hashing # my $date = sprintf("%04d-%02d-%02d", $year, $month, $day) +; # # # magic happens! # my $geo = new Geo::Hashing(lat => $lat, lon => $lon, date + => $date); # # # output placemark xml # print $q->FH "<Placemark>"; # print $q->FH "<name>"; # print $q->FH $months[$month-1]," ",$day,", ",$year; # print $q->FH "</name>"; # print $q->FH "<description>",$geo->lat,", ",$geo->lon,"</ +description>"; # print $q->FH "<Point><coordinates>",$geo->lon,",",$geo->l +at,"</coordinates></Point>"; # print $q->FH "</Placemark>","\n"; # # # increment day and loop again! # ($year, $month, $day) = Add_Delta_Days($startyear, $start +month, $startday, $i); #} # ## finish up xml file #print $q->FH "</Document> #</kml>"; # #print $q->submit(-name=>'submitToGoogle', -value=>'View Results i +n Google Maps'); # #print $q->end_html; # end the HTML }elsif(param->('submitToGoogle')){ print $q->header; # create the HTTP header print $q->start_html('Redirect page'), # start the HTML h3('Redirect Page'); # level 1 header #print $q->redirect("https://maps.google.com/maps?q=http:%2F%2Fpqrs +.be%2Fmisc%2F$filename"); print $q->end_html; # end the HTML }else{ print $q->header; # create the HTTP header print $q->start_html("Shouldn't be here"), # start the HTML h3('You are here in error.'); #print $q->redirect("https://maps.google.com/maps?q=http:%2F%2Fpqrs +.be%2Fmisc%2F$filename"); print $q->end_html; # end the HTML }

Replies are listed 'Best First'.
Re: Flummoxed by CGI problems
by kcott (Archbishop) on Apr 25, 2013 at 06:26 UTC

    G'day OfficeLinebacker,

    Two lines immediately leapt out at me as being problematic:

    ... }elsif (param('submit')){ ... }elsif(param->('submitToGoogle')){ ...

    In both cases, I believe you want '...$q->param(...'. There's also four instances of using 'param(...)' instead of '$q->param(...)'. You don't import any functions (e.g. use CGI qw{:standard};) when you load CGI which suggests you want OO style. I suspect mixing OO and non-OO styles is causing you problems. CGI - PROGRAMMING STYLE explains the differences.

    Adding 'use warnings;' to your script may find other issues.

    -- Ken

Re: Flummoxed by CGI problems
by Anonymous Monk on Apr 25, 2013 at 03:39 UTC