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

This is my second day of perl, so please be easy on me.:) I have this:
open(SESSIONS, "sessions"); $id = new Math::BigInt '123 456 789 123 456 789'; $c = new CGI::Cookie(-name => 'NetAppliance1.0', -value => $id, -path => '/cgi-bin/' -secure => 0 ); # finished. let's jam it out there print "Set-Cookie: $c\n"; print "Content-Type: text/html\n\n"; print SESSIONS "ID:$id";
In the sessions file, I have:
ID:+123456789123456789
but on STDOUT I have:
Set-Cookie: NetAppliance1.0= Content-Type: text/html
What am I doing wrong? Thanks.

Replies are listed 'Best First'.
Re: Printing BigInts
by ahunter (Monk) on Jul 12, 2000 at 18:56 UTC
    Math::BigInt returns references to blessed scalars, and uses overload to cause it to be stringified when you refer to it in a string. You put the reference directly into the argument block used by CGI::Cookie, and CGI::Cookie doesn't know what to do with it. Simple solution: force it to be stringified:
    $c = new CGI::Cookie(-name => 'NetAppliance1.0', -value => "$id", -path => '/cgi-bin/' -secure => 0 );
    Data::Dumper is often your friend in these circumstances:
    use Data::Dumper; print Dumper({-value => $id});
    produces:
    $VAR1 = { '-value' => bless(do {\(my $o= '+123456789123456789')}, 'Mat +h::BigInt') };
    Which is a good indication that things are not as you thought :-)

    Andrew. (You should really be using CGI.pm throughout)

      what do you mean by CGI.pm throughout? I did this :
      use CGI qw/:standard/; use CGI::Cookie;
        I think he means: print header(-cookie => $c); instead of your two print lines. Let CGI.pm take care of the gory details for you!
(jjhorner)Printing BigInts
by jjhorner (Hermit) on Jul 12, 2000 at 18:49 UTC

    Perhaps this code will help:

    #!/usr/bin/perl -w # ic_cookies - sample CGI script that uses a cookie use CGI qw(:standard); use strict; my $cookname = "favorite ice cream"; my $favorite = param("flavor"); my $tasty = cookie($cookname) || 'mint'; unless ($favorite) { print header(), start_html("Ice Cookies"), h1("Hello Ice Cream"), hr(), start_form(), p("Please select a flavor: ", textfield("flavor",$tasty)), end_form(), hr(); exit; } my $cookie = cookie( -NAME => $cookname, -VALUE => $favorite, -EXPIRES => "+2y", ); print header(-COOKIE => $cookie), start_html("Ice Cookies, #2"), h1("Hello Ice Cream"), p("You chose as your favorite flavor `$favorite'.");

    This comes from Recipe 19.10 in the Perl Cookbook.

    Discussion

    The cookie was prepared with:

    my $cookie = cookie( -NAME => $cookname, -VALUE => $favorite, -EXPIRES => "+2y", );

    And the cookie was set with:

    print header(-COOKIE => $cookie), start_html("Ice Cookies, #2"), h1("Hello Ice Cream"), p("You chose as your favorite flavor `$favorite'.");

    I hope this helps.

    UPDATE: Oh Boy! I missed the boat on this one. Please ignore this message!!

    J. J. Horner
    Linux, Perl, Apache, Stronghold, Unix
    jhorner@knoxlug.org http://www.knoxlug.org/