honestly, i havent even covered use::strict in my readings yet. i am brand
spanking new to the language, however i am indeed running perl with -w.

-cjm


On Tue, 5 Jun 2001, Hasanuddin Tamir wrote:

> On Mon, 4 Jun 2001, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote,
>
> > if ( $formdata{view_name} ne "" ) {
> >   $view = $formdata{view_name};
> >   $viewtag = "1";
> > }
> >
> > is there a special method for testing against a null string for a form
> > field's value? i am using the above data, but it seems to always return
> > with a value of "1" making me think that something is incorrect in my if
> > test. the form itself does not have anything default value specified for
> > view_name.
>
> Take a look at these one-liner examples.  The string "(nothing)" indicates
> that no output is printed.
>
> 1% perl -le '$x; print 1 if $x ne ""'
> (nothing)
>
> 2% perl -le '$x = undef; print 1 if $x ne ""'
> (nothing)
>
> 3% perl -wle '$x = undef; print 1 if $x ne ""'
> Use of uninitialized value at -e line 1.
>
> 4% perl -wle '$x = "i am here"; print 1 if $x ne ""'
> 1
>
> 5% perl -we '$x = undef; print "($x)\n";
> Use of uninitialized value at -e line 1.
> ()
>
> 6% perl -we '$x = ""; print "($x)\n";
> ()
>
>
> Number 2 is basically same with number 1 in that $x is undefined,
> except that $x variable in the number 2 is explicitly assigned.
> (Number 1 will also give you extra warning if you use -w).
> The line under number 3 is not an output of the code, it's a
> warning that will guide you what's wrong with the code.
>
> Undefined value is not the same as empty string (""), but both are
> evaluated to false.  If you use undefined variable anyway, it will be
> evaluated to empty (see no.5 and 6).  Empty field is sent as undefined
> value and when you test it against "" with 'ne' operator, it evaluates to
> true because they're not equal.
>
> You need the defined() function to test whether the variable contains
> some defined value, and it will cover both "" and 0 if the test returns
> true.
>
>     if (defined $var) {
>         ....
>     }
>
> If you need the value of the variable and you don't want "" then you
> need to test that too,
>
>     if (defined $var and $var ne "") {
>         ....
>     }
>
> or
>
>     if (defined $var and length $var) {
>         ....
>     }
>
> But if you rather want to test $var against true value, you can simply
> use,
>
>     if ($var) {
>         ....
>     }
>
>
> Btw, did you use -w switch and use strict in your script?
> You really should.  All those example codes above will bail out when
> you use strict.
>
>
> hth
> s.a.n
>

Reply via email to