| 1 | package String::Expando; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use vars qw($VERSION); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 4 | $VERSION = '0.08'; | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 3 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 11 | 3 | my $cls = shift; | |||||||||||||||||||||||||||
| 12 | 3 | my $self = bless { @_ }, $cls; | |||||||||||||||||||||||||||
| 13 | 3 | $self->init; | |||||||||||||||||||||||||||
| 14 | } | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 3 | ▶ | sub init { | ||||||||||||||||||||||||||
|
Subroutine
init: called CC=17 SCAR=36.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 17 | 3 | my ($self) = @_; | |||||||||||||||||||||||||||
| 18 | 3 | ▶ | if (defined $self->{'expando'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 19 | 0 | my $rx = qr/$self->{'expando'}/; | |||||||||||||||||||||||||||
| 20 | 0 | ▶ | $self->{'consume_expando'} = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 21 | 0 | ▶ | $_ =~ s/\A$rx// ? (defined $2 ? $2 : $1) : () | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||
| 23 | 0 | } | |||||||||||||||||||||||||||
| 24 | else { | ||||||||||||||||||||||||||||
| 25 | 79 | ▶ | $self->{'consume_expando'} ||= sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 26 | 79 | ▶ | s{ \A \% ([^%()]*) \( ([^\s()]+) \) }{}x | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | ? ($2, $1) | ||||||||||||||||||||||||||||
| 28 | : () | ||||||||||||||||||||||||||||
| 29 | 3 | ▶ | }; | ||||||||||||||||||||||||||
|
Condition33%1/3
$self->{"consume_expando"} ||= sub {
s/ \A \% ([^%()]*) \( ([^\s()]+) \) //x ? ($2, $1) : ();
}
MC/DC0%0/2
$self->{"consume_expando"} ||= sub {
s/ \A \% ([^%()]*) \( ([^\s()]+) \) //x ? ($2, $1) : ();
}
$self->{"consume_expando"}
sub {
s/ \A \% ([^%()]*) \( ([^\s()]+) \) //x ? ($2, $1) : ();
}
Truth table33%1/3
A ||= B
A= $self->{"consume_expando"}B= sub {
s/ \A \% ([^%()]*) \( ([^\s()]+) \) //x ? ($2, $1) : ();
}
| |||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | 3 | ▶ | if (defined $self->{'literal'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | 0 | my $rx = qr/$self->{'literal'}/; | |||||||||||||||||||||||||||
| 33 | 0 | ▶ | $self->{'consume_literal'} = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 34 | 0 | ▶ | s/\A$rx// ? ($1) : () | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | 0 | } | |||||||||||||||||||||||||||
| 37 | else { | ||||||||||||||||||||||||||||
| 38 | 47 | ▶ | $self->{'consume_literal'} ||= sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
| |||||||||||||||||||||||||||||
| 39 | 47 | ▶ | s{\A(.)}{}s ? ($1) : () | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | 3 | ▶ | } | ||||||||||||||||||||||||||
|
Condition33%1/3
$self->{"consume_literal"} ||= sub {
s/\A(.)//s ? $1 : ();
}
MC/DC0%0/2
$self->{"consume_literal"} ||= sub {
s/\A(.)//s ? $1 : ();
}
$self->{"consume_literal"}
sub {
s/\A(.)//s ? $1 : ();
}
Truth table33%1/3
A ||= B
A= $self->{"consume_literal"}B= sub {
s/\A(.)//s ? $1 : ();
}
| |||||||||||||||||||||||||||||
| 42 | 3 | ▶ | if (defined $self->{'escaped_literal'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | 0 | my $rx = qr/$self->{'escaped_literal'}/; | |||||||||||||||||||||||||||
| 44 | 0 | ▶ | $self->{'consume_escaped_literal'} = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 45 | 0 | ▶ | $_ =~ s/\A$rx// ? ($1) : () | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | 0 | } | |||||||||||||||||||||||||||
| 48 | else { | ||||||||||||||||||||||||||||
| 49 | 49 | ▶ | $self->{'consume_escaped_literal'} ||= sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 50 | 49 | ▶ | s{\A\\(.)}{} ? ($1) : () | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | 3 | ▶ | } | ||||||||||||||||||||||||||
|
Condition33%1/3
$self->{"consume_escaped_literal"} ||= sub {
s/\A\\(.)// ? $1 : ();
}
MC/DC0%0/2
$self->{"consume_escaped_literal"} ||= sub {
s/\A\\(.)// ? $1 : ();
}
$self->{"consume_escaped_literal"}
sub {
s/\A\\(.)// ? $1 : ();
}
Truth table33%1/3
A ||= B
A= $self->{"consume_escaped_literal"}B= sub {
s/\A\\(.)// ? $1 : ();
}
| |||||||||||||||||||||||||||||
| 53 | 3 | ▶ | if (defined $self->{'dot_separator'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 1 | my $dot = $self->{'dot_separator'}; | |||||||||||||||||||||||||||
| 55 | 1 | my $rx; | |||||||||||||||||||||||||||
| 56 | 1 | ▶ | if (ref($dot) eq '') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 57 | 1 | $rx = qr/\Q$dot\E/; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | elsif (ref($dot) eq 'Regexp') { | ||||||||||||||||||||||||||||
| 60 | 0 | $rx = $dot; | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | else { | ||||||||||||||||||||||||||||
| 63 | 0 | $rx = qr/$dot/; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | 1 | ▶ | my $part_decoder = $self->{'decoder'} || \&decode; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->{'decoder'} || \&String::Expando::decode
MC/DC0%0/1
$self->{'decoder'} || \&String::Expando::decode
$self->{'decoder'}
Truth table50%1/2
A
A= $self->{'decoder'}
| |||||||||||||||||||||||||||||
| 66 | 9 | ▶ | $self->{'decoder'} ||= sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 67 | 9 | my ($self, $code, $stash) = @_; | |||||||||||||||||||||||||||
| 68 | 9 | my @parts = split $rx, $code; | |||||||||||||||||||||||||||
| 69 | 9 | my $val = $stash; | |||||||||||||||||||||||||||
| 70 | 9 | foreach my $part (@parts) { | |||||||||||||||||||||||||||
| 71 | 17 | $val = $part_decoder->($self, $part, $val); | |||||||||||||||||||||||||||
| 72 | 17 | ▶ | last if !defined $val; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | 9 | return $val; | |||||||||||||||||||||||||||
| 75 | 1 | ▶ | }; | ||||||||||||||||||||||||||
|
Condition33%1/3
$self->{"decoder"} ||= sub {
my($self, $code, $stash) = @_;
my @parts = split(/$rx/, $code, 0);
my $val = $stash;
foreach my $part (@parts) {
$val = &$part_decoder($self, $part, $val);
last unless defined $val;
};
return $val;
}
MC/DC0%0/2
$self->{"decoder"} ||= sub {
my($self, $code, $stash) = @_;
my @parts = split(/$rx/, $code, 0);
my $val = $stash;
foreach my $part (@parts) {
$val = &$part_decoder($self, $part, $val);
last unless defined $val;
};
return $val;
}
$self->{"decoder"}
sub {
my($self, $code, $stash) = @_;
my @parts = split(/$rx/, $code, 0);
my $val = $stash;
foreach my $part (@parts) {
$val = &$part_decoder($self, $part, $val);
last unless defined $val;
};
return $val;
}
Truth table33%1/3
A ||= B
A= $self->{"decoder"}B= sub {
my($self, $code, $stash) = @_;
my @parts = split(/$rx/, $code, 0);
my $val = $stash;
foreach my $part (@parts) {
$val = &$part_decoder($self, $part, $val);
last unless defined $val;
};
return $val;
}
| |||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | else { | ||||||||||||||||||||||||||||
| 78 | 2 | ▶ | $self->{'decoder'} ||= \&decode; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"decoder"} ||= \&String::Expando::decode
MC/DC0%0/1
$self->{"decoder"} ||= \&String::Expando::decode
$self->{"decoder"}
Truth table50%1/2
A
A= $self->{"decoder"}
| |||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | 3 | ▶ | $self->{'stringify'} ||= \&stringify; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"stringify"} ||= \&String::Expando::stringify
MC/DC0%0/1
$self->{"stringify"} ||= \&String::Expando::stringify
$self->{"stringify"}
Truth table50%1/2
A
A= $self->{"stringify"}
| |||||||||||||||||||||||||||||
| 81 | 3 | ▶ | $self->{'stash'} ||= {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"stash"} ||= {}
MC/DC0%0/1
$self->{"stash"} ||= {}
$self->{"stash"}
Truth table50%1/2
A
A= $self->{"stash"}
| |||||||||||||||||||||||||||||
| 82 | 3 | ▶ | $self->{'functions'} ||= {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"functions"} ||= {}
MC/DC0%0/1
$self->{"functions"} ||= {}
$self->{"functions"}
Truth table50%1/2
A
A= $self->{"functions"}
| |||||||||||||||||||||||||||||
| 83 | 3 | ▶ | $self->{'default_hash_keys'} ||= [q{}, q{""}, q{'}]; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->{"default_hash_keys"} ||= ["", "\"\"", "'"]
MC/DC100%1/1
$self->{"default_hash_keys"} ||= ["", "\"\"", "'"]
$self->{"default_hash_keys"}
Truth table100%2/2
A
A= $self->{"default_hash_keys"}
| |||||||||||||||||||||||||||||
| 84 | 3 | return $self; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 0 | ▶ | sub stash { @_ > 1 ? $_[0]->{'stash'} = $_[1] : $_[0]->{'stash'} } | ||||||||||||||||||||||||||
|
Subroutine
stash: not called CC=2 SCAR=17.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 0 | ▶ | sub functions { @_ > 1 ? $_[0]->{'functions'} = $_[1] : $_[0]->{'functions'} } | ||||||||||||||||||||||||||
|
Subroutine
functions: not called CC=2 SCAR=17.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 4 | ▶ | sub default_hash_keys { @_ > 1 ? $_[0]->{'default_hash_keys'} = $_[1] : $_[0]->{'default_hash_keys'} } | ||||||||||||||||||||||||||
|
Subroutine
default_hash_keys: called CC=2 SCAR=7.6
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 22 | ▶ | sub expand { | ||||||||||||||||||||||||||
|
Subroutine
expand: called CC=9 SCAR=23.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 92 | 22 | my ($self, $str, $stash) = @_; | |||||||||||||||||||||||||||
| 93 | 22 | ▶ | $stash ||= $self->{'stash'}; | ||||||||||||||||||||||||||
|
Condition33%1/3
$stash ||= $self->{"stash"}
MC/DC0%0/2
$stash ||= $self->{"stash"}
$stash
$self->{"stash"}
Truth table33%1/3
A ||= B
A= $stashB= $self->{"stash"}
| |||||||||||||||||||||||||||||
| 94 | 22 | my $mat = $self->{'consume_expando'}; | |||||||||||||||||||||||||||
| 95 | 22 | my $lit = $self->{'consume_literal'}; | |||||||||||||||||||||||||||
| 96 | 22 | my $esc = $self->{'consume_escaped_literal'}; | |||||||||||||||||||||||||||
| 97 | 22 | my $dec = $self->{'decoder'}; | |||||||||||||||||||||||||||
| 98 | 22 | my $sfy = $self->{'stringify'}; | |||||||||||||||||||||||||||
| 99 | 22 | my $out = ''; | |||||||||||||||||||||||||||
| 100 | 22 | local $_ = $str; | |||||||||||||||||||||||||||
| 101 | 22 | ▶ | while (length $_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 79 | my $res; | |||||||||||||||||||||||||||
| 103 | 79 | ▶ | if (my ($code, $fmt) = $mat->()) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 104 | 17 | my $val = $dec->($self, $code, $stash); | |||||||||||||||||||||||||||
| 105 | 17 | $res = $sfy->($self, $val); | |||||||||||||||||||||||||||
| 106 | 17 | ▶ | $res = sprintf($fmt, $res) if defined $fmt && length $fmt; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $fmt and length $fmt
MC/DC0%0/2
defined $fmt and length $fmt
defined $fmt
length $fmt
Truth table33%1/3
A and B
A= defined $fmtB= length $fmt
| |||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | elsif (s/\A(\s+)//) { | ||||||||||||||||||||||||||||
| 109 | 13 | $res = $1; | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | ▶ | elsif (defined ($res = &$esc) || defined ($res = &$lit)) { | |||||||||||||||||||||||||||
|
Condition66%2/3
defined($res = &$esc) or defined($res = &$lit)
MC/DC0%0/2
defined($res = &$esc) or defined($res = &$lit)
defined($res = &$esc)
defined($res = &$lit)
Truth table66%2/3
A or B
A= defined($res = &$esc)B= defined($res = &$lit)
| |||||||||||||||||||||||||||||
| 112 | 49 | 1; | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | else { | ||||||||||||||||||||||||||||
| 115 | 0 | my $p = length($str) - length($_); | |||||||||||||||||||||||||||
| 116 | 0 | my $L = substr($str, 0, $p); | |||||||||||||||||||||||||||
| 117 | 0 | my $R = substr($str, $p); | |||||||||||||||||||||||||||
| 118 | 0 | die "Unparseable substring at <*>: $L<*>$R"; | |||||||||||||||||||||||||||
| 119 | 0 | $res = $1; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | 79 | $out .= $res; | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | 22 | return $out; | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 25 | ▶ | sub decode { | ||||||||||||||||||||||||||
|
Subroutine
decode: called CC=4 SCAR=14.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 127 | 25 | my ($self, $code, $stash) = @_; | |||||||||||||||||||||||||||
| 128 | 25 | my $sr = ref $stash; | |||||||||||||||||||||||||||
| 129 | 25 | ▶ | return if $sr eq ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | 25 | my $val; | |||||||||||||||||||||||||||
| 131 | 25 | eval { $val = $stash->{$code}; 1 } | |||||||||||||||||||||||||||
| 132 | or | ||||||||||||||||||||||||||||
| 133 | 25 | ▶ | eval { $val = $stash->[$code]; 1 } | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
eval {
do {
$val = $stash->{$code};
1
}
} or eval {
do {
$val = $stash->[$code];
1
}
}
MC/DC0%0/2
eval {
do {
$val = $stash->{$code};
1
}
} or eval {
do {
$val = $stash->[$code];
1
}
}
eval {
do {
$val = $stash->{$code};
1
}
}
eval {
do {
$val = $stash->[$code];
1
}
}
Truth table66%2/3
A or B
A= eval {
do {
$val = $stash->{$code};
1
}
}B= eval {
do {
$val = $stash->[$code];
1
}
}
| |||||||||||||||||||||||||||||
| 134 | or die "unable to decode $code given stash type $sr"; | ||||||||||||||||||||||||||||
| 135 | 25 | return value($val); | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 25 | ▶ | sub value { | ||||||||||||||||||||||||||
|
Subroutine
value: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 139 | 25 | my ($val) = @_; | |||||||||||||||||||||||||||
| 140 | 25 | ▶ | $val = $val->() if ref($val) eq 'CODE'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | 25 | ▶ | return '' if !defined $val; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | 23 | return $val; | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 0 | ▶ | sub old_decode { | ||||||||||||||||||||||||||
|
Subroutine
old_decode: not called CC=16 SCAR=56.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 146 | 0 | my ($self, $code, $stash) = @_; | |||||||||||||||||||||||||||
| 147 | # XXX Not quite working fancy-dancy decoding follows... | ||||||||||||||||||||||||||||
| 148 | 0 | ▶ | my $val = $stash || $self->stash; | ||||||||||||||||||||||||||
|
Condition0%0/3
$stash || $self->stash
MC/DC0%0/2
$stash || $self->stash
$stash
$self->stash
Truth table0%0/3
A || B
A= $stashB= $self->stash
| |||||||||||||||||||||||||||||
| 149 | 0 | my $func = $self->functions; | |||||||||||||||||||||||||||
| 150 | 0 | my $rval = ref($val); | |||||||||||||||||||||||||||
| 151 | 0 | ▶ | $code =~ s/^\.?/./ if $rval eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | 0 | ▶ | $func ||= {}; | ||||||||||||||||||||||||||
|
Condition0%0/2
$func ||= {}
MC/DC0%0/1
$func ||= {}
$func
Truth table0%0/2
A
A= $func
| |||||||||||||||||||||||||||||
| 153 | 0 | ▶ | while ($code =~ s{ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | ^ | ||||||||||||||||||||||||||||
| 155 | (?: | ||||||||||||||||||||||||||||
| 156 | \[ (-?\d+) (?: \.\. (-?\d+) )? \] | ||||||||||||||||||||||||||||
| 157 | | | ||||||||||||||||||||||||||||
| 158 | \. ([^\s.:\[\]\(\)]+) | ||||||||||||||||||||||||||||
| 159 | | | ||||||||||||||||||||||||||||
| 160 | :: ([^\s.:\[\]\(\)]+) | ||||||||||||||||||||||||||||
| 161 | ) | ||||||||||||||||||||||||||||
| 162 | }{}xg) { | ||||||||||||||||||||||||||||
| 163 | 0 | my ($l, $r, $k, $f) = ($1, $2, $3, $4); | |||||||||||||||||||||||||||
| 164 | 0 | ▶ | if (defined $f) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 165 | 0 | ▶ | die "No such function: $f" if !$func->{$f} ; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 166 | 0 | $val = $func->{$f}->($val); | |||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | elsif ($rval eq 'HASH') { | ||||||||||||||||||||||||||||
| 169 | 0 | ▶ | die if defined $l or defined $r; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $l or defined $r
MC/DC0%0/2
defined $l or defined $r
defined $l
defined $r
Truth table0%0/3
A or B
A= defined $lB= defined $r
| |||||||||||||||||||||||||||||
| 170 | 0 | $val = $val->{$k}; | |||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | elsif ($rval eq 'ARRAY') { | ||||||||||||||||||||||||||||
| 173 | 0 | ▶ | die if defined $k; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | 0 | ▶ | $val = defined $r ? [ @$val[$l..$r] ] : $val->[$l]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | else { | ||||||||||||||||||||||||||||
| 177 | 0 | die "Can't subval: ref = '$rval'"; | |||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | 0 | $rval = ref $val; | |||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | 0 | ▶ | die if length $code; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 182 | 0 | ▶ | return join('', @$val) if $rval eq 'ARRAY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 183 | 0 | ▶ | return join('', values %$val) if $rval eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 184 | 0 | return $val; | |||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 20 | ▶ | sub stringify { | ||||||||||||||||||||||||||
|
Subroutine
stringify: called CC=9 SCAR=23.1
Pod
documented
| |||||||||||||||||||||||||||||
| 188 | # For backward compatibility, we allow $self-less calls... | ||||||||||||||||||||||||||||
| 189 | 20 | my $val = pop; | |||||||||||||||||||||||||||
| 190 | 20 | my ($self) = @_; | |||||||||||||||||||||||||||
| 191 | 20 | ▶ | return '' if !defined $val; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 192 | 20 | my $r = ref $val; | |||||||||||||||||||||||||||
| 193 | 20 | ▶ | return $val if $r eq ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 194 | # ...and we don't recurse if we don't have $self | ||||||||||||||||||||||||||||
| 195 | 4 | ▶ | return '' if !$self; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 196 | 4 | my $sfy = $self->{'stringify'}; | |||||||||||||||||||||||||||
| 197 | 0 | ▶ | return join('', map { $sfy->($self, $_) } @$val) if $r eq 'ARRAY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 198 | 0 | ▶ | return join('', map { $sfy->($self, $_) } $val->()) if $r eq 'CODE'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 199 | 4 | ▶ | if ($r eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 200 | 4 | foreach (@{ $self->default_hash_keys }) { | |||||||||||||||||||||||||||
| 201 | 9 | ▶ | return $sfy->($self, $val->{$_}) if defined $val->{$_}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||
| 204 | 1 | return ''; | |||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | 4 | 1; | |||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | =pod | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | =head1 NAME | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | String::Expando - expand %(foo) codes in strings | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | $e = String::Expando->new; | ||||||||||||||||||||||||||||
| 218 | print $e->expand('%(foo) %(bar)', { foo => 'Hello', bar => 'world!' }), "\n"; | ||||||||||||||||||||||||||||
| 219 | print $e->expand( | ||||||||||||||||||||||||||||
| 220 | '### %04d(year)-%02d(month)-%02d(day) | ||||||||||||||||||||||||||||
| 221 | { year => 2011, month => 3, day => 9 } | ||||||||||||||||||||||||||||
| 222 | ), "\n"; | ||||||||||||||||||||||||||||
| 223 | ### 2011-03-09 | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | =head1 METHODS | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | =over 4 | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | =item B<new> | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | $e = String::Expando->new; | ||||||||||||||||||||||||||||
| 232 | $e = String::Expando->new( | ||||||||||||||||||||||||||||
| 233 | # "[% foo %]" -> $stash->{foo} | ||||||||||||||||||||||||||||
| 234 | 'expando' => qr/\[%\s*([^%]+?)\s*%\]/, | ||||||||||||||||||||||||||||
| 235 | # "%%" -> "%" | ||||||||||||||||||||||||||||
| 236 | 'escaped_literal' => qr/%(%)/, | ||||||||||||||||||||||||||||
| 237 | # etc. | ||||||||||||||||||||||||||||
| 238 | 'literal' => qr/(.)/, | ||||||||||||||||||||||||||||
| 239 | ); | ||||||||||||||||||||||||||||
| 240 | $e = String::Expando->new( | ||||||||||||||||||||||||||||
| 241 | # "%[.2f]L" => sprintf('%.2f', $stash->{L}) | ||||||||||||||||||||||||||||
| 242 | 'expando' => qr{ | ||||||||||||||||||||||||||||
| 243 | (?x) | ||||||||||||||||||||||||||||
| 244 | % | ||||||||||||||||||||||||||||
| 245 | # Optional format string | ||||||||||||||||||||||||||||
| 246 | (?: | ||||||||||||||||||||||||||||
| 247 | \[ | ||||||||||||||||||||||||||||
| 248 | ([^\]]+) | ||||||||||||||||||||||||||||
| 249 | \] | ||||||||||||||||||||||||||||
| 250 | )? | ||||||||||||||||||||||||||||
| 251 | # Stash key | ||||||||||||||||||||||||||||
| 252 | ( [A-Za-z0-9] ) | ||||||||||||||||||||||||||||
| 253 | }, | ||||||||||||||||||||||||||||
| 254 | 'stash' => { A => 1, B => 2, ... }, | ||||||||||||||||||||||||||||
| 255 | ); | ||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | Create a new expando object. Arguments allowed are as follows. | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | =over 4 | ||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | =item B<stash> | ||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | The hash from which expando values are obtained. An expando C<%(xyz)> expanded | ||||||||||||||||||||||||||||
| 264 | using stash C<$h> will yield the value of C<$h->{'xyz'}> (or the empty string, | ||||||||||||||||||||||||||||
| 265 | if the value of C<$h->{'xyz'}> is undefined). | ||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | =item B<expando> | ||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||
| 269 | The regexp (or simple scalar) to use to identify expando codes when parsing the | ||||||||||||||||||||||||||||
| 270 | input. It must contain a capture group for what will become the key into the | ||||||||||||||||||||||||||||
| 271 | stash. If it contains two capture groups and $2 is defined (and not empty) | ||||||||||||||||||||||||||||
| 272 | after matching, the value of $1 will be used with sprintf to produce the final | ||||||||||||||||||||||||||||
| 273 | output. | ||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | The default is: | ||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | qr/ | ||||||||||||||||||||||||||||
| 278 | (?x) | ||||||||||||||||||||||||||||
| 279 | \% | ||||||||||||||||||||||||||||
| 280 | ([^%()]*j | ||||||||||||||||||||||||||||
| 281 | \( | ||||||||||||||||||||||||||||
| 282 | ([^\s()]+) | ||||||||||||||||||||||||||||
| 283 | \) | ||||||||||||||||||||||||||||
| 284 | / | ||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||
| 286 | In other words, C<%(...)> with an optional format string between C<%> and C<(>. | ||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||
| 288 | =back | ||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||
| 290 | =item B<stash> | ||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||
| 292 | $h = $e->stash; | ||||||||||||||||||||||||||||
| 293 | $e->stash(\%hash); | ||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||
| 295 | Get or set the stash from which expando values will be obtained. | ||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||
| 297 | =item B<decoder> | ||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||
| 299 | A reference to a function with the signature C<($expando, $k, $stash)> that | ||||||||||||||||||||||||||||
| 300 | is called to obtain a value from C<$stash> using code C<$k>. | ||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||
| 302 | The default is to call C<$expando->decode($code, $stash)>, which returns: | ||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | =over 4 | ||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||
| 306 | =item * | ||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||
| 308 | The empty string if C<$stash> is scalar value. | ||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||
| 310 | =item * | ||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||
| 312 | =item * | ||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||
| 314 | C<$stash->{$code}> if C<$stash> is a hash reference and C<$stash->{$k}> is a scalar. | ||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | =item * | ||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||
| 318 | C<$stash->[$code]> if C<$stash> is an array | ||||||||||||||||||||||||||||
| 319 | reference and C<$stash->[$k]> is a scalar; C<< $stash->{$code}->() >> if | ||||||||||||||||||||||||||||
| 320 | C<$stash> is a hash reference and C<$stash->{$k}> is a code reference; or C<< | ||||||||||||||||||||||||||||
| 321 | $stash->[$code]->() >> if C<$stash> is an array reference and C<$stash->[$k]> | ||||||||||||||||||||||||||||
| 322 | is a code reference. | ||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||
| 324 | =back | ||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||
| 326 | =item B<dot_separator> B<STRING|REGEXP> | ||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||
| 328 | A separator to use in expando codes in order to access values not at the top | ||||||||||||||||||||||||||||
| 329 | level of the stash. Decoding happens | ||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||
| 331 | For example, if B<dot_separator> is set to C<.> or C<qr/\./> then the expando | ||||||||||||||||||||||||||||
| 332 | C<foo.bar.baz> expanded using stash C<$h> will yield the same value as the | ||||||||||||||||||||||||||||
| 333 | expando C<baz> expanded using stash C<< $h->{foo}{bar} >> (or the empty string, | ||||||||||||||||||||||||||||
| 334 | if said value is undefined). This may or may not be the same value as | ||||||||||||||||||||||||||||
| 335 | C<< $h->{foo}{bar}{baz} >>, depending on the B<decoder>. | ||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||
| 337 | For example, if C<$h> is this: | ||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||
| 339 | { | ||||||||||||||||||||||||||||
| 340 | 'foo' => { | ||||||||||||||||||||||||||||
| 341 | 'bar' => sub { return { 'baz' => 123 } }, | ||||||||||||||||||||||||||||
| 342 | }, | ||||||||||||||||||||||||||||
| 343 | } | ||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||
| 345 | Then C<foo.bar.baz> will expand to C<123>. | ||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||
| 347 | By default, no dot separator is defined. | ||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||
| 349 | =item B<consume_escaped_literal> | ||||||||||||||||||||||||||||
| 350 | =item B<consume_expando> | ||||||||||||||||||||||||||||
| 351 | =item B<consume_literal> | ||||||||||||||||||||||||||||
| 352 | =item B<default_hash_keys> | ||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | When expanding, the result of the expansion | ||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||
| 356 | =item B<escaped_literal> | ||||||||||||||||||||||||||||
| 357 | =item B<functions> | ||||||||||||||||||||||||||||
| 358 | =item B<literal> | ||||||||||||||||||||||||||||
| 359 | =item B<stash> | ||||||||||||||||||||||||||||
| 360 | =item B<stringify> | ||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||
| 362 | A coderef that will be used to stringify an expanded value. The code will be | ||||||||||||||||||||||||||||
| 363 | called with two arguments: the String::Expando object and the datum to stringify: | ||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||
| 365 | $stringify->($expando, $val); | ||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||
| 367 | =back | ||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||
| 369 | =cut | ||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||