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

I have a cgi script where I need to discern a reply of zero vs. no reply, a text box with zero as a response and retrieved using cgi.pm, appears the same as no response (left blank), can I do this server side or will I have to use javascript?

Within the same script I use ConfigReader::Simple, looking at the code for that module it is just beyond my perlish grasp, I am trying to pass an array of items into a single key, is the most straightforward way (I almost said _only way_) to do this passing a list of items such as
key "1,2,3"
then split it? (which seems very unperlish)

g_White

Replies are listed 'Best First'.
(jeffa) Re: zero vs. empty
by jeffa (Bishop) on Mar 03, 2002 at 21:40 UTC
    First question: simply test for the equality of the string '0', for example:
    use strict; use CGI qw(:standard); my $foo = param('foo'); if ($foo) { print "true value\n"; } elsif ($foo eq 0) { print "zero value\n"; } else { print "undef value\n"; }
    If you have warnings turned on, then an 'unitialized value use' error will be generated. Don't be tempted to 'default' the param with the empty string:
    my $foo = param('foo') || '';
    This will prevent the warning, but if the user sends a zero, $foo will instead contain the empty string.

    Second question, I really don't know. I can tell you that trying to understand how to use a module is best done by reading the documentation and studying any example files that are bundled in the tar ball, and not necessarily by actually trying to read the code itself. ConfigReader::Simple does have an example.pl script file and an example.config config file. It might be possible that this module cannot handle arrays like you wish. You should check out the more powerful Config::General.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: zero vs. empty
by Trimbach (Curate) on Mar 03, 2002 at 21:36 UTC
    To address the first part of your question, it looks like you're making the mistake of confusing definedness with emptiness. Something is defined if it has a value. Something is true if the value meets Perl's definition of true.(i.e., anything that isn't zero or the empty string)

    In your case, if you need to differentiate a value of zero from no entry at all, you can do this:

    my $value = param(value); print "No answer" if ($value eq ""); print "There's an answer, but it's zero" if ($value eq 0);

    Gary Blackburn
    Trained Killer

      Something is true if the value meets Perl's definition of true.(i.e., anything that isn't zero or the empty string)

      undef is false too.

      ++ vs lbh qrpbqrq guvf hfvat n ge va Crey :)
      Nabgure bar vs lbh qvq fb jvgubhg ernqvat n znahny svefg.
      -- vs lbh hfrq OFQ pnrfne ;)
          - Whreq
      

        Well, yes, but that's just an extension of what I said, since any undefined value always evaluates either to zero or the null string. So speaketh the Camel, 3rd ed., pg. 30. :-D

        Gary Blackburn
        Trained Killer

Re: zero vs. empty
by screamingeagle (Curate) on Mar 03, 2002 at 21:40 UTC
    you could check if the value returned from CGI->param('fieldname') is empty by using the length function
    if length($cgi->param('fieldname')) == 0 (that means the field is empt +y.)
    To check if the value is 0, how about :
    if ($CGI->param('fieldname') == 0)...
    Option 2: if u want to use Javascript, then this might work:
    <html <head> <script language="Javascript"> function chkinput(objForm) { if (objForm.inputfield.value == "") { objForm.fld_has_value_or_not.value = "N" } else { objForm.fld_has_value_or_not.value = "Y" } objForm.submit(); return true; } </script> </head> <body> <form name="testfrm" method="post" action="cgifile.cgi" onSubmit="retu +rn chkinput(this);"> <input type=hidden name=fld_has_value_or_not value=""> <input type="text" name="inputfield" value=""> <input type=submit value="Submit"> </form> </body> </html>
    in this case, if the input field is empty, the hidden variable will be set to "Y", which can be checked by the cgi file and processed accordingly...
    hth...

      To check if the value is 0, how about :   if ($CGI->param('fieldname') == 0)...

      Keep in mind that "three" == 0. To check if the entered value equals the single character 0, use a string comparison: $something eq '0'.
      It's generally a good idea to warn if the user didn't enter nothing or digits. Or, using reversed logic: warn if a non-digit character is found:

      die "\$foo contains a non-digit character!\n" if $foo =~ /\D/;

      ++ vs lbh qrpbqrq guvf hfvat n ge va Crey :)
      Nabgure bar vs lbh qvq fb jvgubhg ernqvat n znahny svefg.
      -- vs lbh hfrq OFQ pnrfne ;)
          - Whreq