On Mon, Jul 28, 2008 at 10:10 AM, Sean McAfee <[EMAIL PROTECTED]> wrote:

> On Mon, Jul 28, 2008 at 4:07 AM, Marc Chantreux <
> [EMAIL PROTECTED]> wrote:
>
>> hello,
>>
>> I tried to register a vmethod to an object using this way:
>>
>> use strict;
>> use warnings;
>> use Template;
>> use Template::Stash;
>>
>> $Template::Stash::SCALAR_OPS->{field} = sub {
>>    my $self = shift;
>>    $self->field(@_);
>> }
>>
>> but it doesn't work: $self can't be a reference to be handled by
>> SCALAR_OPS.
>>
>
> Sure it can:
>
>
> sub Foo::field { $_[1] }
>
> $Template::Stash::SCALAR_OPS->{field} = sub { shift->field(@_) };
>
> Template->new->process(\"[% x.field(42) %]", { x => bless({ }, 'Foo') });
>
>
> This prints "42" for me.
>

...And it does so by bypassing SCALAR_OPS entirely.  Oops.  It doesn't work
if the scalar vmethod is named something besides "field".

What you could do, though, is add a hash vmethod, assuming your object is
backed by a hash:

sub Foo::field { $_[1] }

$Template::Stash::HASH_OPS->{foo} = sub { shift->field(@_) };

Template->new->process(\"[% x.foo(42) %]", { x => bless({ }, 'Foo' });


--Sean
_______________________________________________
templates mailing list
[email protected]
http://mail.template-toolkit.org/mailman/listinfo/templates

Reply via email to