On Thu, Jan 24, 2002 at 05:28:59PM -0500, Mark Mills wrote:
> [% IF array.defined %] should tell you if something is null or non-existant in the 
>stashhash.

Yep, if 'array' is defined then .defined returns true, even if it's a 
reference to an empty array or empty hash.

> As people have said, DWIM sez [% IF array %] should be true only if array contains 
>something more than nothing.

As far as I know, there's no way to do this without re-writing large parts
of TT.  Even then, I don't think it's possible.

The Perl code generated for 

  [% IF array %] 

looks like 

  if ($stash->get('array'))

It has to work that way so that things like this:

  [% array1 = array2 %]

properly translate to

  $stash->set('array1', $stash->get('array2'));

That is, it's no good having $stash->get('array2') realised that 'array2'
is an array reference (it can do that fine) and then return 0 or 1 depending
on whether or not it contains anything.  If you did that, then you'd get 
this:

  [% array1 = [] %]
  [% array2 = array1 %]   # array2 = 0

Perhaps the best solution is to have a '.size' method which works consistently
across all data types:

  [% item.size %]    # 1
  [% hash.size %]    # scalar(keys %$hash)
  [% list.size %]    # scalar(@$list)

It's not as DWIMmy as [% IF something %], but at least you could then be
sure that [% IF something.size %] will always work as expected.

If you look for the precedent in Perl, you have something like:

  my $ref = [ ];
  if ($ref) {
     # TRUE!
  }

You have to think of a list ref as being a container like can of beans.
Although it tempting to think that the DWIMery might be nice, I think 
the correct interpretation of 'if $can_of_beans' is to test if the container
is defined, not if it contains any beans.


A



Reply via email to