[Templates] Of segfaults, lists and hashes

Andy Wardley abw@andywardley.com
Fri, 10 Oct 2003 09:52:08 +0100


Simon Wilcox wrote:
> This causes a segfault on my 5.6.1 tt v 2.10 setup:
> 
> [% PERL %]
> $foo = 'hello';
> [% END %]

It doesn't on 5.8.0 which makes me think it's a Perl bug,not TT.

> [% PERL %]
> my $sub = sub { ( ref( $_[0] ) eq $_[1] ) };
> $Template::Stash::LIST_OPS->{ is_a } = $sub;
> $Template::Stash::HASH_OPS->{ is_a } = $sub;
> $Template::Stash::SCALAR_OPS->{ is_a } = $sub;
> [% END %]

And again, it's OK on 5.8.0.

BTW, you can write it like this:

[% PERL %]
my $sub = sub { UNIVERSAL::isa($_[0], $_[1]) ? 1 : 0 };
$stash->define_vmethod( list => isa => $sub);
[% END %]

Or rather, that's how I would write it, using UNIVERSAL::isa and
the new define_vmethod() method.  You can also call define_vmethod()
on the context, btw and it delegates it off to the stash.

$context->define_vmethod( list => isa => $sub );

> .list vmethod automagically converts the single scalar into an arrayref
> but when they're hashref's, .list doesn't DWIM, instead it returns a list
> of hashrefs containing each key, value pair from the original hashref. 

I'll cover this in a separate message...

A