in reply to Re^2: Do I need to use defined?
in thread Do I need to use defined?

In Perl, three things are false: the undefined value (undef), the empty string (''), and zero (0). Two of those things are defined.

Replies are listed 'Best First'.
Re^4: Do I need to use defined?
by afoken (Chancellor) on Sep 07, 2017 at 06:49 UTC

    Empty arrays and hashes are also false:

    >perl -E 'my @a=(); my @a2=@a or say "FALSE"' FALSE >perl -E 'my %h=(); my %h2=%h or say "FALSE"' FALSE >perl -E 'my @a=(0); my @a2=@a or say "FALSE"' >perl -E 'my %h=(""=>0); my %h2=%h or say "FALSE"' >

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
      Empty arrays and hashes become zero in scalar context, and zero is false.
        ...

        Read my posting again. I'm using list context in both cases.

        Or just read "Truth and Falsehood" in perlsyn:

        The number 0, the strings '0' and "" , the empty list () , and undef are all false in a boolean context. All other values are true. Negation of a true value by ! or not returns a special false value. When evaluated as a string it is treated as "" , but as a number, it is treated as 0. Most Perl operators that return true or false behave this way.

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)