>>>>> "Gavin" == Gavin Estey <[EMAIL PROTECTED]> writes:
Gavin> package TimingContext;
Gavin> use base qw( Template::Context );
Gavin> use Time::HiRes qw( gettimeofday tv_interval );
Gavin> foreach my $sub (qw( process include )) {
Gavin> *$sub = sub {
Gavin> my $self = shift;
Gavin> my $template = ref $_[0] ? $_[0]->name : $_[0];
Gavin> my $start = [gettimeofday];
Gavin> my $data = eval "\$self->SUPER::$sub([EMAIL PROTECTED])";
Gavin> my $elapsed = tv_interval($start);
Gavin> return "<!-- START: $template -->\n$data\n<!-- END: $template
Gavin> ($elapsed seconds) -->";
Gavin> };
Gavin> }
Gavin> 1;
Ugh. Eval-string. You can eliminate that with this:
foreach my $sub (qw( process include )) {
my $super = __PACKAGE__->can("SUPER::$sub") or die;
*$sub = sub {
my $self = shift;
my $template = ref $_[0] ? $_[0]->name : $_[0];
my $start = [gettimeofday];
my $data = $super->($self, @_);
my $elapsed = tv_interval($start);
return "<!-- START: $template -->\n$data\n<!-- END: $template
($elapsed seconds) -->";
};
}
It's all a matter of programming. :) Note that this $super->() call
is not a method call, but a real coderef call. Amazingly enough,
to make it a method call, you can just move a few things around:
my $data = $self->$super(@_);
But the net is exactly the same, since $super is a coderef.
--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<[EMAIL PROTECTED]> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
_______________________________________________
templates mailing list
[EMAIL PROTECTED]
http://lists.template-toolkit.org/mailman/listinfo/templates