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

This is my first time dealing with cookies in perl and I have read up on http://search.cpan.org/~lds/CGI.pm-3.33/CGI/Cookie.pm but am still having issues. Like it describes in the "Manipulating Cookies" section I am using
%Cookie = fetch CGI::Cookie; @LoginInfo = $Cookie{'Name'} -> value;
to try and get the values of the Cookie into the array but when I run the sript I get the error "Can't call method "value" on an undefined value" Now I do know that cooke of "name" has not been set on the computer yet. Would this be causing it? And if so how would I properly check to see if the cookie preexists or not.

Replies are listed 'Best First'.
Re: Cookies in Perl
by bart (Canon) on Feb 06, 2008 at 15:26 UTC
    I do know that cooke of "name" has not been set on the computer yet. Would this be causing it?
    Yes.
    And if so how would I properly check to see if the cookie preexists or not.
    If it exists then $Cookie{'Name'} would contain a cookie object, hence, a true value when tested in boolean context. So an easy fix is
    if ($Cookie{'Name'}) { @LoginInfo = $Cookie{'Name'}->value; }
    If @LoginInfo may contain some data before you call this, it's best to wipe it, either first or in an else block.

    p.s. In perl mids, mixed case for variable names is considered poor style. But it'll still work, of course.

      Thanks, I will mode the statment into an if check.
Re: Cookies in Perl
by Burak (Chaplain) on Feb 07, 2008 at 13:29 UTC
    Why don't you just use CGI::cookie() ? I never use CGI::Cookie directly
    use CGI qw(:standard); if ( @login_info = cookie('name') ) { # do something }