Perrin Harkins wrote:
> If performance is a consideration, Sean's will be better than using AUTOLOAD.

One of my favorite tricks in this situation is to have the AUTOLOAD generate
the method (or in this case, alias to it) the first time it is called.  Then
you get the benefit of auto-generated methods, but only pay the AUTOLOAD
penalty once per method.  Subsequent calls to the same method are as fast as
"naturally" defined methods.  Something like this should do it (untested):

sub AUTOLOAD {
     my $self  = shift;
     my $class = ref $self || $self;
     my $name  = $AUTOLOAD;
     my $code;
     no strict 'refs';

     $name =~ s/.*://;
     return if $name eq 'DESTROY';

     if ($ALLOW{$name} && ($code = Template::Directive->can($name))) {
         *{ $class.'::'.$name } = $code;    # alias method for next time
         return $code->($self, @_);         # call method first time
     } else {
         die("\U$name\E not allowed");
     }
}

Also note that this aliases directly to the Template::Directive method
rather than creating a wrapper subroutine around it, so it should be
slightly faster than both the other versions.

HTH
A

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

Reply via email to