jatill has asked for the wisdom of the Perl Monks concerning the following question:

I have a method that uses Contextual::Return to return either a hash or an object depending on how the method is called.

Ex:

sub my_method{ return LIST { $self->get_hash() } HASHREF { $self->get_hash() } OBJREF { $self->get_object()} } ; }
$self->my_method->get_age('phil') works, and so does $self->my_method->{age}{phil}.

However, when I try to use this method from Template::Toolkit, I only get a value back for $self->my_method->{age}{phil}. I am guess this is because somewhere behind the scenes, Template::Toolkit is asking for all data as a hashref, so that's what my_method will always return.

Is my guess correct? Is there a way to make Template::Toolkit behave 'intelligently', like the perl code works?

Thanks. ~jeff

Replies are listed 'Best First'.
Re: Template::Toolkit and Contextual::Return
by perrin (Chancellor) on Sep 22, 2006 at 18:55 UTC
    The TT stash code tries to examine what it's being used on to see if it should be called as a method or as a hash. It works iteratively, so it will call $self->my_method, and then call ->get_age on that. I don't use Contextual::Return so I don't know what it's looking for, but it sounds like actual method chaining may be needed in order to make it work, and TT will not do that when evaluating dot notation.