blib/lib/Interpolation.pm

filter: Statementstmt 84.4% 212 / 251 Branchbran 60.5% 46 / 76 Conditioncond 56.2% 18 / 32 MC/DCmcdc 31.2% 5 / 16 Subroutinesub 95.9% 47 / 49 Podpod n/a totaltotal 77.3% 328 / 424 CC 50 SCAR 19.8
CC
50
Cov
77%
FETCH
20.7
FETCH
13.1
TIEHASH
13.1
CRAP 7.2
↑ Summary
1 # -*- mode: cperl; cperl-indent-level: 2 -*-
2 #
3 # This module is copyright 1998 Mark-Jason Dominus.
4 # (mjd-perl-interpolation@plover.com)
5 # and 2002-2009 Jenda Krynicky
6
7 package Interpolation;
8 4 ▶use vars '$VERSION';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 $VERSION = '0.74';
10 4 ▶use strict 'vars';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶no warnings 'uninitialized'; # I don't want to be forced to use "if (defined $foo and $foo)
Subroutine BEGIN: called CC=1 SCAR=0.0
13
14 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15
16 %Interpolation::builtin = (
17 5 ▶ null => sub { $_[0] },
Subroutine __ANON__: called CC=1 SCAR=0.0
18 5 ▶ 'eval' => sub { $_[0] },
Subroutine __ANON__: called CC=1 SCAR=0.0
19 3 ▶ identity => sub { $_[0] },
Subroutine __ANON__: called CC=1 SCAR=0.0
20 ucwords =>
21 4 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
22 4 my $s = lc shift;
23 4 $s =~ s/\b(\w)/\u$1/g;
24 4 $s
25 },
26 commify =>
27 9 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
28 9 local $_ = sprintf("%.2f", shift());
29 9 ▶ 1 while s/^(-?\d+)(\d{3})/$1,$2/;
Branch: 1 branch
BranchTrueFalseTotal
s/^(-?\d+)(\d{3})/$1,$2/ and do { '???' } 8 9 17
30 9 $_;
31 },
32 'reverse' =>
33 2 ▶ sub { reverse $_[0] },
Subroutine __ANON__: called CC=1 SCAR=0.0
34
35 # Idea for funky sprintf trick thanks to Ken Fox
36 'sprintf' =>
37 5 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
38 5 my %fakehash;
39 5 my $format = shift;
40 tie %fakehash, Interpolation,
41 5 ▶ sub { sprintf($format, split /$;/o,$_[0])};
Subroutine __ANON__: called CC=1 SCAR=0.0
42 5 \%fakehash;
43 },
44 'sprintf1' =>
45 5 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
46 5 my ($fmt, @args) = split(/$;/o, shift());
47 5 sprintf($fmt, @args);
48 },
49 5 ▶ 'sprintfx' => sub {sprintf shift(), @_},
Subroutine __ANON__: called CC=1 SCAR=0.0
50 2 ▶ 'sqlescape' => sub {$_ = $_[0]; s/'/''/g; "'".$_},
Subroutine __ANON__: called CC=1 SCAR=0.0
51 4 ▶ 'htmlescape' => sub {HTML::Entities::encode($_[0], '^\r\n\t !\#\$%\"\'-;=?-~')},
Subroutine __ANON__: called CC=1 SCAR=0.0
52 4 ▶ 'tagescape' => sub {HTML::Entities::encode($_[0], '^\r\n\t !\#\$%\(-;=?-~')},
Subroutine __ANON__: called CC=1 SCAR=0.0
53 4 ▶ 'jsescape' => sub {my $s = $_[0];$s =~ s/(['"])/\\$1/g;HTML::Entities::encode($s, '^\r\n\t !\#\$%\(-;=?-~')},
Subroutine __ANON__: called CC=1 SCAR=0.0
54 0 ▶ 'round' => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
55 0 my ($number, $scale);
56 0 ▶ if (defined $_[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[1]) { } 0 0 0
57 0 ($number, $scale) = @_;
58 } else {
59 0 ($number, $scale) = split /$;/o, $_[0];
60 }
61 0 ▶ $scale = 1 unless $scale;
Branch: 1 branch
BranchTrueFalseTotal
unless $scale 0 0 0
62 0 return POSIX::floor(($number / $scale) + 0.5) * $scale;
63 }
64 4 );
65
66 4 %Interpolation::needmodules = (
67 'htmlescape' => 'use HTML::Entities;',
68 'tagescape' => 'use HTML::Entities;',
69 'jsescape' => 'use HTML::Entities;',
70 'round' => 'use POSIX;',
71 );
72
73 4 my %is_scalar;
74
75 27 ▶sub import {
Subroutine import: called CC=6 SCAR=18.0
76 27 my $caller_pack = caller;
77 27 shift; # the "Interpolation"
78 # print STDERR "exporter args: (@_); caller pack: $caller_pack\n";
79 27 ▶ if (@_ % 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ % 2) 0 27 27
80 0 croak "Argument list in `use Interpolation' must be list of pairs; aborting";
81 }
82 27 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 23 27 50
83 23 my $hashname = shift;
84 23 my $function = shift;
85 23 my $type;
86
87 23 ▶ $function = $hashname unless $function;
Branch: 1 branch
BranchTrueFalseTotal
unless $function 0 23 23
88
89 23 ▶ if ($hashname =~ /^(.+):([\$\@\*\\]*->[\$\@])$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hashname =~ /^(.+):([\$\@\*\\]*->[\$\@])$/) { } 9 14 23
90 # there is a type specification !
91 9 $type = $2;
92 9 $hashname = $1;
93 9 ▶ if ($type eq '->$') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "->\$") 1 8 9
94 1 $is_scalar{$caller_pack . '::' . $hashname} = undef;
95 1 my $fakescalar;
96 1 tie $fakescalar, 'Interpolation::Scalar', $function;
97 1 *{$caller_pack . '::' . $hashname} = \$fakescalar;
98 1 next;
99 }
100 } else {
101 14 $type = '$->$';
102 }
103
104 22 my %fakehash;
105 22 tie %fakehash, 'Interpolation', $type, $function;
106 22 *{$caller_pack . '::' . $hashname} = \%fakehash;
107 }
108 27 return 1
109 }
110
111 35 ▶sub TIEHASH {
Subroutine TIEHASH: called CC=11 SCAR=24.0
112 35 shift;
113 35 my $function = pop();
114 35 my $type = shift();
115 35 my $my_pack;
116
117 35 ▶ if ($type eq '' or $type eq '$->$') {
Branch: 4 branches
BranchTrueFalseTotal
if ($type eq '' or $type eq '$->$') { } 22 13 35
elsif ($type eq '$->@') { } 4 9 13
elsif ($type eq '@->$') { } 2 7 9
elsif ($type eq '@->@') { } 2 5 7
Condition100%3/3
$type eq "" or $type eq "\$->\$"
l!l&&r!l&&!r
61613
MC/DC100%2/2
$type eq "" or $type eq "\$->\$"
$type eq "" $type eq "\$->\$"
Truth table100%3/3
A or B
A= $type eq ""B= $type eq "\$->\$"
ABresult
1X1
011
000
118 22 $my_pack = 'Interpolation::base'
119 } elsif ($type eq '$->@') {
120 4 $my_pack = 'Interpolation::S2A';
121 } elsif ($type eq '@->$') {
122 2 $my_pack = 'Interpolation::A2S';
123 } elsif ($type eq '@->@') {
124 2 $my_pack = 'Interpolation::A2A';
125 } else {
126 5 $my_pack = 'Interpolation::general';
127 }
128
129 35 ▶ if (ref $function) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $function) { } 20 15 35
130 20 ▶ croak "'use Interpolation' needs a reference to a subroutine or a builtin name!" unless ref $function eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $function eq "CODE" 0 20 20
131 } else {
132 15 my $lc_function = lc $function;
133 15 my $lc_hashname;
134 15 ▶ if (exists $Interpolation::builtin{$lc_function}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Interpolation::builtin{$lc_function}) { } 15 0 15
135 15 ▶ if (exists $Interpolation::needmodules{$lc_function}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Interpolation::needmodules{$lc_function}) 3 12 15
136 3 ▶ eval $Interpolation::needmodules{$lc_function};
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
137 3 ▶ croak $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 3 3
138 3 delete $Interpolation::needmodules{$lc_function}
139 }
140 15 $function = $Interpolation::builtin{$lc_function};
141 } else {
142 0 croak "Unknown builtin $function!\n";
143 }
144 }
145
146 35 $my_pack->TIEHASH($function, $type);
147 }
148
149 1 ▶sub unimport {
Subroutine unimport: called CC=3 SCAR=11.9
150 4 ▶ no warnings 'untie';
Subroutine BEGIN: called CC=1 SCAR=0.0
151 1 my $caller_pack = caller;
152 1 my $my_pack = shift;
153 1 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 1 2
154 1 my $varname = shift;
155 1 ▶ if (!exists $is_scalar{$caller_pack . '::' . $varname}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $is_scalar{$caller_pack . '::' . $varname}) { } 1 0 1
156 1 my $oldvar = *{$caller_pack . '::' . $varname}{HASH};
157 1 my %fakehash;
158 1 *{$caller_pack . '::' . $varname} = \%fakehash;
159 1 untie %$oldvar;
160 } else {
161 0 my $oldvar = *{$caller_pack . '::' . $varname}{SCALAR};
162 0 my $fakescalar;
163 0 *{$caller_pack . '::' . $varname} = \$fakescalar;
164 0 untie $$oldvar;
165 0 delete $is_scalar{$caller_pack . '::' . $varname};
166 }
167 }
168 }
169
170 package Interpolation::base;
171 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
172
173 30 ▶sub TIEHASH {
Subroutine TIEHASH: called CC=5 SCAR=25.7
174 30 my $pack = shift;
175 30 my $cref = shift;
176 30 ▶ unless (ref $cref) { # Convert symbolic name to function ref
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $cref) 0 30 30
177 croak "Unknown builtin function `$cref'; aborting"
178 0 ▶ unless exists $Interpolation::builtin{lc $cref};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $Interpolation::builtin{lc $cref} 0 0 0
179
180 eval $Interpolation::needmodules{lc $cref}
181 0 ▶ if (exists $Interpolation::needmodules{lc $cref});
Branch: 1 branch
BranchTrueFalseTotal
if exists $Interpolation::needmodules{lc $cref} 0 0 0
182 0 ▶ croak $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
183
184 0 $cref = $Interpolation::builtin{lc $cref};
185 }
186 30 bless $cref => $pack; # That's it? Yup!
187 }
188
189 # This is where the magic is.
190 75 ▶sub FETCH {
Subroutine FETCH: called CC=1 SCAR=0.0
191 75 &{$_[0]}($_[1]); # For pre-5.004_04 compatibility
192 #$_[0]->($_[1]); # Line of the day?
193 }
194
195 0 ▶sub cut_it_out {
Subroutine cut_it_out: not called CC=1 SCAR=6.9
196 0 my $object = shift;
197 0 my $caller = (caller(1))[3];
198 0 croak "Not allowed to use $caller on an Interpolation variable; aborting";
199 }
200
201 2 ▶sub STORE {
Subroutine STORE: called CC=1 SCAR=0.0
202 2 &{$_[0]}($_[1], $_[2]); # For pre-5.004_04 compatibility
203 #$_[0]->($_[1]); # Line of the day?
204 }
205
206 4 *DELETE = \&cut_it_out;
207 4 *CLEAR = \&cut_it_out;
208 4 *EXISTS = \&cut_it_out;
209 4 *FIRSTKEY = \&cut_it_out;
210 4 *NEXTKEY = \&cut_it_out;
211
212 22 ▶sub UNTIE {};
Subroutine UNTIE: called CC=1 SCAR=0.0
213
214 package Interpolation::S2A;
215
216 4 @Interpolation::S2A::ISA = ('Interpolation::base');
217 12 ▶sub FETCH {
Subroutine FETCH: called CC=1 SCAR=0.0
218 12 join $", &{$_[0]}($_[1]);
219 }
220
221 2 ▶sub STORE {
Subroutine STORE: called CC=2 SCAR=8.4
222 2 ▶ if (defined wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined wantarray) { } 2 0 2
223 2 join $", &{$_[0]}($_[1], $_[2]);
224 } else {
225 0 &{$_[0]}($_[1], $_[2]);
226 }
227 }
228
229 package Interpolation::A2A;
230
231 4 @Interpolation::A2A::ISA = ('Interpolation::base');
232 4 ▶sub FETCH {
Subroutine FETCH: called CC=1 SCAR=0.0
233 4 join $", &{$_[0]}(split /$;/o,$_[1]);
234 }
235
236 package Interpolation::A2S;
237
238 4 @Interpolation::A2S::ISA = ('Interpolation::base');
239 6 ▶sub FETCH {
Subroutine FETCH: called CC=1 SCAR=0.0
240 6 &{$_[0]}(split /$;/o,$_[1]);
241 }
242
243 package Interpolation::general;
244 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
245
246 4 @Interpolation::general::ISA = ('Interpolation::base');
247
248 5 ▶sub TIEHASH {
Subroutine TIEHASH: called CC=3 SCAR=12.3
249 5 my $pack = shift;
250 5 my $cref = shift;
251 5 my $type = shift;
252 5 my $self = [];
253 5 ▶ unless (ref $cref) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $cref) 0 5 5
254 croak "Unknown builtin function `$cref'; aborting"
255 0 ▶ unless exists $Interpolation::builtin{$cref};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $Interpolation::builtin{$cref} 0 0 0
256 0 $cref = $Interpolation::builtin{$cref};
257 }
258 5 $self->[0] = reverse $type;
259 5 $self->[1] = $cref;
260 5 bless $self => $pack; # That's it? Yup!
261 }
262
263 19 ▶sub FETCH {
Subroutine FETCH: called CC=11 SCAR=30.3
264 19 my $self = shift;
265 19 my $type = $self->[0];
266 19 my $par1type = chop ($type);
267 19 my (@param,$par1subtype);
268 19 ▶ if (($par1type eq '\\') and (($par1subtype = chop($type)) eq '@')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($par1type eq '\\' and ($par1subtype = chop $type) eq '@') { } 6 13 19
elsif ($par1type eq '@') { } 0 13 13
Condition66%2/3
$par1type eq "\\" and ($par1subtype = chop $type) eq "\@"
!ll&&!rl&&r
1306
MC/DC50%1/2
$par1type eq "\\" and ($par1subtype = chop $type) eq "\@"
$par1type eq "\\" ($par1subtype = chop $type) eq "\@"
Truth table66%2/3
A and B
A= $par1type eq "\\"B= ($par1subtype = chop $type) eq "\@"
ABresult
0X0
100
111
269 6 $param[0] = [split /$;/o, $_[0]]
270 } elsif ($par1type eq '@') {
271 0 @param = split /$;/o, $_[0]
272 } else {
273 13 $param[0] = $_[0]
274 }
275 19 ▶ if ($type =~ /^(.)>-$/ or ($type =~ /\*$/ and $_[0] eq $; and (undef @param or 1))) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ /^(.)>-$/ or $type =~ /\*$/ and $_[0] eq $; and undef @param || 1) { } 0 19 19
Condition36%4/11
$type =~ /^(.)>-$/ or $type =~ /\*$/ and $_[0] eq $; and undef @param || 1
l!l&&r!l&&!r
0019
$type =~ /\*$/ and $_[0] eq $; and undef @param || 1
!ll&&!rl&&r
1900
$type =~ /\*$/ and $_[0] eq $;
!ll&&!rl&&r
5140
undef @param or 1
l!l
00
MC/DC0%0/4
$type =~ /^(.)>-$/ or $type =~ /\*$/ and $_[0] eq $; and undef @param || 1
$type =~ /^(.)>-$/ $type =~ /\*$/ $_[0] eq $; undef @param
Truth table40%2/5
A or B and C and D
A= $type =~ /^(.)>-$/B= $type =~ /\*$/C= $_[0] eq $;D= undef @param
ABCDresult
1XXX1
01111
00XX0
010X0
01100
276 0 my $code = $self->[1];
277 0 ▶ if ($1 eq '$') {
Branch: 1 branch
BranchTrueFalseTotal
if ($1 eq '$') { } 0 0 0
278 0 &{$code}(@param);
279 } else {
280 0 join $", &{$code}(@param);
281 }
282 } else {
283 19 ▶ if ($type =~ /\*$/) {$type .= $par1subtype.$par1type}; ##<???>
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ /\*$/) 14 5 19
284 19 my %fakehash;
285 19 tie %fakehash, Interpolation::internal, [$type, $self->[1], @param];
286 19 bless \%fakehash, Interpolation::internal;
287 }
288 }
289
290 package Interpolation::internal;
291
292 4 @Interpolation::internal::ISA = ('Interpolation::base');
293
294 47 ▶sub TIEHASH {
Subroutine TIEHASH: called CC=1 SCAR=0.0
295 47 my ($pack, $self) = @_;
296 47 bless $self => $pack; # That's it? Yup!
297 }
298
299 30 ▶sub FETCH {
Subroutine FETCH: called CC=11 SCAR=25.8
300 30 my $self = $_[0];
301 30 my $par1type = chop ($self->[0]);
302 30 my (@param,$par1subtype);
303 30 ▶ if ($par1type eq '\\' and (($par1subtype = chop($self->[0])) eq '@')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($par1type eq '\\' and ($par1subtype = chop $self->[0]) eq '@') { } 6 24 30
elsif ($par1type eq '@' and not $self->[0] =~ /\*$/) { } 0 24 24
Condition66%2/3
$par1type eq "\\" and ($par1subtype = chop $self->[0]) eq "\@"
!ll&&!rl&&r
2406
MC/DC50%1/2
$par1type eq "\\" and ($par1subtype = chop $self->[0]) eq "\@"
$par1type eq "\\" ($par1subtype = chop $self->[0]) eq "\@"
Truth table66%2/3
A and B
A= $par1type eq "\\"B= ($par1subtype = chop $self->[0]) eq "\@"
ABresult
0X0
100
111
304 6 $param[0] = [split /$;/o, $_[1]]
305 ▶ } elsif ($par1type eq '@' and $self->[0] !~ /\*$/) {
Condition33%1/3
$par1type eq "\@" and not $self->[0] =~ /\*$/
!ll&&!rl&&r
2400
MC/DC0%0/2
$par1type eq "\@" and not $self->[0] =~ /\*$/
$par1type eq "\@" not $self->[0] =~ /\*$/
Truth table33%1/3
A and not(B)
A= $par1type eq "\@"B= not $self->[0] =~ /\*$/
ABresult
0X0
100
111
306 0 @param = split /$;/o, $_[1]
307 } else {
308 24 $param[0] = $_[1]
309 }
310 30 push @$self, @param;
311 30 ▶ if ($self->[0] =~ /^(.)>-$/ or ($self->[0] =~ /\*$/ and $_[1] eq $; and pop @$self)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[0] =~ /^(.)>-$/ or $self->[0] =~ /\*$/ and $_[1] eq $; and pop @$self) { } 2 28 30
Condition66%6/9
$self->[0] =~ /^(.)>-$/ or $self->[0] =~ /\*$/ and $_[1] eq $; and pop @$self
l!l&&r!l&&!r
0228
$self->[0] =~ /\*$/ and $_[1] eq $; and pop @$self
!ll&&!rl&&r
2802
$self->[0] =~ /\*$/ and $_[1] eq $;
!ll&&!rl&&r
0282
MC/DC25%1/4
$self->[0] =~ /^(.)>-$/ or $self->[0] =~ /\*$/ and $_[1] eq $; and pop @$self
$self->[0] =~ /^(.)>-$/ $self->[0] =~ /\*$/ $_[1] eq $; pop @$self
Truth table40%2/5
A or B and C and D
A= $self->[0] =~ /^(.)>-$/B= $self->[0] =~ /\*$/C= $_[1] eq $;D= pop @$self
ABCDresult
1XXX1
01111
00XX0
010X0
01100
312 2 shift @$self;
313 2 my $code = shift @$self;
314 2 ▶ if ($1 eq '$') {
Branch: 1 branch
BranchTrueFalseTotal
if ($1 eq '$') { } 0 2 2
315 0 &{$code}(@$self);
316 } else {
317 2 join $", &{$code}(@$self);
318 }
319 } else {
320 28 my %fakehash;
321 28 ▶ if ($self->[0] =~ /\*$/) {$self->[0] .= $par1subtype.$par1type}; ##<???>
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[0] =~ /\*$/) 28 0 28
322 28 tie %fakehash, Interpolation::internal, $self;
323 28 bless \%fakehash, Interpolation::internal;
324 }
325 }
326
327 4 ▶use overload '""' => sub {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine __ANON__: called CC=2 SCAR=7.5
328 17 my $self = tied(%{$_[0]});
329 17 my ($type, $code, @param) = @$self;
330 17 ▶ if ($type =~ /^\$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ /^\$/) { } 17 0 17
331 17 &{$code}(@param);
332 } else {
333 0 join $", &{$code}(@param);
334 }
335 4 };
336
337 package Interpolation::Scalar;
338
339 4 ▶use Tie::Scalar;
Subroutine BEGIN: called CC=1 SCAR=0.0
340 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
341 4 our @ISA=(Tie::Scalar);
342
343 1 ▶sub TIESCALAR {
Subroutine TIESCALAR: called CC=2 SCAR=7.4
344 1 my $pack = shift;
345 1 my $cref = shift;
346 1 ▶ unless (ref $cref) { # symbolic names not supported
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $cref) 0 1 1
347 0 croak "Builtins not supported for type (void)->\$";
348 }
349 1 bless $cref => $pack; # That's it? Yup!
350 }
351
352 5 ▶sub FETCH {
Subroutine FETCH: called CC=1 SCAR=0.0
353 5 &{$_[0]}(); # For pre-5.004_04 compatibility
354 #$_[0]->($_[1]); # Line of the day?
355 }
356
357 1 ▶sub STORE {
Subroutine STORE: called CC=1 SCAR=0.0
358 1 &{$_[0]}($_[1]); # For pre-5.004_04 compatibility
359 #$_[0]->($_[1]); # Line of the day?
360 }
361
362
363 4 1;
364
365 =head1 NAME
366
367 Interpolation - Arbitrary string interpolation semantics (using tie())
368
369 Version 0.74
370
371 Originaly by Mark-Jason Dominus (mjd-perl-interpolation@plover.com)
372 Since version 0.66 maintained by Jenda@Krynicky.cz
373
374 =head1 SYNOPSIS
375
376 use Interpolation name => \&function, ...;
377 print "la la la la $name{blah blah blah}";
378
379 # This is like doing:
380 $VAR = &function(blah blah blah);
381 print "la la la la $VAR";
382
383 =head1 DESCRIPTION
384
385 Beginners always want to write this:
386
387 print "The sum of three and four is: 3+4";
388
389 And they want the C<3+4> part to be evaluated, so that it prints
390 this:
391
392 The sum of three and four is: 7
393
394 Of course, it's a double-quoted string, so it's not evaluated. The
395 only things that are evaluated in double-quoted strings are variable
396 references.
397
398 There are solutions to this, but most of them are ugly. This module
399 is less ugly. Well .... this module IS ugly, but only inside. Your code may end up being nice.
400
401 The module also lets you define arbitrary interpolation semantics.
402
403 For example, you can say
404
405 use Interpolation money => \&commify_with_dollar_sign,
406 E => 'eval',
407 placename => 'ucwords',
408 ;
409
410 And then you can write these:
411
412 print "3 + 4 = $E{3+4}";
413 # Prints ``3 + 4 = 7''
414
415 $SALARY = 57500;
416 print "The salary is $money{$SALARY}";
417 # Prints ``The salary is $57,500.00''
418
419 $PLACE1 = 'SAN BERNADINO HIGH SCHOOL';
420 $PLACE2 = 'n.y. state';
421 print "$placename{$PLACE1} is not near $placename{$PLACE2}";
422 # Prints ``San Bernadino High School is not near N.Y. State";
423
424 =head1 DETAILS
425
426 The arguments to the C<use> call should be name-function pairs. If
427 the pair is C<($n, $f)>, then C<$n> will be the name for the semantics
428 provided by C<$f>. C<$f> must either be a reference to a function
429 that you supply, or it can be the name of one of the built-in
430 formatting functions provided by this package. C<Interpolation> will
431 take over the C<%n> hash or C<$n> scalar in your package, and tie it so that acessing
432 C<$n{X}> calls C<f(X)> and yields its return value.
433
434 If for some reason you want to, you can add new semantics at run time
435 by using
436
437 import Interpolation name => function, ...
438
439 You can remove them again with
440
441 unimport Interpolation 'name', ...
442
443 Interpolators created by the import() or use statements are always PACKAGE variables, not lexicals!
444 If you want a lexical interpolator you can create it like this:
445
446 my %name;
447 tie %name, 'Interpolation', sub { ...
448 or
449 my %name;
450 tie %name, 'Interpolation', '$$->$', sub { ...
451
452
453 =head2 Built-ins
454
455 C<Interpolation> provides a few useful built-in formatting functions;
456 you can refer to these by name in the C<use> or C<import> line. They are:
457
458 eval - Evaluate the argument
459 null - Same as eval
460 identity - Also the same as eval
461
462 ucwords - Capitalize Input String Like This
463
464 commify - 1428571 => 1,428,571.00
465
466 reverse - reverse string
467
468 sprintf - makes "$S{'%.2f %03d'}{37.5,42}" turn into "37.50 042"
469 use Interpolation S => 'sprintf';
470 print "$S{'%.2f %03d'}{37.5, 42}\n";
471
472 sprintf1 - makes "$S{'%.2f %03d', 37.5,42}" turn into "37.50 042".
473 use Interpolation S => 'sprintf1';
474 print "$S{'%.2f %03d', 37.5, 42}\n";
475
476 sprintfX - makes "$S{'%.2f %03d'}{37.5}{42}" turn into "37.50 042".
477 use Interpolation 'S:$$*->$' => 'sprintfX';
478 print "$S{'%.2f %03d'}{37.5}{42}\n";
479
480 sqlescape - escapes single quotes for use in SQL queries
481
482 round - rounds the number
483 use Interpolation round => 'round'; print "The sum is: $round{$sum, 0.01}\n";
484 use Interpolation 'round:$$->$' => 'round'; print "The sum is: $round{$sum}{0.01}\n";
485
486 htmlescape - escapes characters special to HTML
487 "<b>$htmlescape{$text}</b>
488 tagescape - escapes characters special to HTML plus double and single quotes
489 qq{<input type=text name=foo value="$tagescape{$value}">}
490 jsescape - escapes the text to be used in JavaScript
491 qq{<a href="JavaScript:foo( '$jsescape{$value}' )">}
492 (the last three require module HTML::Entities)
493
494 =head1 ADVANCED
495
496 It is posible to pass multiple (or no) arguments to your function.
497 There are two alternate syntaxes:
498
499 $interpolator{param1,param2}
500 $interpolator{param1}{param2}
501
502 The first syntax will pass both arguments in $_[0] joined by $;, so you have to split them:
503
504 use Interpolation add => sub{@_ = split /$;/o, $_[0]; $_[0] + $_[1]};
505 print "3 + 4 = $add{3,4}\n";
506
507 The other syntax (used for example by builtin 'sprintf') requires quite some magic,
508 so you probably wouldn't want to be forced to write it yourself.
509 (See the source of this module if you want to know how strange is the code. )
510
511 The other problem is, that your interpolator might want to return an array.
512 In that case you would anticipate to get all the items joined by $",
513 but instead you would get only the last item. You have to join the list yourself:
514
515 use Interpolation foo => sub {join $", &bar($_[0])};
516
517 To make your life easier this module provides a way to specify the "type" of the interpolator
518 and then does the necessary splits, joins or magic itself.
519
520 The syntax is:
521
522 use Interpolation 'name:input->output' => sub { ...
523
524 where the input is a list of '$'s, '@'s and '\@'s and the output is either '$' or '@'.
525 The '$' means that the parameter/output should be left intact, while '@'
526 forces a split/join on the parameter/output. Each character in the input list
527 specifies the type of one brace in the call.
528
529 In addition you may add an asterisk
530 to the end of the input type specification. This will allow for an arbitrary long
531 list of parameters. Their type will be the last specified.
532
533 In previous version you had to "close" the interpolator call by $;.
534 That is you would write something like "xxx $foo{par1}{par2}...{parn}{$;} xxx".
535 While this is still suported it is NOT required anymore.
536
537 The default type is $->$.
538
539 Ex.:
540 'foo:$->$' - pass the argument to function directly and evaluate it in scalar context
541 $foo{param}
542 'foo:$->@' - pass the argument to function directly, evaluate it in list context and join
543 the result by $" (by default space)
544 $foo{param}
545 'foo:@->$' - split the first parameter by $; and pass the resulting list to the function,
546 evaluate in scalar context
547 $foo{param1,param2,...}
548 'foo:@->@' - split the first parameter by $; and pass the resulting list to the function,
549 evaluate in list context and join
550 $foo{param1,param2,...}
551 'foo:$$->$' - ask for two parameters enclosed in braces
552 $foo{param1}{param2}
553 'foo:$@->$' - ask for two parameters enclosed in braces and split the second one
554 the list you get from the split will be added to @_ flatlist
555 $foo{paramA}{paramB1,paramB2,...}
556 'foo:$\@->$' - ask for two parameters enclosed in braces and split the second one
557 the list you get from the split will be passed as a reference to an array
558 $foo{paramA}{paramB1,paramB2,...}
559 'foo:$*->$ - ask for arbitrary number of scalar parameters
560 $foo{par1}{par2}{par3}{$;}
561 'foo:->$ - no parameters. This creates a tied scalar.
562 $foo
563
564
565 'foo:@->$' => &bar IS EQUAL TO 'foo' => sub {&bar(split /$;/o, $_[0])}
566 'foo:$->@' => &bar IS EQUAL TO 'foo' => sub {join $", &bar($_[0])}
567 'foo:@->@' => &bar IS EQUAL TO 'foo' => sub {join $", &bar(split /$;/o, $_[0])}
568 'foo:\@->$' => &bar IS EQUAL TO 'foo' => sub {&bar([split /$;/o, $_[0] ])}
569
570 The builtin function sprintf could be implemented as:
571 'sprintf:$@->$' => sub {sprintf shift,@_}
572
573 Since version 0.69 it is possible to assign to interpolators of type '$->$', '$->@' and '->$'.
574 The assigned value will be passed to the function you specified as the last parameter:
575
576 use Interpolation 'count:->$' => sub {if (@_) {$count = $_[0]} else {$count++}};
577 # print "Current count is $count\n";
578 use Interpolation 'count:$->$' => sub {
579 if (@_ == 2) {
580 $count{$_[0]} = $_[1]
581 } else {
582 $count{$_[0]}++
583 }
584 };
585 # print "Current count of A is $count{A}\n";
586
587
588
589 =head1 Cool examples
590
591 =over 2
592
593 =item SQL
594
595 use Interpolation "'" => sub {$_ = $_[0]; s/'/''/g; "'".$_};
596 ...
597 $db->Sql("SELECT * FROM People WHERE LastName = $'{$lastname}'");
598
599 When passing strings to SQL you have to escape the apostrophes
600 (and maybe some other characters) this crazy hack allows you do it quite easily.
601
602 Instead of "... = '$variable'" you write "... = $'{$variable}'" et voila ;-)
603 You may of course use this syntax for whatever string escaping you like.
604
605 =item IF
606
607 use Interpolation 'IF:$*->$' => sub {$_[0] ? $_[1] : $_[2]};
608 #or
609 use Interpolation '?:$*->$' => sub {$_[0] ? $_[1] : $_[2]};
610 #...
611 print <<"*END*"
612 Blah blah blah
613 There was $count $IF{$count > 1}{jobs}{job}.
614 There was $count $?{$count > 1}{jobs}{job}.
615 There was $count job$?{$count > 1}{s}.
616 *END*
617
618 =back
619
620 =head1 Warnings
621
622 It's easy to forget that the index to a C<$hash{...}> is an arbitrary
623 expression, unless it looks like an identifier. There are two gotchas here.
624
625 =over 4
626
627 =item Trap 1.
628
629 print "$X{localtime}";
630
631 Here the C<X> formatter is used to format the literal string
632 C<localtime>; the C<localtime> built-in function is not invoked. If
633 you really want the current time, use one of these:
634
635 print "$X{+localtime}";
636 print "$X{localtime()}";
637
638 =item Trap 2.
639
640 print "$X{What ho?}";
641
642 This won't compile---you get `search pattern not terminated'. Why?
643 Because Perl sees the C<?> and interprets it as the beginning of a
644 pattern match operator, similar to C</>. (Ah, you forgot that C<?>
645 could be a pattern match delimiter even without a leading C<m>, didn't
646 you?) You really need
647
648 print "$X{'What ho?'}";
649
650 =back
651
652 The rule is simple: That thing in the braces that looks like a hash
653 key really is a hash key, and so you need to put it in quotes under
654 the same circumstances that you need to put any other hash key in
655 quotes. You probably wouldn't expect this to work either:
656
657 $V = $X{What ho?};
658
659
660 =head1 Author
661
662 =begin text
663
664 Originaly, Mark-Jason Dominus (C<mjd-perl-interpolation@plover.com>), Plover Systems co.
665 http://www.plover.com/~mjd/perl/Interpolation
666
667 Now maintained by, C<Jenda@Krynicky.cz> .
668 http://Jenda.Krynicky.cz/#Interpolation
669
670 =end text
671
672 =begin man
673
674 Originaly, Mark-Jason Dominus (C<mjd-perl-interpolation@plover.com>), Plover Systems co.
675 http://www.plover.com/~mjd/perl/Interpolation
676
677 Now maintained by, C<Jenda@Krynicky.cz> .
678 http://Jenda.Krynicky.cz/#Interpolation
679
680 =end man
681
682 =begin html
683
684 <p>Originaly, Mark-Jason Dominus (<a href="mailto:mjd-perl-interpolation@plover.com"><tt>mjd-perl-interpolation@plover.com</tt></a>), Plover Systems co.<BR/>
685 <a href="http://www.plover.com/~mjd/perl/Interpolation">http://www.plover.com/~mjd/perl/Interpolation</a></p>
686
687 Now maintained by, <a href="mailto:Jenda@Krynicky.cz">Jenda Krynicky</a>.<BR/>
688 <a href="http://Jenda.Krynicky.cz/#Interpolation">http://Jenda.Krynicky.cz/#Interpolation</a></p>
689
690 =end html
691
692
693 =cut
694
695
↑ Summary