[Templates] Re: Array to hash with 1 result

Ryan Blomberg rb@eventful.com
Mon, 9 Jul 2007 12:59:41 -0700


Thanks for all the suggestions and help...  Its nice to see so many  
people taking the time out to respond.

I think I will take the following from this:

1) Don't ever return arrays or hashes from subroutines or else  
subscribe to the "Life is a hack" motto
2) Hate perl for not being able to know what is being returned to the  
caller
3) Love perl for not being able to know what is being returned to the  
caller
4) Find an excuse to prevent template builders from calling  
subroutines directly ( MVC paradigm ) and act religious about it.

Thanks again,

rb

On Jul 9, 2007, at 12:33 PM, Bill Ward wrote:

> Another way is to create a wrapper class that uses AUTOLOAD to call
> the "real" class methods (using SUPER::) but return all the values in
> arrayrefs.  Use that class instead of the real one in all your code.
>
> On 7/9/07, Ryan Blomberg <rb@eventful.com> wrote:
>> The problem with that is that you have to get your ui guys to
>> remember to add the _ref
>>
>> It is more likely that they will forget and will not test for the
>> single element array case and it will
>> come back to bite you.  For now we will follow the convention of
>> separating our data model from
>> the view (some would argue this is the correct thing to do anyway).
>>
>> rb
>>
>> On Jul 9, 2007, at 11:09 AM, Bill Ward wrote:
>>
>> > You could add new methods to your object that return refs.  So if
>> > you have
>> >
>> > sub function
>> > { .... return @something }
>> >
>> > then you could create an additional subroutine:
>> >
>> > sub function_ref
>> > {
>> >    my $self = shift;
>> >    my @something = $self->function(@_);
>> >    \@something;
>> > }
>> >
>> > Use the latter in your templates.
>> >
>> > On 7/9/07, Ryan Blomberg <rb@eventful.com> wrote:
>> >> Sergey and Cees,
>> >>
>> >> Sergey, I am using 2.18 as well.. but discovered why it is  
>> happening.
>> >>
>> >> Due to perl not being strong typed a subroutine can return  
>> whatever
>> >> it wants (scalar,hash,array,reference).
>> >>
>> >> TT tries to guess what was returned so that is in the correct  
>> context
>> >> by doing the following:
>> >>
>> >> my @return = &function(@args);
>> >>
>> >> if(scalar(@return) > 1) {
>> >>         assume array of stuff
>> >> } else {
>> >>         return $return[0];
>> >> }
>> >>
>> >> It does that because doing the following would break things:
>> >>
>> >> my $return = &function(@args); (would only get first key or first
>> >> element if an array or hash is returned)
>> >>
>> >> But, doing it this way means they make a guess in the case of  
>> arrays
>> >> and hashes that yields my issue.
>> >>
>> >> As Cees explained.. the best way to fix this is to always return
>> >> references or scalars from your subs and never arrays or hashes.
>> >>
>> >> We return arrays and hashes in more places than I would like to  
>> admit
>> >> though so we will probably just avoid calling object  
>> subroutines in
>> >> templates.
>> >>
>> >> rb
>> >>
>> >> I am using 2.18 as well.
>> >>
>> >> On Jul 7, 2007, at 6:33 AM, Sergey Martynoff wrote:
>> >>
>> >> >
>> >> >> my template does the following
>> >> >>
>> >> >> [% FOREACH thing IN object.list_things() %]
>> >> >>    The name of the thing is [% thing.name %]
>> >> >> [% END %]
>> >> >>
>> >> >> If the function returns
>> >> >>
>> >> >> return [ { name => 'test' } ];
>> >> >>
>> >> >> I get not output and dumping the result of the function yields
>> >> $VAR1
>> >> >> = { name => 'test' } instead of the expected $VAR1 =  
>> [ { name =>
>> >> >> 'test' } ]
>> >> >
>> >> > You are probably using an older version of Template Tookit. I
>> >> > recall that
>> >> > there was such a problem formerly, but I cannot repeat this  
>> bug on
>> >> > TT 2.18.
>> >> >
>> >> >
>> >> > --
>> >> > Sergey Martynoff
>> >> >
>> >> >
>> >> > _______________________________________________
>> >> > templates mailing list
>> >> > templates@template-toolkit.org
>> >> > http://lists.template-toolkit.org/mailman/listinfo/templates
>> >> >
>> >>
>> >>
>> >> _______________________________________________
>> >> templates mailing list
>> >> templates@template-toolkit.org
>> >> http://lists.template-toolkit.org/mailman/listinfo/templates
>> >>
>> >
>> >
>> > --
>> > Help bring back the San Jose Earthquakes - http://
>> > www.soccersiliconvalley.com/
>> >
>> > _______________________________________________
>> > templates mailing list
>> > templates@template-toolkit.org
>> > http://lists.template-toolkit.org/mailman/listinfo/templates
>> >
>>
>>
>
>
> -- 
> Help bring back the San Jose Earthquakes - http:// 
> www.soccersiliconvalley.com/
>
> _______________________________________________
> templates mailing list
> templates@template-toolkit.org
> http://lists.template-toolkit.org/mailman/listinfo/templates
>