Subject: | patch to handle objects that overload '%{}' or '@{}' |
Find enclosed patch to handle objects overloaded to generate hash-ref or array-ref.
If an object overloads both it will be treated as a hash-ref. Not sure whether it is useful
to test $atom for numeric and handle array-ref first.
The patch works for me - it isn't widely tested - but it is really simple.
I created a sub-class of Class::DBI::Iterator that overloads '@{}' to work with this patch.
Thanks for a great module. I like TAL. I don't like zope.
:)
Chris.
Subject: | overloaded-objects.patch.txt |
Index: lib/Template/TAL/Language/TAL.pm
===================================================================
--- lib/Template/TAL/Language/TAL.pm (revision 1101)
+++ lib/Template/TAL/Language/TAL.pm (working copy)
@@ -24,6 +24,7 @@
use strict;
use Carp qw( croak );
use base qw( Template::TAL::Language );
+use overload;
use Template::TAL::TALES;
@@ -71,7 +72,9 @@
# coerce to a list if it's not already. Template toolkit does this, I like it.
my $items = Template::TAL::TALES->value($list, $local_context, $global_context);
- my @items = UNIVERSAL::isa($items, "ARRAY") ? @$items : ($items);
+ my @items =
+ ( UNIVERSAL::isa($items, "ARRAY") || overload::Method($items, '@{}') ) ?
+ @$items : ($items);
# create a scratch node to hold the things we're going to hand
# back. We do this rather than create a list, because the recursive
Index: lib/Template/TAL/TALES.pm
===================================================================
--- lib/Template/TAL/TALES.pm (revision 1101)
+++ lib/Template/TAL/TALES.pm (working copy)
@@ -23,6 +23,7 @@
package Template::TAL::TALES;
use warnings;
use strict;
+use overload;
use Carp qw( croak );
use Scalar::Util qw( blessed );
@@ -88,9 +89,11 @@
if (ref($local) and blessed($local) and $local->can($atom) ) {
$local = $local->$atom();
# TODO what about objects that support hash de-referencing or something?
- } elsif (UNIVERSAL::isa($local, "HASH")) {
+ } elsif (UNIVERSAL::isa($local, "HASH") or
+ overload::Method($local,'%{}')) {
$local = $local->{ $atom };
- } elsif (UNIVERSAL::isa($local, "ARRAY")) {
+ } elsif (UNIVERSAL::isa($local, "ARRAY") or
+ overload::Method($local,'@{}')) {
no warnings 'numeric';
if ($atom eq int($atom)) {
$local = $local->[ $atom ];