I am currently a college student at Trident Technical College here in Charleston, SC. My instructor and another student were puzzled about and assignment and we do not understand the logic in how this script runs. This script works, as it is the answer to the assignment, but we are curious in the order the script runs to produce a cookie and retrieve it for a script. The assignment was to access a web page we created, click on an item, then the script would print a web page with the items purchased, and every time anothe ritem was clicked, it would add those results to the cookie and print on the web page. I and other students do not understand the order of how this runs because it makes sense to us that you would gahter the information, create a cookie, send the cookie to the browser, then retrieve the cookie to print. The text book we are using did not have any explanantion to this and the order in which it must be written. I have included the script below so that you can see how it does work...

#!/usr/bin/perl #c11ex6.cgi - displays items purchased on a web page use CGI qw(:standard); #prevent Perl from creating undeclared variables use strict; #declare variables my ($rec, @records, $C_records, $basket, @purchases, @items); @items = ("One Pint Square","One Quart Square","One Quart Oblong","Fou +r Quart Square","Four Quart Oblong"); #assign input to variable $basket = param('Basket'); #retrieving cookie @purchases = split(/ /, cookie('cookie')); #adding purchase to cookie push(@purchases, $basket); #create cookie $C_records = cookie(-name => "cookie", -value => "@purchases", -path => "/cgi-bin/c11ex6.cgi"); #send cookie to browser print header(-cookie => $C_records); #create Web page print "<HTML>\n"; print "<HEAD><TITLE>Wicker Baskets, Inc.</TITLE></HEAD>\n"; print "<BODY>\n"; print "<H1>Wicker Baskets, Inc.</H1><BR>\n"; print "<H2>Your purchases are: </H2><BR>\n"; foreach my $rec (@purchases) { print "$items[$rec]<BR>\n"; } print "</FONT>\n"; print "</BODY></HTML>\n";

If you you put the steps in a logic fashion as we were trying to do, the script runs, but it will not add to the cookie and display the items purchased. My instructor asked if I could research this and let the class know why does the above script have to be put in the order it does to work properly...... Thank you, James

janitored by ybiC: Retitle from "JOT007" because onewordnodetitles hinder site search, also enclosed somewhat long-ish codeblock within balanced <readmore> tags as per Monastery convention


In reply to Question regarding CGI and cookies by JOT007

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.