develooper Front page | perl.perl5.porters | Postings from June 2003

Re: perlbool.pod

Thread Previous | Thread Next
Steve Grazzini
June 29, 2003 13:25
Re: perlbool.pod
Message ID:
On Sun, Jun 29, 2003 at 09:12:53PM +0900, Dan Kogai wrote:
>   ()           # scalar () is 0 so false
>   (0)          # scalar (0) is 1 so true!

Hmm ?

> =head2 Boolean values of arrays and hashes
> For boolean values of arrays and hashes, perl just uses scalar()
> thereof.  In other words, boolean values of arrays and hashes are
> false when and only when they are empty.
> C<()> is false but C<(0)> and C<("")> are true.

Those aren't arrays, so they don't make good examples.

    @array = 0;   # @array is true, since scalar(@array) == 1

> =head1 Boolean Context vs. Boolean Value
> Though perl does not have explicit boolean value like C<t> or C<nil> in
> LISP and ruby, perl does have boolean context.

Or indeed C<true> and C<false> in Ruby.  But I don't think we
need the comparisons to get the point.

>   $a;         # void context
>   $a+1        # scalar context
>   push @a, $a # array context

"list context"

>   if ($a)     # boolean context

But this is still scalar context.  I couldn't think of a more 
precise term either, but we're talking about the different ways
$a can be evaluated:

    $a + $b    # numeric context
    $a . $b    # string context
    $a or $b   # boolean context


Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About