cLive ;-) has asked for the wisdom of the Perl Monks concerning the following question:

Once again, more niggle than desperate, but...

Why does CGI.pm not store a null value in param() if one is sent?

eg,

my $q = new CGI; my %conf = ... hash defined elsewhere for ( $q->param() ) { $conf{$_} = $q->param($_); }
All well and good, but if the field sent contains an empty value, it doesn't appear when looping through param().

So for each conf var that *can* be empty, I'm having to add:

$conf{'var_name'} = $q->param('var_name');
explicitly.

Or am I missing some hidden feature of CGI.pm that covers this? It's just getting a little messy and umm i don't like that much...

.02 please :)

cLive ;-)

Replies are listed 'Best First'.
(crazyinsomniac) Re: CGI.pm and blank form fields
by crazyinsomniac (Prior) on Nov 28, 2001 at 12:50 UTC
    hmm, intersting results
    #!/usr/bin/perl -wT use strict; use CGI; my $q = new CGI; my %Q = $q->Vars(); if(exists $Q{a}) { print "a exists ", (defined $Q{a} )?("and it is"):("but it isn't"), " defined ($Q{a})\n"; } use Data::Dumper; print Dumper \%Q; __END__ F:\>perl -T f a=b a exists and it is defined (b) $VAR1 = { 'a' => 'b' }; F:\>perl -T f a= a exists and it is defined () $VAR1 = { 'a' => '' }; F:\>perl -T f a= a exists and it is defined () $VAR1 = { 'a' => '' }; F:\>perl -T f a $VAR1 = { 'keywords' => 'a' }; F:\>perl -T f a b c d $VAR1 = { 'keywords' => 'a b c d' }; F:\>perl -T f a=b;a=c;a=d; a exists and it is defined (b c d) $VAR1 = { 'a' => 'b c d' }; F:\>

     
    ___crazyinsomniac_______________________________________
    Disclaimer: Don't blame. It came from inside the void

    perl -e "$q=$_;map({chr unpack qq;H*;,$_}split(q;;,q*H*));print;$q/$q;"

Re: CGI.pm and blank form fields
by tachyon (Chancellor) on Nov 28, 2001 at 16:42 UTC

    There is a $NO_UNDEF_PARAMS var in new versions of CGI.pm (in 2.78, not in 2.74) but the default is 0 so this is not the problem. I presume you understand the difference between exists and defined with respect to hashes. Your results are not repeatable vis:

    use strict; use CGI; use Data::Dumper; my %fields; my $q = new CGI ( 'name=&value=&void=' ); print "got param '$_'\n" for $q->param; $fields{$_} = $q->param($_) for $q->param; print Dumper($q, \%fields); __END__ got param 'name' got param 'value' got param 'void' $VAR1 = bless( { 'void' => [ '' ], '.charset' => 'ISO-8859-1', '.parameters' => [ 'name', 'value', 'void' ], '.fieldnames' => {}, 'value' => [ '' ], 'name' => [ '' ] }, 'CGI' ); $VAR2 = { 'void' => '', 'value' => '', 'name' => '' };

    The .fieldnames should actually contain the fieldnames (this is really a bug) but the ultimate behaviour is not broken. I presume you have an old version of CGI.pm which does not have the .parameters part in the object and works off .fieldnames which was the behaviour pre 2.63? I think. Which version do you have?

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

      Version 2.753 - $NO_UNDEF_PARAMS is there.

      So, i'm beginning to think the vars aren't sent in the first place from IE. Hmmm.

      Ah well, don't worry. I really can't be bothered with it right now :-)

      cLive ;-)

Re: CGI.pm and blank form fields
by tstock (Curate) on Nov 28, 2001 at 12:07 UTC
    Are you sure the browser is sending name= in the query line ? I tried a bit of code and couldn't duplicate your example:

    use CGI; my $q= new CGI; print "content-type: text/plain\n\n"; if ($q->param()) { print "got param $_\n" for ($q->param()); } print "the end\n";


    This on CGI version '2.56', calling it as http://script?name= did print out "got param name".

    Tiago
Re: CGI.pm and blank form fields
by rob_au (Abbot) on Nov 28, 2001 at 15:18 UTC
    The following is the code that I use for this task and null fields are certainly incorporated into the hash (as determined by parsing of parameter values by HTML::FormValidator):

    my $cgi = CGI->new; my %param = map { $_ => $cgi->param($_) } $cgi->param();

     

    Ooohhh, Rob no beer function well without!