« 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 991 | my $mod_expr = shift @mod_exprs; | |||||||||||||||||||||||||||||||||||||||||||||
| 18 | 991 | ▶ | if ( ref $mod_expr eq 'HASH' ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 1955 | push @blocks, $_; | |||||||||||||||||||||||||||||||||||||||||||||
| 38 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 3353 | push @exprs, $_; | |||||||||||||||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 244 | ▶ | push @blocks, 'UNUSED_CONSTANT' if ( ! scalar @blocks ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 244 | ▶ | push @exprs, 'UNUSED_CONSTANT' if ( ! scalar @exprs ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
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
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
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 59 | length $text and $text =~ s/ $expr_expr / | ||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 2018 | my $substitute = $rules{ $1 }; | |||||||||||||||||||||||||||||||||||||||||||||
| 61 | 2018 | ▶ | if ( ! ref $substitute ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||||||