List Info

Thread: Template can not use overloaded @{} operator




Template can not use overloaded @{} operator
user name
2006-02-01 15:45:58
Hello everybody,

i have the following package definition:
package Collection;
use overload 
	'{}' 	=> &op_deref_array;


sub new {
	# usual new code
	my $self = shift;
	$self-> [1-3] = [];	
}

sub op_deref_array {
	my $object = shift;
	return $object-> [1-3];
}


as i use this in normal Perl like :

my $c = new Collection();
foreach my $x ({$c}) {
	print $x . "n";
}

i get the right objects ...

if i use this with template toolkit.
my $c = new Collection();
my $t = new Template();
$t->process('mytemplate.txt', { list => $c });

and where mytemplate.txt looks like 
[% c %]
[% FOREACH x = c %]
[% x %]
[% END %]

i can not see the 'x' in my output. It seems that the
overloading of the
{}
operator doesn't work in TT.

any suggestions.

thanks in advance

Gernot

_______________________________________________
templates mailing list
templatestemplate-toolkit.org
http://lists.template-toolkit.org/mailman/listinfo/t
emplates
Template can not use overloaded @{} operator
user name
2006-02-01 17:43:33
Hi Gernot -

> and where mytemplate.txt looks like
> [% c %]
> [% FOREACH x = c %]
> [% x %]
> [% END %]

Did you mean :
   [% FOREACH x IN c %]
?


Larry

_______________________________________________
templates mailing list
templatestemplate-toolkit.org
http://lists.template-toolkit.org/mailman/listinfo/t
emplates
Template can not use overloaded @{} operator
user name
2006-02-01 19:03:11
Gernot,

> i have the following package definition:
> package Collection;
> use overload 
> 	'{}' 	=> &op_deref_array;
> :
> :
> i can not see the 'x' in my output. It seems that the
overloading of the
> {} operator doesn't work in TT.

You are absolutely correct: it doesn't work.

I looked into doing a patch for this once, but I never had
the time to actually follow through.  Patching the Perl
stash 
code seems relatively simple (although there were a few
different ways to do it and I thought I'd probably have to
do 
some profiling to see what gave the best performance), but
then there's the issue of the XS stash code, which just
plain 
scares me. <g>

What I did as a workaround will only work for getting around
the overloaded hash dereferencing not working, and it's not 
without it's problems even then.  For getting around the
array dereferencing not working, AFAIK your only hope is to 
create some function that takes an int and returns that
index out of your list.  Then you can access that method 
directly in TT2.  It's inelegant, but it would work.


		-- Buddy


PS--My solution for the hash deref problem, for those who
care, is below.  Its primary problem is that I can't have
any 
hash key which exactly matches a method name.  Of course, in
TT2, that would have been a problem anyway.  But now it's a 
problem in my Perl code as well.  Thus far, it hasn't bitten
me. <knocking on wood>

Suggestions for improvement, as always, welcome.

our $AUTOLOAD;
sub AUTOLOAD
{
     my ($method) = $AUTOLOAD =~ /::(w+)$/;
     # for some reason DESTROY gets called via AUTOLOAD a
lot; don't want _that_ one to blow up
     return if $method eq 'DESTROY';

     my ($this) = _;
     debuggit("DataRow::AUTOLOAD: method $AUTOLOAD
($method) with", ref $this) if DEBUG >= 4;

     if (ref $this eq 'Geek::SQL:ataRow')
     {
         # not sure why we need to go directly into the hash
with this, but the overload apparently doesn't work in
AUTOLOAD
         return exists $$this->->{$method} ?
$$this->->{$method} : croak("No such method
(or column): $method");
     }
     else
     {
         croak("No such class method: $method");
     }
}

_______________________________________________
templates mailing list
templatestemplate-toolkit.org
http://lists.template-toolkit.org/mailman/listinfo/t
emplates
[1-3]

about | contact  Other archives ( Real Estate discussion Medical topics )