Hi Monks!

I am running Perl v5.10.0 (built for x86_64-linux-thread-multi) and I have an issue with the "defined" function. I am not sure this is actually a fault, but it have worked in previously releases (unfortunately I don't know which).

The issue seems to happen when I've got a defined variable without a value, e.g. $var1 = "".

If I need to check if (one of) two variables are defined, I need to do that like this:

if (defined $var1 || defined $var2) if (defined $var1 && defined $var2)

While I've previously been able to do it like this:

if (defined ($var1 || $var2)) if (defined ($var1 && $var2))

Running the following fails for both OR and AND (test 1 and 3):

./test.pl "" Test 1: NOK Test 2: OK Test 3: OK Test 4: NOK

When test.pl contains:

#!/usr/bin/perl use strict; use warnings; for my $i (1 .. 4) { my $result = (&successfulValidation ($i))? "OK": "NOK"; print "Test ", $i, ": ", $result, "\n"; } sub successfulValidation { my $test = shift; if ($test == 1) { return defined ($ARGV[0] || $ARGV[1]); } elsif ($test == 2) { return defined $ARGV[0] || defined $ARGV[1]; } elsif ($test == 3) { return defined ($ARGV[0] && $ARGV[1]); } elsif ($test == 4) { return defined $ARGV[0] && defined $ARGV[1]; } }

The following works just fine, though, so maybe the first (and only) variable to check can't be empty!?

./test.pl "" ""

Is it a bug that test 1 and test 3 fail? I mean, test 1 should have returned TRUE ($ARGV[0] was defined, although with an empty value) and test 3 should have returned "FALSE" ($ARGV[1] wasn't defined)? Maybe I am using "defined" in the wrong way!?

Many thanks in advance,

/Johan


In reply to "defined" function fails to evaluate expression; feature or fault? by w8lle

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.