[Templates] Of segfaults, lists and hashes
Simon Wilcox
essuu@ourshack.com
Fri, 10 Oct 2003 03:52:40 +0100 (BST)
This causes a segfault on my 5.6.1 tt v 2.10 setup:
[% PERL %]
$foo = 'hello';
[% END %]
Whereas this does not:
[% PERL %]
my $foo = 'hello';
[% END %]
This segfaults:
[% 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 %]
But make it a RAWPERL block & it doesn't. I don't know what's wrong but
I'm sure it shouldn't segfault.
This came about because I couldn't find the right idiom with the built in
vmethods so I had to add my own. I have a routine that returns a hashref
or an arrayref containing multiple hashrefs. If they're scalars then the
.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
had to resort to the vmethod above and this code:
IF locn.recommendation.is_a( 'HASH' );
recommendations = [ locn.recommendation ];
ELSE;
recommendations = locn.recommendation;
END;
Which seems a bit clumsy to me. Am I missing something here as the
behaviour of .list on hashrefs seems to break the DWIMery of TT ?
Simon.
--
"Time is an illusion, lunchtime doubly so"