* D. Martin <designs at nolaflash.com> [2004/02/09 19:32]:
> I have a question about the nature of truth

Mmmm, college flashbacks.

>                                             (in TT2).

Oh.

>                                                       I am finding that
> empty arrays passed from my module always test true in my templates and
> that I have to use [% IF myArray.size > 0 %] rather than [% IF myArray
> %] to test whether to take action on array data.

TT uses Perl's concept of truth, which means (basically) taking the
variable in scalar context.  An arrayref (which is what a TT list is
under the hood) in scalar context is true:

  $ perl -wle '$a = [ ]; print(($a) ? "yes" : "no")'
  yes

  $ perl -wle '$a = [ ]; print((@$a) ? "yes" : "no")'
  no

You can do:

  [% IF myArray.size %]

which will return an integer.  Spefically, 0 for an empty array.

> Is this the intended behavior?

Yes.  Template::Directive, which generates the code for the IF
statement, simple takes the expression and puts it into Perl's if.

(darren)

-- 
I have discovered that all human evil comes from this, man's being
unable to sit still in a room.
    -- Blaise Pascal

Attachment: pgp00000.pgp
Description: PGP signature

Reply via email to