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

i can't figure out why save_param from a session object can't seem to save keys that existed before with a undef value. a simple test shows here : (u can see that my name is gone only after assigning an empty string when saving)
#test1.pl #!/usr/bin/perl use strict; use warnings; use CGI; use CGI::Session qw/-ip-match/; # instantiate a new CGI object my $cgi = new CGI; my $session = new CGI::Session(); my $sessionid = $session->id(); $cgi->param("myname", "max payne"); #save param into session $session->save_param($cgi); print "after saving, it's actually : "; print $session->param("myname"); print $cgi->redirect( -URL => 'test2.pl?sessionid='.$sessionid);
#test2.pl #!/usr/bin/perl use strict; use warnings; use CGI; use CGI::Session qw/-ip-match/; require "library.pl"; # instantiate a new CGI object my $cgi = new CGI; my $sessionid = $cgi->param("sessionid"); # MARKING 1 my $session = CGI::Session->load($sessionid); print "what was my name again >? " . $session->param("myname"); $cgi->param("myname", undef); $session->save_param($cgi); print "<BR>(as undef) what was my name again >? " . $session->para +m("myname"); $cgi->param("myname", ""); $session->save_param($cgi); print "<BR>(as empty string) what was my name again >? " . $sessio +n->param("myname");
it actually outputs :
what was my name again >? max payne
(as undef) what was my name again >? max payne
(as empty string) what was my name again >?

Replies are listed 'Best First'.
Re: session save_param can't save undefined value
by locked_user sundialsvc4 (Abbot) on Feb 28, 2008 at 03:35 UTC

    This behavior does not surprise me too much, since undef can be thought of as “a value is not there at all.”

    In other words, undef is not the same as “null” or “empty string.” It's not like a null-value (e.g. in a database), where a definitive value has been provided even though that value is “the absence of a value.” Rather, this is indistinguishable from the case where “the question has never been answered, period.” What is a session-data-store supposed to do with something that “does not exist?” A software-designer's quandary, at best... and the designer's choice here is quite defensible.

    Perhaps you can be more specific with your question? That's quite a lot of code to wade through, when it's hard to see just what you're getting at... Can you provide a little more guidance to the Monks who are anxious to help you?