in reply to Storing an array in a cookie...

Submitted for the perusal of the monestary:

Here is the code for the cgi program. Now, at the line separated by asterisks from the rest of the code, I have an error. My httpd error log gives the following explanation:

"Can't use an undefined value as an ARRAY reference at /var/www/cgi-bin/perl/distance-test.cgi line 39."

Ovid was kind enough to show me an explanation here once, but when I attempted to implement his suggestion, I am evidently short some info. I guess I need help using "thaw".

#! /usr/bin/perl -w use strict; use Storable qw/freeze thaw/; use CGI ':standard'; my $source; my $destination; my $oldtotal; my $miles; my $date; my $tripdate; my $tripcounter; my $tablesize; my @table; #master array to carry data from previous iterat +ions #my @newdata; #new array to push array-style data into @table #my @tablereform; #new array to take care of spaces in items in @t +able #my %spaghetti; #to remind myself that my code is horribly convo +luted #my %sauce; #to keep spaghetti company #my $i; #for the "for" loop that creates the table my $e; #for the "for" loop that controls the number of e +lements in @table my $m; #new variable to play with in @table assignments my $n; #ditto as $m my $cookiedata; #outgoing formatted table data comma delimited my $cookiepickup; #incoming cookie data # Now we get our info from the HTML parameters $source = param('source'); $destination = param('destination'); $oldtotal = param('oldtotal'); $tripdate = param('tripdate'); $tripcounter = param('tripcounter'); $cookiepickup = cookie('trip_history'); ********************************************* @table = @{ thaw $cookiepickup }; #this is the problem. ********************************************* # Now we set some controller variables $tablesize = 1; $e = 1; $m = -1; $n = $tripcounter; # Here is the distance database. Can I do this better? if ($source eq 'HHS') { if ($destination eq 'District') { $miles = 6.7} if ($destination eq 'GHS') { $miles = 5} if ($destination eq 'MHS') { $miles = 8.3} } if ($source eq 'GHS') { if ($destination eq 'HHS') { $miles = 5} if ($destination eq 'District') { $miles = 1.7} if ($destination eq 'MHS') { $miles = 3.3} } if ($source eq 'MHS') { if ($destination eq 'District') { $miles = 1.6} if ($destination eq 'GHS') { $miles = 3.3} if ($destination eq 'HHS') { $miles = 8.3} } if ($source eq 'District') { if ($destination eq 'MHS') { $miles = 1.6} if ($destination eq 'GHS') { $miles = 1.7} if ($destination eq 'HHS') { $miles = 6.7} } if ($source ne $destination) { $oldtotal = $miles + $oldtotal; $tripcounter = $tripcounter + 1; push (@table, $tripdate, $source, $destination, $miles); $cookiedata = freeze \@table; # push(@table, @newdata); print qq (Set-cookie:trip_history=$cookiedata \n); print qq (Content-type: text/html\n\n <html><title>Mileage</title>); print qq (<body><form action="http://10.1.1.205/cgi-bin/perl/distanc +e-test.cgi" method="Post">); print qq (Trip Number $tripcounter <input type="hidden" name="tripco +unter" value="$tripcounter">----------); print qq (Trip Date: <select name="tripdate"><option value="1">1</op +tion><option value="2">2</option>); print qq (<option value="3">3</option> <option value="4">4</option> + <option value="5">5</option>); print qq (<option value="6">6</option> <option value="7">7</option> + <option value="8">8</option>); print qq (<option value="9">9</option> <option value="10">10</optio +n> <option value="11">11</option>); print qq (<option value="12">12</option> <option value="13">13</opt +ion> <option value="14">14</option>); print qq (<option value="15">15</option> <option value="16">16</opt +ion> <option value="17">17</option>); print qq (<option value="18">18</option> <option value="19">19</opt +ion> <option value="20">20</option>); print qq (<option value="21">21</option> <option value="22">22</opt +ion> <option value="23">23</option>); print qq (<option value="24">24</option> <option value="25">25</opt +ion> <option value="26">26</option>); print qq (<option value="27">27</option> <option value="28">28</opt +ion> <option value="29">29</option>); print qq (<option value="30">30</option> <option value="31">31</opt +ion></select><hr>); print qq (Starting Point:<select name="source"><option value="HHS"> +Highland High School </option>); print qq (<option value="MHS"> Mesquite High School </option><option + value="GHS"> Gilbert High School </option>); print qq (<option value="District"> District Office</option></select +>); print qq (Ending Point:<select name="destination"><option value="HHS +"> Highland High School </option>); print qq (<option value="MHS"> Mesquite High School </option><option + value="GHS"> Gilbert High School </option>); print qq (<option value="District"> District Office </option></selec +t>); print qq (<br><hr>); print qq (Current mileage is <Input Type=text name=oldtotal value=$o +ldtotal> miles<br>); print qq (<input type="submit" value="Get Distance"><input type="res +et" value="Oops! Reset Please!"></form>); # Retrieve Date &get_date; sub get_date { my @days; my @months; my $sec; my $min; my $hour; my $mday; my $mon; my $year; my $wday; my $time; # Define arrays for the day of the week and month of the year. @days = ('Sunday','Monday','Tuesday','Wednesday', 'Thursday','Friday','Saturday'); @months = ('January','February','March','April','May','June','July +', 'August','September','October','November','December') +; # Get the current time and format the hour, minutes and seconds. +Add # 1900 to the year to get the full 4 digit year. ($sec,$min,$hour,$mday,$mon,$year,$wday) = (localtime(time))[0,1,2 +,3,4,5,6]; $time = sprintf("%02d:%02d:%02d",$hour,$min,$sec); $year += 1900; # Format the date. $date = "$days[$wday], $months[$mon] $mday, $year"; } print qq (<p><hr>$date); print qq (</body></html>); print qq (<br>cookie value is @table); }