Re: custom vmethods

Dave Howorth <dhoworth-fDajt2Yx3S8pY9vWkoisglpr/1R2p/[email protected]> Wed, 24 Feb 2016 14:07:44 +0000
Newsgroups gmane.comp.lang.perl.modules.template-toolkit
Message-ID <[email protected]>
On 2016-02-24 13:01, Tom Molesworth wrote:
> Hi Dave,
>
> On 24/02/16 12:44, Dave Howorth wrote:
>> I'm trying to define a custom vmethod but not succeeding.
>>
>> I tried using the way described in the manual and also by calling
>> define_vmethod. Both ways appear to have set up methods in the
>> Template::Stash::SCALAR_OPS hashref but neither of them appears to
>> work, even with a very simple body. Any clues to what I'm doing wrong
>> gratefully received!
>>
>> In the perl:
>>
>> use Template::Stash;
>> $Template::Stash::SCALAR_OPS->{uniprot_is_obsolete} =
>>     sub {
>>         return 42;
>>     };
>>
>> sub forty_two { return 42; }
>> Template::Stash->define_vmethod('scalar', 'forty_two', &forty_two);
>>
>> warn join ' ', keys %$Template::Stash::SCALAR_OPS, "\n";
>
> Those look fine - are you sure that [% representative %] is defined as a
> scalar rather than a hashref/arrayref/object?

representative is an object. An object is a scalar. If I replace the 
definition of x in your test by x => sub {123} the test still passes.

More to the point, representative.defined returns 1 and has the same 
code (apart from the value of the numerical constant) as forty_two, 
which doesn't return 42!

So I still have no idea what's going wrong.

Cheers, Dave

> Attached example test script for the two methods works for me on
> Template 2.26.
>
> cheers,
>
> Tom
>