blib/lib/String/ExpandEscapes.pm

filter: Statementstmt 42.2% 19 / 45 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 66.6% 4 / 6 Podpod 50.0% 1 / 2 totaltotal 32.4% 24 / 74 CC 10 SCAR 34.6
CC
10
Cov
30%
expand
56.0
expand_handler
20.0
BEGIN
1.0
CRAP 31.7
↑ Summary
1 package String::ExpandEscapes;
2 #
3 # String::ExpandEscapes - Expand printf-style %-escapes in a string.
4 #
5 # Copyright (c) 2003 Matthias Friedrich <matt@mafr.de>.
6 #
7 # $Id: ExpandEscapes.pm,v 1.1.1.1 2003/04/03 19:10:10 matthias Exp $
8 #
9  
10 1 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 require Exporter;
16  
17 #
18 # Change version number for each release!
19 #
20 1 our $VERSION = '0.01';
21  
22 1 our @ISA = qw(Exporter);
23  
24 #
25 # This allows declaration use String::ExpandEscapes ':all';
26 #
27 1 our %EXPORT_TAGS = (
28 'all' => [ qw(expand expand_handler) ]
29 );
30  
31 1 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
32  
33 # Export nothing by default.
34 #
35 1 our @EXPORT = qw(
36 );
37  
38  
39 #
40 # This is the default substitution function.
41 #
42 sub expand_handler($$$$$)
43 0 ▶{
Subroutine expand_handler: not called CC=4 SCAR=30.0
Pod undocumented
44 0 my $flags = shift; # left or right alignment
45 0 my $width = shift; # maximum field width
46 0 my $precision = shift; # precision
47 0 my $format = shift; # key for the conversation table
48 0 my $table = shift; # conversation table (hash reference)
49  
50 0 ▶ $precision = ".$precision" if $precision ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $precision ne "" 0 0 0
51  
52 # rewrite '%%' to '%'
53 0 ▶ return '%' if $format eq '%';
Branch: 1 branch
BranchTrueFalseTotal
if $format eq "%" 0 0 0
54  
55 # check if we have a rewrite rule for this format specifier
56 0 ▶ return undef unless defined $table->{ $format };
Branch: 1 branch
BranchTrueFalseTotal
unless defined $table->{$format} 0 0 0
57  
58 # everything worked fine, now return the string
59 0 return sprintf("%${flags}${width}${precision}s", $table->{ $format } );
60 }
61  
62  
63 #
64 # expand()
65 #
66 # Expand printf-style escape sequences in a string according to a
67 # given conversation table.
68 #
69 # Arguments: source string
70 # hash reference to the conversation table or code ref
71 # optional user data that is passed to each handler call
72 #
73 # Returns: destination string
74 #
75 # Examples: expand('%s', \&handler, ...)
76 # expand('%s', \%table)
77 #
78 sub expand($$@)
79 0 ▶{
Subroutine expand: not called CC=7 SCAR=40.3
Pod documented
80 0 my $str = shift; # The string containing the escape sequences.
81 0 my $arg = shift; # First argument: either HASH or CODE.
82 0 my $handler; # Code reference for a handler function.
83 my @user_data; # Arguments that are passed to the handler.
84  
85 #
86 # two possibilities:
87 # 1. handler is code reference
88 # 2. handler is hash reference
89 #
90 0 ▶ if ( ref $arg eq 'CODE' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $arg eq 'CODE') { } 0 0 0
elsif (ref $arg eq 'HASH') { } 0 0 0
91 0 $handler = $arg;
92 0 @user_data = @_;
93 }
94 elsif ( ref $arg eq 'HASH' ) {
95  
96 # If called with a hash reference, no further arguments are
97 # allowed
98 #
99 0 ▶ if ( @_ != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ != 0) 0 0 0
100 0 croak 'expand called in table mode with '
101 . 'to many arguments';
102 }
103 0 $handler = \&expand_handler;
104 0 @user_data = $arg;
105 }
106 else {
107 0 croak 'expand called with an argument that is '
108 . 'neither a code nor a hash reference';
109 }
110  
111 #
112 # Parse the format string. The code below is executed for each match.
113 #
114 0 $str =~ s/ %
115 ([- +0#]*) # flags
116 (\d*) # minimum field width aka width
117 (\.?)
118 (\d*) # maximum field width aka precision
119 (.) # format selection
120 /
121 # Special case: "%-10.s" means "%-10.0s".
122 #
123 0 ▶ my $prec = ( $3 eq '.' and $4 eq '' ) ? 0 : $4;
Branch: 1 branch
BranchTrueFalseTotal
$3 eq '.' && $4 eq '' ? : 0 0 0
Condition0%0/3
$3 eq "." and $4 eq ""
!ll&&!rl&&r
000
MC/DC0%0/2
$3 eq "." and $4 eq ""
$3 eq "." $4 eq ""
Truth table0%0/3
A and B
A= $3 eq "."B= $4 eq ""
ABresult
0X0
100
111
124  
125 # The handler returns the string to substitute.
126 #
127 0 my $result = &$handler($1, $2, $prec, $5, @user_data);
128  
129 # Leave the function if the handler returned an error.
130 #
131 0 ▶ return (undef, $&) unless defined $result;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $result 0 0 0
132  
133 0 $result; # Do the replacement.
134 /gesx;
135  
136 #
137 # We got here without errors, return the string.
138 #
139 #return $str;
140 0 return ($str, 0);
141 }
142  
143  
144 1 1;
145  
146 __END__
↑ Summary