On 8/30/06, Mary Anderson <[EMAIL PROTECTED]> wrote:

Hi All,
   I know this isn't strictly a cgi problem, but it is arising in a cgi
application.  I have a loop which reads certain fields, hashed on names.
Some of my fields hold character strings, some hold numbers.  Sometimes
the number field is a blank.  I need a test on the field value $fieldValue
which will tell me if my field was blank regardless of whether it holds a
character string or a number.

      I would like to say something like

  $fieldValue = (($fieldValue == 0) or $fieldValue) ? $fieldValue : 'null'

but perl appears to have a strange interpretation of $fieldValue == 0 if
$fieldValue is a character.

Thanks
Mary

     I have seen references to a function which will do the trick, but it
is not mentioned in the camel book.


Well, fisrt of all, you need to decide what you mean by "null," and
"blank." In most cases they aren't the same thing. "Null" normally
means a string containing the value '\0'. This is especially true if
you are interacting with anything that has C code in it. Perl
programmers, for the most part, are interested in whether a particular
value is defined, in which case you can use the defined() function,
e.g.:

   next unless defined($var);

When working with hashes, exists() is also useful, perldoc -f defined,
perldoc -f exists, and perldoc -f undef for more info.

in many contexts, a simple test for boolean truth suffices as well, e.g.:

   next unless $var;

But the catch there is that '0', '', and undef will all fail, so you
can get into trouble if '0' is a legal value.

Finally, none of these will catch a blank, that is, a sting composed
entirely of whitespace. to handle that, you need a regex, something
like

  next if $var =~ /^\s*$/;

HTH,

-- jay
--------------------------------------------------
This email and attachment(s): [  ] blogable; [ x ] ask first; [  ]
private and confidential

daggerquill [at] gmail [dot] com
http://www.tuaw.com  http://www.downloadsquad.com  http://www.engatiki.org

values of β will give rise to dom!

Reply via email to