File Coverage

blib/lib/HTML/Template/JIT.pm
Criterion Covered Total %
statement 55 63 87.3
branch 14 34 41.1
condition 9 16 56.2
subroutine 9 9 100.0
pod 0 1 0.0
total 87 123 70.7


line stmt bran cond sub pod time code
1             package HTML::Template::JIT;
2              
3 10     10   219950 use 5.006;
  10         52  
  10         458  
4 10     10   57 use strict;
  10         19  
  10         460  
5 10     10   61 use warnings;
  10         22  
  10         881  
6              
7             our $VERSION = '0.05';
8              
9 10     10   57 use Carp qw(croak);
  10         47  
  10         927  
10 10     10   59 use File::Spec;
  10         27  
  10         318  
11 10     10   62 use File::Path qw(rmtree mkpath);
  10         26  
  10         954  
12 10     10   54 use Digest::MD5 qw(md5_hex);
  10         15  
  10         8797  
13              
14             sub new {
15 9     9 0 147 my $pkg = shift;
16            
17 9 50       55 croak(__PACKAGE__ . "::new() called with odd number of arguments")
18             if @_ % 2;
19 9         73 my %args = @_;
20              
21 9 50       45 croak(__PACKAGE__ . "::new() : called without required filename parameter")
22             unless exists $args{filename};
23              
24 9 50       55 croak(__PACKAGE__ . "::new() : called without required jit_path parameter")
25             unless exists $args{jit_path};
26            
27 9 50       258 croak(__PACKAGE__ . "::new() : jit_path \"$args{jit_path}\" does not exist!")
28             unless -e $args{jit_path};
29              
30 9 50 33     132 croak(__PACKAGE__ . "::new() : jit_path \"$args{jit_path}\" is not a writable directory!")
31             unless -d _ and -w _;
32              
33             # try to find the template file
34 9   50     77 my $path = _find_file($args{filename}, $args{path} || []);
35 9 50       35 croak(__PACKAGE__ . "::new() : unable to find template file \"$args{filename}\"")
36             unless $path;
37              
38             # setup options we care about
39 9   100     56 $args{global_vars} ||= 0;
40 9   100     56 $args{print_to_stdout} ||= 0;
41 9   50     52 $args{case_sensitive} ||= 0;
42 9   50     74 $args{loop_context_vars} ||= 0;
43              
44             # get a hash of the path and mtime. hashing them together means
45             # that everytime the template file is changed we'll get a new md5
46             # and that'll force a recompile.
47 9         295 my $path_md5 = md5_hex($path . (stat($path))[9] . $VERSION .
48             join(' ', $args{global_vars},
49             $args{print_to_stdout},
50             $args{loop_context_vars},
51             $args{case_sensitive}));
52            
53             # compute package and filesystem details
54 9         34 my $package = "tmpl_$path_md5"; # package name
55 9         76 my $package_dir = File::Spec->catdir($args{jit_path}, $path_md5);
56 9         100 my $package_path = File::Spec->catfile($package_dir, "$package.pm");
57              
58 9 50       49 print STDERR __PACKAGE__ . "::new() : found file : $path : $path_md5\n"
59             if $args{jit_debug};
60              
61 9 50       34 print STDERR __PACKAGE__ . "::new() : attempting to load...\n"
62             if $args{jit_debug};
63              
64             # try to load the module and return package handle if successful
65 9         18 my $result;
66 9         33 eval { $result = require $package_path; };
  9         5368  
67 9 50       81 if ($result) {
68 0         0 $package->clear_params(); # need to clear out params from prior run
69 0         0 return $package;
70             }
71              
72             # die now if we can't compile
73 9 50       40 croak(__PACKAGE__ . "::new() : no_compile is on but no compile form for $path is available!")
74             if $args{no_compile};
75              
76 9 50       35 print STDERR __PACKAGE__ . "::new() : compiling...\n"
77             if $args{jit_debug};
78              
79             # load the compiler
80 9         6683 require 'HTML/Template/JIT/Compiler.pm';
81              
82             # compile the template
83 9         110 $result = HTML::Template::JIT::Compiler::compile(%args,
84             package => $package,
85             package_dir => $package_dir,
86             package_path => $package_path);
87 0 0       0 croak(__PACKAGE__ . "::new() : Unable to compile $path.")
88             unless $result;
89              
90 0         0 return $package;
91             }
92              
93             # _find_file stolen from HTML::Template - needs to stay in sync but it
94             # would be a shame to have to load HTML::Template just to get it.
95             sub _find_file {
96 9     9   35 my ($filename, $path) = @_;
97 9         36 my $filepath;
98              
99             # first check for a full path
100 9 50 33     195 return File::Spec->canonpath($filename)
101             if (File::Spec->file_name_is_absolute($filename) and (-e $filename));
102              
103             # try pre-prending HTML_Template_Root
104 9 50       46 if (exists($ENV{HTML_TEMPLATE_ROOT})) {
105 0         0 $filepath = File::Spec->catfile($ENV{HTML_TEMPLATE_ROOT}, $filename);
106 0 0       0 return File::Spec->canonpath($filepath) if -e $filepath;
107             }
108              
109             # try "path" option list..
110 9         28 foreach my $path (@$path) {
111 9         267 $filepath = File::Spec->canonpath(File::Spec->catfile($path, $filename));
112 9 50       224 return File::Spec->canonpath($filepath) if -e $filepath;
113             }
114              
115             # try even a relative path from the current directory...
116 0 0         return File::Spec->canonpath($filename) if -e $filename;
117            
118 0           return undef;
119             }
120              
121              
122              
123             1;
124             __END__