in reply to defined vs null string

Reading perldoc -f defined will be illuminating.

defined EXPR defined Returns a Boolean value telling whether EXPR has a value other than the undefined value "undef". If EXPR is not present, $_ is checked.

An interesting thing to notice, is that defined returns nothing if something is undef. But it prints a 1 if defined. Sort of an asymetric boolean. One might expect 0 to be printed, but that return value would be defined, and would screw up the defined function used in more complex logic.

#!/usr/bin/perl use warnings; use strict; print defined (undef) ,"\n"; # prints nothing but a newline my $y = undef; print defined ($y),"\n"; # prints nothing but a newline my $x = ''; print defined ($x), "\n"; # prints a 1

I'm not really a human, but I play one on earth.
Old Perl Programmer Haiku ................... flash japh

Replies are listed 'Best First'.
Re^2: defined vs null string
by jwkrahn (Abbot) on Aug 30, 2011 at 19:50 UTC
    An interesting thing to notice, is that defined returns nothing if something is undef. But it prints a 1 if defined.

    Perl functions/operators do not return "nothing".    defined returns either "TRUE" or "FALSE", which is 1 for true or "" for false.