Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

testing for the zero length string (blank value) in cgi fields

by scratch (Sexton)
on Oct 24, 2001 at 17:41 UTC ( #121104=perlquestion: print w/replies, xml ) Need Help??

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

Hello all,

I'm using to collect some user info through a series of web pages. I've been just writing the captured data out to a file, but now I'm trying to write to a database. For the most part this is working well, but for a few fields I'm trying to test for a value of 'blank' or 'no data' (this would be for fields left unanswered). By default perl seems to convert these to a value of 0, but I'd like to convert them to -1.

My problem is in testing for this blank value. I've tried lots of things, all of them more or less like this:

@negs = qw ( foo bar baz ); foreach $neg (@negs) { if (param('$neg') eq "") { param(-name=>"$neg", -value=>-1) ; } }

This, however, replaces *all* values of foo, bar, and baz with -1, not just the blank values.

Is there a way I can test for the blank value? Is there a better way to achieve my goal of not allowing fields to be left blank by users, but recording a value of -1 in the database for these fields?

Thanks as always,


Replies are listed 'Best First'.
Re: testing for the zero length string (blank value) in cgi fields
by Masem (Monsignor) on Oct 24, 2001 at 17:49 UTC
    This line:
    if (param('$neg') eq "")
    is checking the param named "'$neg'"; that is, there is no expansion of the variable going on with single quotes, so you're not checking what you think you are checking.

    Ditch the quotes:

    if (param($neg) eq "")
    and it should work.

    Dr. Michael K. Neylon - || "You've left the lens cap of your mind on again, Pinky" - The Brain
    "I can see my house from here!"
    It's not what you know, but knowing how to find it if you don't know that's important

Re: testing for the zero length string (blank value) in cgi fields
by davorg (Chancellor) on Oct 24, 2001 at 18:35 UTC

    A number of people have pointed out your problems with quotes, but no-one has yet simplified your test. If you change it to

    if (param($neg)) {

    then you'll cover the case where the value isn't set, but also the case where the input doesn't exist.

    Update: blakem is absolutely right!


    "The first rule of Perl club is you don't talk about Perl club."

      That's inverted... I think you mean:
      unless (param($neg)) { # or if (!param($neg)) {


Re: testing for the zero length string (blank value) in cgi fields
by jj808 (Hermit) on Oct 24, 2001 at 17:52 UTC
    I think you may have a problem with your quotes. The line
    if (param('$neg') eq "") {
    will not interpolate the value of $neg, so unless you have a parameter called '$neg' (with the dollar symbol) then this will always evaluate to true, hence trashing the values of your other parameters.

    Try it like this instead:

    if (param($neg) eq "") {


    Update: Must learn to type as quickly as Masem :-)

Re: testing for the zero length string (blank value) in cgi fields
by tommyw (Hermit) on Oct 24, 2001 at 17:52 UTC

    Ahem! You're testing param('$neg') (ie a parameter with the four character name $neg), but resetting the param(-name=>"$neg") (ie. one of foo, bar or baz).

    Change the quotes and the problem goes away.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://121104]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2023-03-27 19:54 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (65 votes). Check out past polls.