| 1 | package Text::Template::LocalVars; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use 5.008009; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use parent 'Text::Template'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use Text::Template::LocalVars::Package; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | 3 | our @EXPORT_OK = qw(fill_in_file fill_in_string TTerror); | |||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | 3 | 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 | 3 | our $TemplatePackage; | |||||||||||||||||||||||||||||||||||||||||
| 24 | 3 | our $TemplateParentPackage; | |||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | 3 | our $TrackParentDefault = 1; | |||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | ################################################################# | ||||||||||||||||||||||||||||||||||||||||||
| 29 | # match public API (and some private ones too) | ||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | 3 | *_param = \&Text::Template::_param; | |||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | 3 | *TTerror = \&Text::Template::TTerror; | |||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | 3 | *ERROR = \$Text::Template::ERROR; | |||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | 21 | ▶ | sub fill_in_string { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
fill_in_string: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 38 | 21 | my $string = shift; | |||||||||||||||||||||||||||||||||||||||||
| 39 | 21 | 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 | my $trackvarpkg = _param( 'trackvarpkg', @_ ); | |||||||||||||||||||||||||||||||||||||||||
| 50 | 21 | ▶ | $trackvarpkg = $TrackParentDefault unless defined $trackvarpkg; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||
| 52 | 21 | ▶ | if ( !defined $package ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 53 | 19 | ▶ | push @_, | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$trackvarpkg and defined $Text::Template::LocalVars::TemplatePackage
MC/DC100%2/2
$trackvarpkg and defined $Text::Template::LocalVars::TemplatePackage
$trackvarpkg
defined $Text::Template::LocalVars::TemplatePackage
Truth table100%3/3
A and B
A= $trackvarpkgB= defined $Text::Template::LocalVars::TemplatePackage
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | 'package' => ( | ||||||||||||||||||||||||||||||||||||||||||
| 55 | $trackvarpkg && defined $TemplatePackage | ||||||||||||||||||||||||||||||||||||||||||
| 56 | ? $TemplatePackage | ||||||||||||||||||||||||||||||||||||||||||
| 57 | : scalar( caller ) ); | ||||||||||||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||||||||||
| 59 | 21 | __PACKAGE__->fill_this_in( $string, @_ ); | |||||||||||||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | ▶ | sub fill_in_file { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
fill_in_file: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | my $fn = shift; | |||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | my $package = _param( 'package', @_ ); | |||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | # pull in the correct package if we're localizing and tracking | ||||||||||||||||||||||||||||||||||||||||||
| 68 | # parent packages. | ||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | my $trackvarpkg = _param( 'trackvarpkg', @_ ); | |||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | ▶ | $trackvarpkg = $TrackParentDefault unless defined $trackvarpkg; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | ▶ | push @_, $TemplatePackage | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
not defined $package and $trackvarpkg and defined $Text::Template::LocalVars::TemplatePackage
not defined $package and $trackvarpkg
MC/DC0%0/3
not defined $package and $trackvarpkg and defined $Text::Template::LocalVars::TemplatePackage
not defined $package
$trackvarpkg
defined $Text::Template::LocalVars::TemplatePackage
Truth table0%0/4
not(A) and B and C
A= not defined $packageB= $trackvarpkgC= defined $Text::Template::LocalVars::TemplatePackage
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | if !defined $package | ||||||||||||||||||||||||||||||||||||||||||
| 75 | && $trackvarpkg | ||||||||||||||||||||||||||||||||||||||||||
| 76 | && defined $TemplatePackage; | ||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | ▶ | my $templ = __PACKAGE__->new( TYPE => 'FILE', SOURCE => $fn, @_ ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | or return undef; | ||||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | ▶ | $templ->compile or return undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | my $text = $templ->fill_in( @_ ); | |||||||||||||||||||||||||||||||||||||||||
| 82 | 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 | 34 | ▶ | sub _del_param { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_del_param: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | 34 | my $kk; | |||||||||||||||||||||||||||||||||||||||||
| 91 | 34 | my ( $k, $h ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | 34 | my $first; | |||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | # delete *all* keys that would match, just to be safe. | ||||||||||||||||||||||||||||||||||||||||||
| 96 | 34 | my @keys = grep { exists $h->{$_} } $k, "\u$k", "\U$k", "-$k", "-\u$k", | |||||||||||||||||||||||||||||||||||||||||
| 97 | "-\U$k"; | ||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | 34 | delete @{$h}{@keys}; | |||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | 34 | return $keys[0]; | |||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | 22 | ▶ | sub fill_in { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
fill_in: called CC=12 SCAR=26.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | 22 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 108 | 22 | my %args = @_; | |||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | 22 | my $trackvarpkg = _param( 'trackvarpkg', %args ); | |||||||||||||||||||||||||||||||||||||||||
| 111 | 22 | ▶ | $trackvarpkg = $TrackParentDefault unless defined $trackvarpkg; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||
| 113 | 22 | my $localize = _param( 'localize', %args ); | |||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||
| 115 | 22 | ▶ | if ( $localize ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | 12 | ▶ | my $pkg | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition80%4/5
_param('package', %args) || ($trackvarpkg ? $Text::Template::LocalVars::TemplatePackage : ()) || scalar caller
_param("package", %args) or $trackvarpkg ? $Text::Template::LocalVars::TemplatePackage : ()
MC/DC50%1/2
_param('package', %args) || ($trackvarpkg ? $Text::Template::LocalVars::TemplatePackage : ()) || scalar caller
_param("package", %args)
$trackvarpkg ? $Text::Template::LocalVars::TemplatePackage : ()
Truth table66%2/3
A or B
A= _param("package", %args)B= $trackvarpkg ? $Text::Template::LocalVars::TemplatePackage : ()
| |||||||||||||||||||||||||||||||||||||||||||
| 118 | = _param( 'package', %args ) | ||||||||||||||||||||||||||||||||||||||||||
| 119 | || ( $trackvarpkg ? $TemplatePackage : () ) | ||||||||||||||||||||||||||||||||||||||||||
| 120 | || scalar( caller ); | ||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||
| 122 | 12 | my $npkg = Text::Template::LocalVars::Package->new( $pkg ); | |||||||||||||||||||||||||||||||||||||||||
| 123 | 12 | ▶ | $args{ _del_param( 'package', \%args ) || 'package' } = $npkg->pkg; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
_del_param('package', \%args) || 'package'
MC/DC100%1/1
_del_param('package', \%args) || 'package'
_del_param('package', \%args)
Truth table100%2/2
A
A= _del_param('package', \%args)
| |||||||||||||||||||||||||||||||||||||||||||
| 124 | 12 | _del_param( 'localize', \%args ); | |||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||
| 126 | 12 | local $TemplateParentPackage = $pkg; | |||||||||||||||||||||||||||||||||||||||||
| 127 | 12 | local $TemplatePackage = $npkg->pkg; | |||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||
| 129 | 12 | $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 | ▶ | my $parent | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition40%2/5
_param('package', %args) || ($trackvarpkg ? $Text::Template::LocalVars::TemplatePackage : ()) || scalar caller
_param("package", %args) or $trackvarpkg ? $Text::Template::LocalVars::TemplatePackage : ()
MC/DC0%0/2
_param('package', %args) || ($trackvarpkg ? $Text::Template::LocalVars::TemplatePackage : ()) || scalar caller
_param("package", %args)
$trackvarpkg ? $Text::Template::LocalVars::TemplatePackage : ()
Truth table33%1/3
A or B
A= _param("package", %args)B= $trackvarpkg ? $Text::Template::LocalVars::TemplatePackage : ()
| |||||||||||||||||||||||||||||||||||||||||||
| 140 | = _param( 'package', %args ) | ||||||||||||||||||||||||||||||||||||||||||
| 141 | || ( $trackvarpkg ? $TemplatePackage : () ) | ||||||||||||||||||||||||||||||||||||||||||
| 142 | || scalar( caller ); | ||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||
| 144 | 10 | local $TemplateParentPackage = $parent; | |||||||||||||||||||||||||||||||||||||||||
| 145 | 10 | 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 | ▶ | $args{ _del_param( 'package', \%args ) || 'package' } = $parent | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
_del_param('package', \%args) || 'package'
MC/DC0%0/1
_del_param('package', \%args) || 'package'
_del_param('package', \%args)
Truth table50%1/2
A
A= _del_param('package', \%args)
| |||||||||||||||||||||||||||||||||||||||||||
| 150 | if $trackvarpkg; | ||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||
| 152 | 10 | $self->SUPER::fill_in( %args ); | |||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||
| 158 | 3 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||
| 160 | __END__ | ||||||||||||||||||||||||||||||||||||||||||