> -----Original Message-----
> From: D. Martin [mailto:[EMAIL PROTECTED] 
> Sent: Monday, February 09, 2004 8:32 PM
> To: [EMAIL PROTECTED]
> Subject: [Templates] The Nature of Truth (in TT2)
> 
> 
> Hello,
> 
> I have a question about the nature of truth (in TT2). 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. The following sub and
> template snippet demonstrated the problem.  Both empty_array and
> empty_ref test true in my template conditionals.
> 
> Is this the intended behavior?
> 
> TIA,
> 
> David Martin  

David:

That's the behavior of perl itself.  A reference to something evaluates to
true, even if that something is empty:

foreach my $ref ([],{},\''){
  print 'reference to an empty ' . ref($ref) . ' evaluates to ';
  $ref ? print "true\n" : print "false\n";
}

outputs:
reference to an empty ARRAY evaluates to true
reference to an empty HASH evaluates to true
reference to an empty SCALAR evaluates to true

Also, in your example, empty_array is a reference to an empty array, not an
actual array.

Perhaps code your sub routines or methods to return undef, rather than a
reference to an empty something.


Mike

_______________________________________________
templates mailing list
[EMAIL PROTECTED]
http://lists.template-toolkit.org/mailman/listinfo/templates

Reply via email to