blib/lib/String/Interpolate/Shell.pm

filter: Statementstmt 91.0% 61 / 67 Branchbran 73.9% 34 / 46 Conditioncond 62.5% 5 / 8 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 100% 11 / 11 Podpod 100% 1 / 1 totaltotal 81.8% 113 / 138 CC 28 SCAR 31.0
CC
28
Cov
83%
strinterp
33.7
_handle_undef
5.0
BEGIN
1.0
CRAP 22.2
↑ Summary
1 package String::Interpolate::Shell;
2  
3 # ABSTRACT: Variable interpolation, shell style
4  
5 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 ▶use Text::Balanced qw[ extract_bracketed extract_multiple extract_quotelike];
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Params::Check qw[ check ];
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 4 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 4 our @EXPORT_OK = qw[ strinterp ];
15  
16 4 our $VERSION = '0.02';
17  
18 21 ▶sub _extract {
Subroutine _extract: called CC=1 SCAR=0.0
19  
20 extract_multiple( $_[0],
21 [
22 qr/\s+/,
23 qr/\\(\\)/,
24 qr/\\(\$)/,
25 { V => qr/\$(\w+)/ },
26 30 ▶ { B => sub { (extract_bracketed( $_[0], '{}', qr/\$/ ))[0] } },
Subroutine __ANON__: called CC=1 SCAR=0.0
27 21 ] );
28 }
29  
30 13 ▶sub _handle_undef {
Subroutine _handle_undef: called CC=5 SCAR=16.1
31  
32 13 my ( $var, $q, $attr, $rep ) = @_;
33  
34 ## no critic(ProhibitAccessOfPrivateData)
35  
36 13 ▶ return $var->{$q} if defined $var->{$q};
Branch: 1 branch
BranchTrueFalseTotal
if defined $var->{$q} 8 5 13
37  
38 4 ▶ no if $] >= 5.022, warnings => 'redundant';
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 carp( sprintf( $attr->{undef_message}, $rep) )
41 5 ▶ if $attr->{undef_verbosity} eq 'warn';
Branch: 1 branch
BranchTrueFalseTotal
if $attr->{"undef_verbosity"} eq "warn" 1 4 5
42  
43 croak( sprintf( $attr->{undef_message}, $rep) )
44 5 ▶ if $attr->{undef_verbosity} eq 'fatal';
Branch: 1 branch
BranchTrueFalseTotal
if $attr->{"undef_verbosity"} eq "fatal" 2 3 5
45  
46 3 ▶ return $rep if $attr->{undef_value} eq 'ignore';
Branch: 1 branch
BranchTrueFalseTotal
if $attr->{"undef_value"} eq "ignore" 2 1 3
47 }
48  
49 21 ▶sub strinterp{
Subroutine strinterp: called CC=24 SCAR=35.2
Pod documented
50  
51 21 my ( $text, $var, $attr ) = @_;
52  
53 ## no critic(ProhibitAccessOfPrivateData)
54 21 ▶ $attr = check( {
Branch: 1 branch
BranchTrueFalseTotal
$attr = check({"undef_value", {"allow", ["ignore", "remove"], "default", "ignore"}, "undef_verbosity", {"allow", ["silent", "warn", "fatal"], "default", "silent"}, "undef_message", {"default", "undefined variable: %s\n"}}, $attr || {}) or croak("error parsing arguments: ", Params::Check::last_error()) 21 0 21
Condition100%2/2
$attr || {}
l!l
714
MC/DC100%1/1
$attr || {}
$attr
Truth table100%2/2
A
A= $attr
Aresult
11
00
55 undef_value => { allow => [ qw[ ignore remove ] ],
56 default => 'ignore' },
57 undef_verbosity => { allow => [ qw[ silent warn fatal ] ],
58 default => 'silent' },
59 undef_message => { default => "undefined variable: %s\n" },
60 }, $attr || {} )
61 or croak( "error parsing arguments: ", Params::Check::last_error() );
62  
63 21 my @matches;
64  
65 21 for my $matchstr ( _extract($text ) ) {
66  
67 25 my $ref = ref $matchstr;
68  
69 25 ▶ if ( 'B' eq $ref ) {
Branch: 3 branches
BranchTrueFalseTotal
if ('B' eq $ref) { } 13 12 25
elsif ('V' eq $ref) { } 6 6 12
elsif ($ref) { } 0 6 6
70  
71 # remove enclosing brackets
72 13 my $match = substr( $$matchstr, 1,-1 );
73  
74 # see if there's a 'shell' modifier expression
75 13 my ( $ind, $q, $modf, $rest ) = $match =~ /^(!)?(\w+)(:[-?=+~:])?(.*)/;
76  
77 # if there's no modifier but there is trailing cruft, it's an error
78 13 ▶ die( "unrecognizeable variable name: \${$match}\n")
Branch: 1 branch
BranchTrueFalseTotal
if not defined $modf and $rest ne "" 0 13 13
Condition66%2/3
not defined $modf and $rest ne ""
!ll&&!rl&&r
940
MC/DC0%0/2
not defined $modf and $rest ne ""
not defined $modf $rest ne ""
Truth table66%2/3
not(A) and B
A= not defined $modfB= $rest ne ""
ABresult
0X0
100
111
79 if ! defined $modf && $rest ne '';
80  
81 # if indirect flag is set, expand variable
82 13 ▶ if ( defined $ind ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ind) 1 12 13
83  
84 1 ▶ if ( defined $var->{$q} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $var->{$q}) { } 1 0 1
85 1 $q = $var->{$q};
86 }
87 else
88 {
89 0 push @matches, _handle_undef( $attr, '$' . $$matchstr );
90 0 next;
91 }
92 }
93  
94  
95 13 ▶ if ( ! defined $modf ) {
Branch: 7 branches
BranchTrueFalseTotal
if (not defined $modf) { } 4 9 13
elsif (':?' eq $modf) { } 2 7 9
elsif (':-' eq $modf) { } 1 6 7
elsif (':=' eq $modf) { } 0 6 6
elsif (':+' eq $modf) { } 2 4 6
elsif ('::' eq $modf) { } 1 3 4
elsif (':~' eq $modf) { } 3 0 3
96  
97 4 push @matches, _handle_undef( $var, $q, $attr, '$' . $$matchstr );
98 }
99  
100 elsif ( ':?' eq $modf ) {
101  
102 2 local $attr->{undef_verbosity} = 'fatal';
103 2 local $attr->{undef_message} = $rest;
104  
105 2 push @matches, _handle_undef( $var, $q, $attr, '$' . $$matchstr );
106  
107 }
108 elsif ( ':-' eq $modf ) {
109  
110 1 ▶ push @matches, defined $var->{$q} ? $var->{$q} : strinterp( $rest, $var, $attr );
Branch: 1 branch
BranchTrueFalseTotal
defined $var->{$q} ? : 0 1 1
111  
112 }
113  
114 elsif ( ':=' eq $modf ) {
115  
116  
117 $var->{$q} = strinterp( $rest, $var, $attr )
118 0 ▶ unless defined $var->{$q};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $var->{$q} 0 0 0
119 0 push @matches, $var->{$q};
120  
121 }
122  
123 elsif ( ':+' eq $modf ) {
124  
125 push @matches, strinterp( $rest, $var, $attr )
126 2 ▶ if defined $var->{$q};
Branch: 1 branch
BranchTrueFalseTotal
if defined $var->{$q} 2 0 2
127  
128 }
129  
130 elsif ( '::' eq $modf ) {
131  
132 1 push @matches, sprintf( $rest, _handle_undef( $var, $q, $attr, '$' . $$matchstr ) );
133  
134 }
135  
136 elsif ( ':~' eq $modf ) {
137  
138 3 my ( $expr, $xtra, $op ) = (extract_quotelike( $rest ))[0,1,3];
139 3 ▶ die( "unable to parse variable substitution command: $rest\n" )
Branch: 1 branch
BranchTrueFalseTotal
unless $xtra =~ /^\s*$/ and $op =~ /^(s|tr|y)$/ 0 3 3
Condition33%1/3
$xtra =~ /^\s*$/ and $op =~ /^(s|tr|y)$/
!ll&&!rl&&r
003
MC/DC0%0/2
$xtra =~ /^\s*$/ and $op =~ /^(s|tr|y)$/
$xtra =~ /^\s*$/ $op =~ /^(s|tr|y)$/
Truth table33%1/3
A and B
A= $xtra =~ /^\s*$/B= $op =~ /^(s|tr|y)$/
ABresult
0X0
100
111
140 if $xtra !~ /^\s*$/ or $op !~ /^(s|tr|y)$/;
141  
142 3 my $t = $var->{$q};
143 ## no critic(ProhibitStringyEval)
144 3 eval "\$t =~ $expr";
145 3 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 3 3
146  
147 3 push @matches, $t;
148  
149 }
150  
151 0 else { die( "internal error" ) }
152  
153 }
154 elsif ( 'V' eq $ref ) {
155  
156 6 my $q = $$matchstr;
157  
158 6 push @matches, _handle_undef( $var, $q, $attr, "\$$q" );
159  
160 }
161  
162 elsif ( $ref ) {
163  
164 0 push @matches, $$matchstr;
165  
166 }
167 else {
168  
169 6 push @matches, $matchstr;
170  
171 }
172  
173  
174 }
175  
176 19 return join('', @matches);
177  
178 }
179  
180 4 1;
181 #
182 # This file is part of String-Interpolate-Shell
183 #
184 # This software is Copyright (c) 2017 by Smithsonian Astrophysical Observatory.
185 #
186 # This is free software, licensed under:
187 #
188 # The GNU General Public License, Version 3, June 2007
189 #
190  
191 =pod
192  
193 =head1 NAME
194  
195 String::Interpolate::Shell - Variable interpolation, shell style
196  
197 =head1 VERSION
198  
199 version 0.02
200  
201 =head1 SYNOPSIS
202  
203 use String::Interpolate::Shell qw[ strinterp ];
204  
205 $interpolated_text = strinterp( $text, \%var, \%attr );
206  
207 =head1 DESCRIPTION
208  
209 B<String::Interpolate::Shell> interpolates variables into strings.
210 Variables are specified using a syntax similar to that use by B<bash>.
211 Undefined variables can be silently ignored, removed from the string,
212 can cause warnings to be issued or errors to be thrown.
213  
214 =over
215  
216 =item $I<varname>
217  
218 Insert the value of the variable.
219  
220 =item ${I<varname>}
221  
222 Insert the value of the variable.
223  
224 =item ${I<varname>:?error message}
225  
226 Insert the value of the variable. If it is not defined,
227 the routine croaks with the specified message.
228  
229 =item ${I<varname>:-I<default text>}
230  
231 Insert the value of the variable. If it is not defined,
232 process the specified default text for any variable interpolations and
233 insert the result.
234  
235 =item ${I<varname>:+I<default text>}
236  
237 If the variable is defined, insert the result of interpolating
238 any variables into the default text.
239  
240 =item ${I<varname>:=I<default text>}
241  
242 Insert the value of the variable. If it is not defined,
243 insert the result of interpolating any variables into the default text
244 and set the variable to the same value.
245  
246 =item ${I<varname>::I<format>}
247  
248 Insert the value of the variable as formatted according to the
249 specified B<sprintf> compatible format.
250  
251 =item ${I<varname>:~I<op>/I<pattern>/I<replacement>/msixpogce}
252  
253 Insert the modified value of the variable. The modification is
254 specified by I<op>, which may be any of C<s>, C<tr>, or C<y>,
255 corresponding to the Perl operators of the same name. Delimiters for
256 the modification may be any of those recognized by Perl. The
257 modification is performed using a Perl string B<eval>.
258  
259 =back
260  
261 In any of the bracketed forms, if the variable name is preceded with an exclamation mark (C<!>)
262 the name of the variable to be interpreted is taken from the value of the specified variable.
263  
264 =head1 FUNCTIONS
265  
266 =over
267  
268 =item strinterp
269  
270 $interpolated_text = strinterp( $template, \%var, \%attr );
271  
272 Return a string containing a copy of C<$template> with variables interpolated.
273  
274 C<%var> contains the variable names and values.
275  
276 C<%attr> may contain the following entries:
277  
278 =over
279  
280 =item undef_value
281  
282 This indicates how undefined variables should be interpolated
283  
284 =over
285  
286 =item C<ignore>
287  
288 Ignore them. The token in C<$text> is left as is.
289  
290 =item C<remove>
291  
292 Remove the token from C<$text>.
293  
294 =back
295  
296 =item undef_verbosity
297  
298 This indicates how undefined variables should be reported.
299  
300 =over
301  
302 =item C<silent>
303  
304 No message is returned.
305  
306 =item C<warn>
307  
308 A message is output via C<carp()>.
309  
310 =item C<fatal>
311  
312 A message is output via C<croak()>.
313  
314 =back
315  
316 =back
317  
318 =back
319  
320 =head1 BUGS AND LIMITATIONS
321  
322 You can make new bug reports, and view existing ones, through the
323 web interface at L<https://rt.cpan.org/Public/Dist/Display.html?Name=String-Interpolate-Shell>.
324  
325 =head1 AUTHOR
326  
327 Diab Jerius <djerius@cpan.org>
328  
329 =head1 COPYRIGHT AND LICENSE
330  
331 This software is Copyright (c) 2017 by Smithsonian Astrophysical Observatory.
332  
333 This is free software, licensed under:
334  
335 The GNU General Public License, Version 3, June 2007
336  
337 =cut
338  
339 __END__
↑ Summary