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

I have this code from perldoc CGI::Cookies
#!/usr/bin/perl -w use strict; use diagnostics; use CGI qw/:standard/; use CGI::Cookie; # Create new cookies and send them my $cookie1 = new CGI::Cookie(-name=>'ID',-value=>123456); my $cookie2 = new CGI::Cookie(-name=>'preferences', -value=>{ font => 'Helvetica', size => 12 } ); print header(-cookie=>[$cookie1,$cookie2]); print start_html; # fetch existing cookies my %cookies = fetch CGI::Cookie; my $id = $cookies{'ID'}->value; print $id; print end_html;
but when I run the code I get the following error. I know what it means - I just dont understand why Im getting it.
print $id;Set-Cookie: ID=123456; path=/ Set-Cookie: preferences=font&Helvetica&size&12; path=/ Date: Sat, 19 Mar 2005 04:19:17 GMT Content-Type: text/html; charset=ISO-8859-1 <?xml version="1.0" encoding="iso-8859-1"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-U +S"><head><title>Untitled Document</title> Can't call method "value" on an undefined value at ./index.cgi line 17 + (#1) (F) You used the syntax of a method call, but the slot filled by t +he object reference or package name contains an undefined value. Som +ething like this will reproduce the error: $BADREF = undef; process $BADREF 1,2,3; $BADREF->process(1,2,3); Uncaught exception from user code: Can't call method "value" on an undefined value at ./index.cgi + line 17. at ./index.cgi line 17

Much thanks in advance Ted

Replies are listed 'Best First'.
Re: setting cookies
by chromatic (Archbishop) on Mar 19, 2005 at 06:35 UTC

    You can't set and fetch the same cookie in the same request. Unless you've previously set the cookies correctly (and I don't believe you have, because I think the error message will hit the browser before the headers, though I could be wrong about this if you're using a weird HTTP server I've not seen before but take it as a rule of thumb anyway), $cookies{ID} will be empty.

    Even if you have set the cookie successfully, you have no control over what the client does with it. You have to check that you've retrieved a cookie successfully and you have to be ready to do something sensible if you haven't.

    if (exists $cookies{ID}) { my $id = $cookies{ID}->value(); # do something here }
Re: setting cookies
by Tanktalus (Canon) on Mar 19, 2005 at 04:42 UTC

    First, it would be really handy if you'd point out line 17. ;-)

    Unless I miscounted something, line 17 is the line that says:

    my $id = $cookies{'ID'}->value();
    To me, that means that the 'ID' key doesn't exist in the %cookies hash. Try dumping the hash as a debug step to see what cookies CGI::Cookie::fetch thinks are there. I would bet that 'ID' isn't one of them. Especially if you're testing this on the commandline (although it's perfectly feasible that it doesn't exist when it's out in production, say if a user has cookies disabled).