File Coverage

blib/lib/Text/Template/LocalVars.pm
Criterion Covered Total %
statement 47 56 83.9
branch 11 20 55.0
condition 7 17 41.1
subroutine 8 9 88.8
pod 3 3 100.0
total 76 105 72.3


line stmt bran cond sub pod time code
1             package Text::Template::LocalVars;
2              
3 3     3   105681 use 5.008009;
  3         14  
4 3     3   17 use strict;
  3         6  
  3         94  
5 3     3   32 use warnings;
  3         6  
  3         195  
6              
7 3     3   1475 use parent 'Text::Template';
  3         933  
  3         23  
8 3     3   18480 use Text::Template::LocalVars::Package;
  3         12  
  3         2782  
9              
10             our @EXPORT_OK = qw(fill_in_file fill_in_string TTerror);
11              
12             our $VERSION = '0.04';
13              
14             #################################################################
15              
16             # These are used to pass the name of the variable package to a nested
17             # fill. The fragment code may run code compiled into *another* package,
18             # and if that code wants to perform a localized fill using the
19             # fragment's variable package, it has no easy way of finding out.
20             # This package variable is localized to the correct package name just
21             # prior to calling SUPER::fill_in
22              
23             our $TemplatePackage;
24             our $TemplateParentPackage;
25              
26             our $TrackParentDefault = 1;
27              
28             #################################################################
29             # match public API (and some private ones too)
30              
31             *_param = \&Text::Template::_param;
32              
33             *TTerror = \&Text::Template::TTerror;
34              
35             *ERROR = \$Text::Template::ERROR;
36              
37             sub fill_in_string {
38 21     21 1 694967 my $string = shift;
39 21         91 my $package = _param( 'package', @_ );
40              
41             # pull in the correct package if we're tracking parent
42             # packages. note that we retain Text::Template's behavior of
43             # always assigning a package ( RT#51473 ). this behavior is most
44             # likely because otherwise if the package parameter is not
45             # specified the call to fill_this_in() will always use *this*
46             # package to store its values, rather than the original caller's
47             # package.
48              
49 21         554 my $trackvarpkg = _param( 'trackvarpkg', @_ );
50 21 100       277 $trackvarpkg = $TrackParentDefault unless defined $trackvarpkg;
51              
52 21 100       58 if ( !defined $package ) {
53 19 100 100     165 push @_,
54             'package' => (
55             $trackvarpkg && defined $TemplatePackage
56             ? $TemplatePackage
57             : scalar( caller ) );
58             }
59 21         141 __PACKAGE__->fill_this_in( $string, @_ );
60             }
61              
62             sub fill_in_file {
63 0     0 1 0 my $fn = shift;
64              
65 0         0 my $package = _param( 'package', @_ );
66              
67             # pull in the correct package if we're localizing and tracking
68             # parent packages.
69              
70 0         0 my $trackvarpkg = _param( 'trackvarpkg', @_ );
71 0 0       0 $trackvarpkg = $TrackParentDefault unless defined $trackvarpkg;
72              
73 0 0 0     0 push @_, $TemplatePackage
      0        
74             if !defined $package
75             && $trackvarpkg
76             && defined $TemplatePackage;
77              
78 0 0       0 my $templ = __PACKAGE__->new( TYPE => 'FILE', SOURCE => $fn, @_ )
79             or return undef;
80 0 0       0 $templ->compile or return undef;
81 0         0 my $text = $templ->fill_in( @_ );
82 0         0 $text;
83             }
84              
85              
86             # delete a parameter from a passed set and return the key that was
87             # used to specify it.
88             sub _del_param {
89              
90 34     34   56 my $kk;
91 34         73 my ( $k, $h ) = @_;
92              
93 34         50 my $first;
94              
95             # delete *all* keys that would match, just to be safe.
96 34         117 my @keys = grep { exists $h->{$_} } $k, "\u$k", "\U$k", "-$k", "-\u$k",
  204         485  
97             "-\U$k";
98              
99 34         74 delete @{$h}{@keys};
  34         82  
100              
101 34         116 return $keys[0];
102             }
103              
104              
105             sub fill_in {
106              
107 22     22 1 10057 my $self = shift;
108 22         133 my %args = @_;
109              
110 22         90 my $trackvarpkg = _param( 'trackvarpkg', %args );
111 22 100       329 $trackvarpkg = $TrackParentDefault unless defined $trackvarpkg;
112              
113 22         65 my $localize = _param( 'localize', %args );
114              
115 22 100       254 if ( $localize ) {
116              
117 12   50     38 my $pkg
118             = _param( 'package', %args )
119             || ( $trackvarpkg ? $TemplatePackage : () )
120             || scalar( caller );
121              
122 12         229 my $npkg = Text::Template::LocalVars::Package->new( $pkg );
123 12   100     74 $args{ _del_param( 'package', \%args ) || 'package' } = $npkg->pkg;
124 12         45 _del_param( 'localize', \%args );
125              
126 12         31 local $TemplateParentPackage = $pkg;
127 12         37 local $TemplatePackage = $npkg->pkg;
128              
129 12         165 $self->SUPER::fill_in( %args );
130              
131             }
132              
133             else {
134              
135             # track the template variable package even if not localizing,
136             # in case a fragment calls fill_in and wants to localize
137             # and track the parent. yes, we are our own parent.
138              
139 10   0     55 my $parent
140             = _param( 'package', %args )
141             || ( $trackvarpkg ? $TemplatePackage : () )
142             || scalar( caller );
143              
144 10         111 local $TemplateParentPackage = $parent;
145 10         16 local $TemplatePackage = $parent;
146              
147             # if no package was specified, and we're requested to track the parent,
148             # use that as the package
149 10 50 50     43 $args{ _del_param( 'package', \%args ) || 'package' } = $parent
150             if $trackvarpkg;
151              
152 10         57 $self->SUPER::fill_in( %args );
153              
154             }
155              
156             }
157              
158             1;
159              
160             __END__