NULL and UNDEF

Discussion in 'Perl Misc' started by John, Dec 22, 2006.

  1. John

    John Guest

    Hi

    Is it the case, in Perl, that both the following

    if ($x eq undef) and if ($x eq '')

    will give identical results?

    If so, how can you tell if the $x is actually null or undefined?

    Regards
    John
    John, Dec 22, 2006
    #1
    1. Advertising

  2. John wrote:
    > Is it the case, in Perl, that both the following
    >
    > if ($x eq undef)


    Somehow I doubt that even works. I would expect perl to throw an error.

    > and if ($x eq '')
    >
    > will give identical results?


    What happened when you tried it?

    > If so, how can you tell if the $x is actually null or undefined?


    perldoc -f defined
    perldoc -f length (if you want an alternative to eq '')

    jue
    Jürgen Exner, Dec 22, 2006
    #2
    1. Advertising

  3. John

    Paul Lalli Guest

    Jürgen Exner wrote:
    > John wrote:
    > > Is it the case, in Perl, that both the following
    > >
    > > if ($x eq undef)

    >
    > Somehow I doubt that even works. I would expect perl to throw an error.


    It "works" fine. No errors. Just warnings. At least one warning,
    because your using undef in a string context. If $x actually is
    undefined, then you're using another undef in string context, so you
    get a second warning:

    $ perl -wle'
    my $x;
    if ($x eq undef) {
    print "Yes";
    } else {
    print "No";
    }
    '
    Use of uninitialized value in string eq at -e line 3.
    Use of uninitialized value in string eq at -e line 3.
    Yes

    $ perl -wle'
    my $x = q{};
    if ($x eq undef) {
    print "Yes";
    } else {
    print "No";
    }
    '
    Use of uninitialized value in string eq at -e line 3.
    Yes

    $ perl -wle'
    my $x = 0;
    if ($x eq undef) {
    print "Yes";
    } else {
    print "No";
    }
    '
    Use of uninitialized value in string eq at -e line 3.
    No


    Paul Lalli
    Paul Lalli, Dec 22, 2006
    #3
  4. John

    Paul Lalli Guest

    John wrote:
    > Subject: NULL and UNDEF


    There is no such thing as "NULL" in Perl. The string containing no
    characters is referred to as either the empty string or the null
    string. The character with ASCII code 0 is referred to as the null
    byte. A pattern match which does not look for any characters is set to
    be a null pattern. But "NULL" by itself is meaningless.

    > Is it the case, in Perl, that both the following
    >
    > if ($x eq undef) and if ($x eq '')
    >
    > will give identical results?


    Functionally, yes. The eq operator imposes string context on both of
    its operands. The undefined value is treated as the empty string in
    string context, so the above are functionally equivalent(*). However,
    with warnings enabled, the former will give a warning about using undef
    in a string context for at least the undef constant. (If $x actually
    *is* undefined, it too will produce a warning, but that's the same with
    both expressions).

    > If so, how can you tell if the $x is actually null or undefined?


    Again, no such thing as "null".

    Check for undefined:
    if (!defined $x) { ... }
    Check for empty string:
    if ($x eq '') { ... }
    Check for null byte:
    if ($x eq '\0') { ... }

    Paul Lalli

    (*) Since undef in a numeric context is treated as 0, it also follows
    that these two will produce the same results, with the same caveat
    about the warning:

    if ($x == undef) { ... }
    if ($x == 0) { ... }
    Paul Lalli, Dec 22, 2006
    #4
  5. Paul Lalli <> wrote
    > Check for null byte:
    > if ($x eq '\0') { ... }


    Sure you didn't mean C<"\0"> instead of C<'\0'>?

    > perl -wle"print '\0'"

    \0

    gr.,
    Robert

    --
    The mind is its own place, and in itself,
    Can make a heaven of hell, a hell of heaven.
    -- John Milton, Paradise Lost
    Robert 'phaylon' Sedlacek, Dec 22, 2006
    #5
  6. John

    Paul Lalli Guest

    Robert 'phaylon' Sedlacek wrote:
    > Paul Lalli <> wrote
    > > Check for null byte:
    > > if ($x eq '\0') { ... }

    >
    > Sure you didn't mean C<"\0"> instead of C<'\0'>?
    >
    > > perl -wle"print '\0'"

    > \0


    Whoops! Thanks for the correction!

    Paul Lalli
    Paul Lalli, Dec 22, 2006
    #6
  7. John

    John Guest

    Hi

    Many thanks for those replies. Much appreciated,
    I guess I have fallen into the bad habit of using "eq undef".
    So, check not defined, then empty string then null byte (with double
    primes).
    I've tried this on some empty XML <city/> and it works well.

    Thanks again
    John
    John, Dec 23, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Vikas Yadav
    Replies:
    0
    Views:
    1,116
    Vikas Yadav
    Sep 9, 2003
  2. Replies:
    5
    Views:
    26,491
    Mike Schilling
    Mar 29, 2006
  3. karthikbalaguru
    Replies:
    3
    Views:
    2,996
    Chris Dollin
    Nov 27, 2008
  4. deepak p
    Replies:
    1
    Views:
    130
    Martien Verbruggen
    Jul 10, 2003
  5. Tim McDaniel

    undef($foo) versus $foo = undef()?

    Tim McDaniel, Aug 19, 2009, in forum: Perl Misc
    Replies:
    6
    Views:
    136
    Peter J. Holzer
    Aug 19, 2009
Loading...

Share This Page