Probably not up to merlyn's standards (lack of use strict for example), but her's something that works.

First time you run it as a cgi, it gives you a form to fill in. After you fill in and and submit, it comes back with "Your cookie is". If you look at your browser's cookies at this point, you'll see it. The third time you run it, it prints as above, but this time because it saw the cookie come back, the value you originally entered shows up.

#!/usr/bin/perl -Tw use CGI; use CGI::Cookie; my $q = CGI->new(); if ( $q->cookie('id') ) # cookie sent back by browser { print $q->header(-cookie=>$q->cookie('id')), $q->start_html, $q->h1('your cookie is ' . $q->cookie('id' +)), $q->end_html; } elsif ( $q->param('name') ) # form filled in by user { $c = CGI::Cookie->new(-name=>'id', -value=>$q->param('name') ); print $q->header(-cookie=>$c), $q->start_html, $q->h1('your cookie is ' . $q->cookie('id')), $q->end_html; } else # no cookie, nor form filled in { print $q->header(), $q->start_html, $q->start_form, "What's your name? ", $q->textfield('name'), $q->submit, $ +q->end_form, $q->end_html; }

--Bob Niederman, http://bob-n.com

In reply to Re: A rotten cookie by bobn
in thread A rotten cookie by sulfericacid

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.