SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Thread: Perl & Cookies

  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2009
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Perl & Cookies

    Hi.

    Im defining a variable in a foreach() loop. It calculates a total, based on values retrieved from a textfile.

    I want to store this variable called $grandTotal, in a cookie.

    The problem i'm having is that I cannot define a cookie after the "Content-type=text/html\n\n" line. I've tried defining the cookie at the start, but it just gives me the value 0 as there is no value assigned to it yet.

    Is there anyway i can update the variable at the top, with the result I have from the loop.

    The code is below.

    Code:
    #!/usr/bin/perl
    use CGI::Carp qw(fatalsToBrowser);
    
    $grandTotal = 0;
    
    $file = "DONATIONS.TXT"; # Locate the file
    open(HANDLE, $file) || die("Could not open the file."); # Open the file
    @data=<HANDLE>; # Add handler to an array
    close(HANDLE); # Close file handler
    
    print "Set-Cookie:totalDon=$grandTotal;";
    
    print "Content-type=text/html\n\n";
    print <<HTMLCODE;
    
    <font face='verdana'>
    <TABLE border=1>
    <TR>
    <TD><b>Display Name</TD>
    <TD><b>Date</TD>
    <TD><b>Comments</TD>
    <TD><b>Tax Bonus</TD>
    <TD><b>Amount</TD>
    </TR>
    
    HTMLCODE
    
    
    
    foreach $donation(@data)
    {  
    
      chomp($donation);
      ($name, $date, $total, $tax, $comment)=split(/\|/, $donation); # Split file where there is a pipe
    
      print "<TR>";
      print "<TD>$name</TD>";
      print "<TD>$date</TD>";
      print "<TD>$comment</TD>";
      print "<TD>$tax</TD>";
      print "<TD>$total</TD>";
    
      $grandTotal = $grandTotal + $total;
     
    }
    I want the value of $grandTotal from the loop, to update the value of $grandTotal at the top of the page.

    Thanks!

  2. #2
    SitePoint Wizard bronze trophy KevinR's Avatar
    Join Date
    Nov 2004
    Location
    Moon Base Alpha
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Then put the loop at the top of the page and use concatenation to buildup your table code, like so:

    Code:
    #!/usr/bin/perl
    use CGI::Carp qw(fatalsToBrowser);
    
    $grandTotal = 0;
    
    $file = "DONATIONS.TXT"; # Locate the file
    open(HANDLE, $file) || die("Could not open the file."); # Open the file
    @data=<HANDLE>; # Add handler to an array
    close(HANDLE); # Close file handler
    
    my $html;
    foreach $donation(@data)
    {  
    
      chomp($donation);
      my ($name, $date, $total, $tax, $comment)= split(/\|/, $donation); # Split file where there is a pipe
      $html .= "<TR>\n<TD>$name</TD>\n<TD>$date</TD>\n<TD>$comment</TD>\n<TD>$tax</TD>\n<TD>$total</TD>\n";
      $grandTotal += $total;
     
    }
    
    
    print "Set-Cookie:totalDon=$grandTotal;";
    
    print "Content-type: text/html\n\n";
    print qq{
    <font face='verdana'>
    <TABLE border=1>
    <TR>
    <TD><b>Display Name</TD>
    <TD><b>Date</TD>
    <TD><b>Comments</TD>
    <TD><b>Tax Bonus</TD>
    <TD><b>Amount</TD>
    </TR>
    $html
    </TR></TABLE>
    };
    Note I changed to "qq" instead of the heredoc, which is a much more modern quoting operator and much easier to use than heredocs. The Content-Type header was also incorrect, you had "=" where you should have ":".


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •