blib/lib/Class/MakeMethods/Utility/TextBuilder.pm

filter: Statementstmt 83.6% 41 / 49 Branchbran 63.3% 19 / 30 Conditioncond 83.3% 5 / 6 MC/DCmcdc 66.6% 2 / 3 Subroutinesub 100% 4 / 4 Podpod 0.0% 0 / 1 totaltotal 76.3% 71 / 93 CC 19 SCAR 29.8
CC
19
Cov
76%
text_builder
23.7
import
2.1
BEGIN
1.0
CRAP 19.7
« blib/lib/Class/MakeMethods/Template/StructBuiltin.pm ↑ Summary blib/lib/Class/MakeMethods/Basic/Hash.pm »
1 package Class::MakeMethods::Utility::TextBuilder;
2  
3 62 $VERSION = 1.008;
4  
5 62 @EXPORT_OK = qw( text_builder );
6 2 ▶sub import { require Exporter and goto &Exporter::import } # lazy Exporter
Subroutine import: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
require Exporter and goto \&Exporter::import 2 0 2
7  
8 62 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 62 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # $expanded_text = text_builder( $base_text, @exprs )
12 244 ▶sub text_builder {
Subroutine text_builder: called CC=18 SCAR=31.7
Pod undocumented
13 244 my ( $text, @mod_exprs ) = @_;
14
15 244 my @code_exprs;
16 244 ▶ while ( scalar @mod_exprs ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @mod_exprs) 991 244 1235
17 991 my $mod_expr = shift @mod_exprs;
18 991 ▶ if ( ref $mod_expr eq 'HASH' ) {
Branch: 4 branches
BranchTrueFalseTotal
if (ref $mod_expr eq 'HASH') { } 248 743 991
elsif (ref $mod_expr eq 'ARRAY') { } 108 635 743
elsif (ref $mod_expr eq 'CODE') { } 0 635 635
elsif (not ref $_) { } 635 0 635
19 248 push @code_exprs, %$mod_expr;
20 } elsif ( ref $mod_expr eq 'ARRAY' ) {
21 108 unshift @mod_exprs, @$mod_expr;
22 } elsif ( ref $mod_expr eq 'CODE' ) {
23 0 $text = &$mod_expr( $text );
24 } elsif ( ! ref $_ ) {
25 635 $mod_expr =~ s{\*}{$text}g;
26 635 $text = $mod_expr;
27 } else {
28 0 Carp::confess "Wierd contents of modifier array.";
29 }
30 }
31 244 my %rules = @code_exprs;
32
33 244 my @exprs;
34 my @blocks;
35 244 foreach ( sort { length($b) <=> length($a) } keys %rules ) {
36 5308 ▶ if ( s/\{\}\Z// ) {
Branch: 1 branch
BranchTrueFalseTotal
if (s/\{\}\Z//) { } 1955 3353 5308
37 1955 push @blocks, $_;
38 } else {
39 3353 push @exprs, $_;
40 }
41 }
42 244 ▶ push @blocks, 'UNUSED_CONSTANT' if ( ! scalar @blocks );
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @blocks 0 244 244
43 244 ▶ push @exprs, 'UNUSED_CONSTANT' if ( ! scalar @exprs );
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @exprs 0 244 244
44
45 # There has *got* to be a better way to regex matched brackets... Right?
46 # Erm, well, no. It looks like Text::Balanced would do the trick, with the
47 # requirement that the below bit get re-written to not be regex-based.
48 244 my $expr_expr = '\b(' . join('|', map "\Q$_\E", @exprs ) . ')\b';
49 244 my $block_expr = '\b(' . join('|', map "\Q$_\E", @blocks ) . ') \{
50 ( [^\{\}]*
51 (?: \{
52 [^\{\}]*
53 (?: \{ [^\{\}]* \} [^\{\}]* )*?
54 \} [^\{\}]* )*?
55 )
56 \}';
57
58 244 ▶ 1 while (
Branch: 1 branch
BranchTrueFalseTotal
length $text && $text =~ s/ $expr_expr /my $substitute = $rules{$1}; if (not ref $substitute) { $substitute; } elsif (ref $substitute eq "CODE") { &{$substitute;}(); } else { croak("Unknown type of substitution rule: '${substitute}'"); } ;/egsx || $text =~ s[ $block_expr ][my $substitute = $rules{$1 . '{}'}; my $contents = $2; if (not ref $substitute) { $substitute =~ s/\*/$contents/g; $substitute; } elsif (ref $substitute eq "HASH") { $substitute->{$contents}; } elsif (ref $substitute eq "CODE") { &{$substitute;}($contents); } else { croak("Unknown type of substitution rule: '${substitute}'"); } ;]egsx and do { '???' } 1403 244 1647
Condition83%5/6
length $text and $text =~ s/ $expr_expr /my $substitute = $rules{$1}; if (not ref $substitute) { $substitute; } elsif (ref $substitute eq "CODE") { &{$substitute;}(); } else { croak("Unknown type of substitution rule: '${substitute}'"); } ;/egsx or $text =~ s[ $block_expr ][my $substitute = $rules{$1 . '{}'}; my $contents = $2; if (not ref $substitute) { $substitute =~ s/\*/$contents/g; $substitute; } elsif (ref $substitute eq "HASH") { $substitute->{$contents}; } elsif (ref $substitute eq "CODE") { &{$substitute;}($contents); } else { croak("Unknown type of substitution rule: '${substitute}'"); } ;]egsx
l!l&&r!l&&!r
943460244
length $text and $text =~ s/ $expr_expr /my $substitute = $rules{$1}; if (not ref $substitute) { $substitute; } elsif (ref $substitute eq "CODE") { &{$substitute;}(); } else { croak("Unknown type of substitution rule: '${substitute}'"); } ;/egsx
!ll&&!rl&&r
0704943
MC/DC66%2/3
length $text and $text =~ s/ $expr_expr /my $substitute = $rules{$1}; if (not ref $substitute) { $substitute; } elsif (ref $substitute eq "CODE") { &{$substitute;}(); } else { croak("Unknown type of substitution rule: '${substitute}'"); } ;/egsx or $text =~ s[ $block_expr ][my $substitute = $rules{$1 . '{}'}; my $contents = $2; if (not ref $substitute) { $substitute =~ s/\*/$contents/g; $substitute; } elsif (ref $substitute eq "HASH") { $substitute->{$contents}; } elsif (ref $substitute eq "CODE") { &{$substitute;}($contents); } else { croak("Unknown type of substitution rule: '${substitute}'"); } ;]egsx
length $text $text =~ s/ $expr_expr /my $substitute = $rules{$1}; if (not ref $substitute) { $substitute; } elsif (ref $substitute eq "CODE") { &{$substitute;}(); } else { croak("Unknown type of substitution rule: '${substitute}'"); } ;/egsx $text =~ s[ $block_expr ][my $substitute = $rules{$1 . '{}'}; my $contents = $2; if (not ref $substitute) { $substitute =~ s/\*/$contents/g; $substitute; } elsif (ref $substitute eq "HASH") { $substitute->{$contents}; } elsif (ref $substitute eq "CODE") { &{$substitute;}($contents); } else { croak("Unknown type of substitution rule: '${substitute}'"); } ;]egsx
Truth table60%3/5
A and B or C
A= length $textB= $text =~ s/ $expr_expr /my $substitute = $rules{$1}; if (not ref $substitute) { $substitute; } elsif (ref $substitute eq "CODE") { &{$substitute;}(); } else { croak("Unknown type of substitution rule: '${substitute}'"); } ;/egsxC= $text =~ s[ $block_expr ][my $substitute = $rules{$1 . '{}'}; my $contents = $2; if (not ref $substitute) { $substitute =~ s/\*/$contents/g; $substitute; } elsif (ref $substitute eq "HASH") { $substitute->{$contents}; } elsif (ref $substitute eq "CODE") { &{$substitute;}($contents); } else { croak("Unknown type of substitution rule: '${substitute}'"); } ;]egsx
ABCresult
11X1
0X11
1011
0X00
1000
59 length $text and $text =~ s/ $expr_expr /
60 2018 my $substitute = $rules{ $1 };
61 2018 ▶ if ( ! ref $substitute ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $substitute) { } 2018 0 2018
elsif (ref $substitute eq 'CODE') { } 0 0 0
62 2018 $substitute;
63 } elsif ( ref $substitute eq 'CODE' ) {
64 0 &{ $substitute }();
65 } else {
66 0 croak "Unknown type of substitution rule: '$substitute'";
67 }
68 /gesx or $text =~ s/ $block_expr /
69 1168 my $substitute = $rules{ $1 . '{}' };
70 1168 my $contents = $2;
71 1168 ▶ if ( ! ref $substitute ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not ref $substitute) { } 1097 71 1168
elsif (ref $substitute eq 'HASH') { } 71 0 71
elsif (ref $substitute eq 'CODE') { } 0 0 0
72 1097 $substitute =~ s{\*}{$contents}g;
73 1097 $substitute;
74 } elsif ( ref $substitute eq 'HASH' ) {
75 71 $substitute->{$contents};
76 } elsif ( ref $substitute eq 'CODE' ) {
77 0 &{ $substitute }( $contents );
78 } else {
79 0 croak "Unknown type of substitution rule: '$substitute'";
80 }
81 /gesx
82 );
83
84 244 return $text;
85 }
86  
87 62 1;
88  
89 __END__
« blib/lib/Class/MakeMethods/Template/StructBuiltin.pm ↑ Summary blib/lib/Class/MakeMethods/Basic/Hash.pm »