It has been discussed:
http://www.mail-archive.com/[email protected]/msg08514.html
Best is to always return a reference and avoid "wantarray"...
-M.
Michael Lackhoff wrote:
> Hello,
>
> I have a problem when iterating over the results of an object method
> that returns its results dependant on wantarray.
> The problem only shows, when the result contains only a single element
> and a vmethod like reverse is called (see example below).
>
> This is what seems to happen: TT calls mainobject->subobjects in list
> context and gets a list with only one element. When reverse is called on
> this element it is inspected found to be a hash_ref, converted to a
> hash, converted to an array and that array reversed -- wich obviously is
> not the desired result.
>
> Is there a way to tell TT to either take the result as an array no
> matter how many results there are OR force a scalar context on the
> mainobjects.subobjects call?
>
> I already tried .list and a check on .size both don't work because they
> do the same unhelpful conversion(?).
> I also wanted to give Template::Stash::Context a try but don't know how
> to change my code to use it correctly. When I tried, I always got an
> error that SubObject doesn't have a reverse-method.
>
> Thanks for any help
> -Michael
>
> here is a complete example that demonstrates the problem:
> #!perl -w
>
> package MainObject;
> sub new { bless {}, shift }
> sub subobjects {
> my $data = SubObject->new();
> my @data = ($data);
> # no problem when there are more than 1 subobjects
> return wantarray ? @data : [EMAIL PROTECTED];;
> }
>
> package SubObject;
> sub new { bless {somevalue => '2008-01-01'}, shift}
> sub somevalue {
> return shift->{somevalue};
> }
>
> package main;
> use strict;
> use Template;
>
> my $template = Template->new();
> $template->process(\*DATA, {mainobject => MainObject->new()});
>
> __END__
> # Does work:
> [% FOREACH subobject IN mainobject.subobjects %]
> [% subobject.somevalue %]
> [% END %]
> # Doesn't work:
> [% FOREACH subobject IN mainobject.subobjects.reverse %]
> [% subobject.somevalue %]
> [% END %]
>
> _______________________________________________
> templates mailing list
> [email protected]
> http://mail.template-toolkit.org/mailman/listinfo/templates
>
>
--
www.dynarchlib.com - the mighty AJAX toolkit
_______________________________________________
templates mailing list
[email protected]
http://mail.template-toolkit.org/mailman/listinfo/templates