File Coverage

blib/lib/Devel/Profiler/Plugins/Template/Context.pm
Criterion Covered Total %
statement 13 23 56.5
branch 0 4 0.0
condition 1 5 20.0
subroutine 5 6 83.3
pod 1 1 100.0
total 20 39 51.2


line stmt bran cond sub pod time code
1             package Devel::Profiler::Plugins::Template::Context;
2              
3 2     2   4463 use Template::Context;
  2         37101  
  2         89  
4              
5 2     2   25 use strict;
  2         4  
  2         81  
6 2     2   11 use warnings FATAL => qw(all);
  2         5  
  2         158  
7              
8             #---------------------------------------------------------------------
9             # constants and global variables
10             #---------------------------------------------------------------------
11             our @ISA = qw(Template::Context);
12              
13 2   50 2   10 use constant DEBUG => $ENV{DEVEL_PROFILER_PLUGIN_DEBUG} || 0;
  2         5  
  2         214  
14              
15             # tell tt to use this package instead of Template::Context
16             # for directive parsing
17             $Template::Config::CONTEXT = __PACKAGE__;
18              
19             # capture the real process() sub as a coderef
20             our $process;
21              
22             BEGIN {
23 2     2   405 our $process = *Template::Context::process{CODE};
24             };
25              
26              
27             #---------------------------------------------------------------------
28             # override Template::Context::process() with our own routine
29             # so we can inject Devel::Profiler magic
30             #---------------------------------------------------------------------
31             sub process {
32              
33 0     0 1   my $self = shift;
34              
35 0           my ($template, $params, $localize) = @_;
36              
37             # derive the name of the BLOCK as best we can
38 0           my $name = $template;
39              
40 0 0 0       $name = $template->name()
41             if ref $template && $template->isa('Template::Document');
42              
43             # make sure it's safe to use as a perl subroutine
44 0           $name = Devel::Profiler::Plugins::Template::_tidy_sub($name);
45              
46             # now, create and instrument a new subroutine, based on the
47             # name of the BLOCK. the fully qualified subroutine looks like
48             # TT::INCLUDE::layout_2fframe_2fhead_2ett
49              
50 0 0         my $type = $localize ? 'INCLUDE' : 'PROCESS';
51 0           my $package = "TT::${type}";
52 0           my $sub = "${package}::${name}";
53              
54             # instrument this puppy...
55 0           Devel::Profiler::Plugins::Template::_instrument($package, $sub,
56             $name, $process);
57              
58             # and remember to actually call it so it runs
59 0           return $package->$name($self, @_);
60             };
61              
62              
63             1;
64              
65             __END__