Well, coincidentally I found myself needing to solve the same problem as the
OP.  I replaced some template inputs that had been plain numbers with
objects (implemented in the standard fashion as a reference to a hash) that
perform some basic caching, and that reduce to numbers via "use overload".
I found that my scalar vmethods stopped working.

My new solution was to add an extra level of indirection, implementing my
object as a reference to a reference to a hash.  It now looks a bit like
this:

package MyObj;

use overload '0+' => 'get';

sub new {
  my ($class, $arg1, $arg2) = @_;
  bless \{ one => $arg1, two => $arg2 }, $class;
}

sub get {
  my $self = shift;
  return $$self->{one} + $$self->{two};
}

And voila!  Scalar vmethods can be applied transparently again.  Any kind of
non-hash, non-array object implementation suffices.  If my project were
using inside-out objects, I would have gotten the behavior I wanted for
free.

I briefly toyed with the idea of using a reference to a glob, like the
various IO::* modules do, but I didn't want to be any harder on my
successors than I have to be.


--Sean
_______________________________________________
templates mailing list
templates@template-toolkit.org
http://mail.template-toolkit.org/mailman/listinfo/templates

Reply via email to