| 1 | ############################################################################### | ||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||
| 3 | # This file copyright © 2016 by Randy J. Ray, all rights reserved | ||||||||||||||||||||||||||||
| 4 | # | ||||||||||||||||||||||||||||
| 5 | # See "LICENSE AND COPYRIGHT" in the POD for terms. | ||||||||||||||||||||||||||||
| 6 | # | ||||||||||||||||||||||||||||
| 7 | ############################################################################### | ||||||||||||||||||||||||||||
| 8 | # | ||||||||||||||||||||||||||||
| 9 | # Description: A string-formatter inspired by Python's format() | ||||||||||||||||||||||||||||
| 10 | # | ||||||||||||||||||||||||||||
| 11 | # Functions: YASF | ||||||||||||||||||||||||||||
| 12 | # new | ||||||||||||||||||||||||||||
| 13 | # bind | ||||||||||||||||||||||||||||
| 14 | # format | ||||||||||||||||||||||||||||
| 15 | # | ||||||||||||||||||||||||||||
| 16 | # Libraries: None (only core) | ||||||||||||||||||||||||||||
| 17 | # | ||||||||||||||||||||||||||||
| 18 | # Global Consts: @EXPORT_OK | ||||||||||||||||||||||||||||
| 19 | # %NOT_ACCEPTABLE_REF | ||||||||||||||||||||||||||||
| 20 | # | ||||||||||||||||||||||||||||
| 21 | # Environment: None | ||||||||||||||||||||||||||||
| 22 | # | ||||||||||||||||||||||||||||
| 23 | ############################################################################### | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | package YASF; | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 7 | ▶ | use 5.008; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | 7 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | 7 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | 7 | ▶ | use overload fallback => 0, | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | 'eq' => \&_eq, | ||||||||||||||||||||||||||||
| 32 | 'ne' => \&_ne, | ||||||||||||||||||||||||||||
| 33 | 'lt' => \&_lt, | ||||||||||||||||||||||||||||
| 34 | 'le' => \&_le, | ||||||||||||||||||||||||||||
| 35 | 'gt' => \&_gt, | ||||||||||||||||||||||||||||
| 36 | 'ge' => \&_ge, | ||||||||||||||||||||||||||||
| 37 | 'cmp' => \&_cmp, | ||||||||||||||||||||||||||||
| 38 | q{.} => \&_dot, | ||||||||||||||||||||||||||||
| 39 | q{.=} => \&_dotequal, | ||||||||||||||||||||||||||||
| 40 | q{""} => \&_stringify, | ||||||||||||||||||||||||||||
| 41 | 7 | q{%} => \&_interpolate; | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 7 | ▶ | use Carp qw(carp croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 44 | 7 | ▶ | use Exporter qw(import); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 7 | our $VERSION = '0.005'; # VERSION | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 7 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 49 | 7 | ▶ | no strict 'refs'; ## no critic (ProhibitNoStrict) | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 7 | for my $method (qw(template bindings on_undef)) { | |||||||||||||||||||||||||||
| 52 | 21 | ▶ | *{$method} = sub { shift->{$method} } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 53 | 21 | } | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 7 | my %NOT_ACCEPTABLE_REF = ( | |||||||||||||||||||||||||||
| 57 | SCALAR => 1, | ||||||||||||||||||||||||||||
| 58 | CODE => 1, | ||||||||||||||||||||||||||||
| 59 | REF => 1, | ||||||||||||||||||||||||||||
| 60 | GLOB => 1, | ||||||||||||||||||||||||||||
| 61 | LVALUE => 1, | ||||||||||||||||||||||||||||
| 62 | FORMAT => 1, | ||||||||||||||||||||||||||||
| 63 | IO => 1, | ||||||||||||||||||||||||||||
| 64 | VSTRING => 1, | ||||||||||||||||||||||||||||
| 65 | Regexp => 1, | ||||||||||||||||||||||||||||
| 66 | ); | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 7 | my %VALID_ON_UNDEF = ( | |||||||||||||||||||||||||||
| 69 | die => 1, | ||||||||||||||||||||||||||||
| 70 | warn => 1, | ||||||||||||||||||||||||||||
| 71 | token => 1, | ||||||||||||||||||||||||||||
| 72 | ignore => 1, | ||||||||||||||||||||||||||||
| 73 | ); | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 7 | our @EXPORT_OK = qw(YASF); | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | ############################################################################### | ||||||||||||||||||||||||||||
| 78 | # | ||||||||||||||||||||||||||||
| 79 | # Sub Name: YASF | ||||||||||||||||||||||||||||
| 80 | # | ||||||||||||||||||||||||||||
| 81 | # Description: Shortcut to calling YASF->new($str) with no other args. | ||||||||||||||||||||||||||||
| 82 | # | ||||||||||||||||||||||||||||
| 83 | # Arguments: NAME IN/OUT TYPE DESCRIPTION | ||||||||||||||||||||||||||||
| 84 | # $template in scalar String template for formatter | ||||||||||||||||||||||||||||
| 85 | # | ||||||||||||||||||||||||||||
| 86 | # Returns: Success: new object | ||||||||||||||||||||||||||||
| 87 | # Failure: dies | ||||||||||||||||||||||||||||
| 88 | # | ||||||||||||||||||||||||||||
| 89 | ############################################################################### | ||||||||||||||||||||||||||||
| 90 | ## no critic(ProhibitSubroutinePrototypes) | ||||||||||||||||||||||||||||
| 91 | 2 | ▶ | sub YASF ($) { return YASF->new(shift); } | ||||||||||||||||||||||||||
|
Subroutine
YASF: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | ############################################################################### | ||||||||||||||||||||||||||||
| 94 | # | ||||||||||||||||||||||||||||
| 95 | # Sub Name: new | ||||||||||||||||||||||||||||
| 96 | # | ||||||||||||||||||||||||||||
| 97 | # Description: Class constructor. Creates the basic object and | ||||||||||||||||||||||||||||
| 98 | # pre-compiles the template into the form that the formatter | ||||||||||||||||||||||||||||
| 99 | # uses. | ||||||||||||||||||||||||||||
| 100 | # | ||||||||||||||||||||||||||||
| 101 | # Arguments: NAME IN/OUT TYPE DESCRIPTION | ||||||||||||||||||||||||||||
| 102 | # $class in scalar Name of class | ||||||||||||||||||||||||||||
| 103 | # $template in scalar String template for formatter | ||||||||||||||||||||||||||||
| 104 | # @args in scalar Everything else (see code) | ||||||||||||||||||||||||||||
| 105 | # | ||||||||||||||||||||||||||||
| 106 | # Returns: Success: new object | ||||||||||||||||||||||||||||
| 107 | # Failure: dies | ||||||||||||||||||||||||||||
| 108 | # | ||||||||||||||||||||||||||||
| 109 | ############################################################################### | ||||||||||||||||||||||||||||
| 110 | 31 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 111 | 31 | my ($class, $template, @args) = @_; | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 31 | ▶ | croak 'new requires string template argument' if (! defined $template); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 30 | ▶ | my $args = @args == 1 ? $args[0] : { @args }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | 30 | my $self = bless { | |||||||||||||||||||||||||||
| 117 | template => $template, | ||||||||||||||||||||||||||||
| 118 | bindings => undef, | ||||||||||||||||||||||||||||
| 119 | on_undef => 'warn', | ||||||||||||||||||||||||||||
| 120 | }, $class; | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 30 | $self->_compile; | |||||||||||||||||||||||||||
| 123 | 27 | ▶ | if ($args->{bindings}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | 14 | $self->bind($args->{bindings}); | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | 27 | ▶ | if ($args->{on_undef}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | croak "new: Invalid value for 'on_undef' ($args->{on_undef})" | ||||||||||||||||||||||||||||
| 128 | 5 | ▶ | if (! $VALID_ON_UNDEF{$args->{on_undef}}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | 5 | $self->{on_undef} = $args->{on_undef}; | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 27 | return $self; | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | ############################################################################### | ||||||||||||||||||||||||||||
| 136 | # | ||||||||||||||||||||||||||||
| 137 | # Sub Name: bind | ||||||||||||||||||||||||||||
| 138 | # | ||||||||||||||||||||||||||||
| 139 | # Description: Add or change object-level bindings | ||||||||||||||||||||||||||||
| 140 | # | ||||||||||||||||||||||||||||
| 141 | # Arguments: NAME IN/OUT TYPE DESCRIPTION | ||||||||||||||||||||||||||||
| 142 | # $self in ref Object of this class | ||||||||||||||||||||||||||||
| 143 | # $bindings in ref New bindings | ||||||||||||||||||||||||||||
| 144 | # | ||||||||||||||||||||||||||||
| 145 | # Globals: %NOT_ACCEPTABLE_REF | ||||||||||||||||||||||||||||
| 146 | # | ||||||||||||||||||||||||||||
| 147 | # Returns: Success: $self | ||||||||||||||||||||||||||||
| 148 | # Failure: dies | ||||||||||||||||||||||||||||
| 149 | # | ||||||||||||||||||||||||||||
| 150 | ############################################################################### | ||||||||||||||||||||||||||||
| 151 | 24 | ▶ | sub bind { ## no critic(ProhibitBuiltinHomonyms) | ||||||||||||||||||||||||||
|
Subroutine
bind: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 152 | 24 | my ($self, $bindings) = @_; | |||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 24 | ▶ | if ((@_ == 2) && (! defined $bindings)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@_ == 2 and not defined $bindings
MC/DC100%2/2
@_ == 2 and not defined $bindings
@_ == 2
not defined $bindings
Truth table100%3/3
A and not(B)
A= @_ == 2B= not defined $bindings
| |||||||||||||||||||||||||||||
| 155 | # The means of unbinding is to call $obj->bind(undef): | ||||||||||||||||||||||||||||
| 156 | 1 | undef $self->{bindings}; | |||||||||||||||||||||||||||
| 157 | } else { | ||||||||||||||||||||||||||||
| 158 | 23 | ▶ | croak 'bind: New bindings must be provided as a parameter' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | if (! $bindings); | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 22 | my $type = ref $bindings; | |||||||||||||||||||||||||||
| 162 | 22 | ▶ | if (! $type) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 163 | 1 | croak 'New bindings must be a reference (HASH, ARRAY or object)'; | |||||||||||||||||||||||||||
| 164 | } elsif ($NOT_ACCEPTABLE_REF{$type}) { | ||||||||||||||||||||||||||||
| 165 | 1 | croak "New bindings reference type ($type) not usable"; | |||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 20 | $self->{bindings} = $bindings; | |||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 21 | return $self; | |||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | ############################################################################### | ||||||||||||||||||||||||||||
| 175 | # | ||||||||||||||||||||||||||||
| 176 | # Sub Name: format | ||||||||||||||||||||||||||||
| 177 | # | ||||||||||||||||||||||||||||
| 178 | # Description: Front-end to the recursive _format routine, which does the | ||||||||||||||||||||||||||||
| 179 | # bulk of the parsing/interpolation of the object's template | ||||||||||||||||||||||||||||
| 180 | # against the given bindings. | ||||||||||||||||||||||||||||
| 181 | # | ||||||||||||||||||||||||||||
| 182 | # Arguments: NAME IN/OUT TYPE DESCRIPTION | ||||||||||||||||||||||||||||
| 183 | # $self in ref Object of this class | ||||||||||||||||||||||||||||
| 184 | # $bindings in ref Optional, bindings to use in | ||||||||||||||||||||||||||||
| 185 | # interpolation. Defaults to | ||||||||||||||||||||||||||||
| 186 | # object-level bindings. | ||||||||||||||||||||||||||||
| 187 | # | ||||||||||||||||||||||||||||
| 188 | # Returns: Success: string | ||||||||||||||||||||||||||||
| 189 | # Failure: dies | ||||||||||||||||||||||||||||
| 190 | # | ||||||||||||||||||||||||||||
| 191 | ############################################################################### | ||||||||||||||||||||||||||||
| 192 | 66 | ▶ | sub format { ## no critic(ProhibitBuiltinHomonyms) | ||||||||||||||||||||||||||
|
Subroutine
format: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 193 | 66 | my ($self, $bindings) = @_; | |||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | 66 | ▶ | $bindings ||= $self->bindings; | ||||||||||||||||||||||||||
|
Condition100%3/3
$bindings ||= $self->bindings
MC/DC100%2/2
$bindings ||= $self->bindings
$bindings
$self->bindings
Truth table100%3/3
A ||= B
A= $bindingsB= $self->bindings
| |||||||||||||||||||||||||||||
| 196 | 66 | ▶ | croak 'format: Bindings are required if object has no internal bindings' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 197 | if (! $bindings); | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | my $value = join q{} => | ||||||||||||||||||||||||||||
| 200 | 365 | ▶ | map { ref() ? $self->_format($bindings, @{$_}) : $_ } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 201 | 65 | @{$self->{_compiled}}; | |||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | 61 | return $value; | |||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | # Private functions that support the public API: | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | # Compile the template into a tree structure that can be easily traversed for | ||||||||||||||||||||||||||||
| 209 | # formatting. | ||||||||||||||||||||||||||||
| 210 | 30 | ▶ | sub _compile { | ||||||||||||||||||||||||||
|
Subroutine
_compile: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||
| 211 | 30 | my $self = shift; | |||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | 30 | my @tokens = $self->_tokenize; | |||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | 30 | my @stack = ([]); | |||||||||||||||||||||||||||
| 216 | 30 | my $level = 0; | |||||||||||||||||||||||||||
| 217 | 30 | my @opens = (); | |||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | 30 | ▶ | while (my ($type, $value) = splice @tokens, 0, 2) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 220 | 441 | ▶ | if ($type eq 'STRING') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 221 | 113 | push @{$stack[$level]}, $value; | |||||||||||||||||||||||||||
| 222 | } elsif ($type eq 'OPEN') { | ||||||||||||||||||||||||||||
| 223 | 165 | push @stack, []; | |||||||||||||||||||||||||||
| 224 | 165 | $level++; | |||||||||||||||||||||||||||
| 225 | 165 | push @opens, $value; | |||||||||||||||||||||||||||
| 226 | } else { | ||||||||||||||||||||||||||||
| 227 | # Must be 'CLOSE'. | ||||||||||||||||||||||||||||
| 228 | 163 | ▶ | if ($level) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 229 | 162 | my $subtree = pop @stack; | |||||||||||||||||||||||||||
| 230 | 162 | $level--; | |||||||||||||||||||||||||||
| 231 | 162 | push @{$stack[$level]}, $subtree; | |||||||||||||||||||||||||||
| 232 | 162 | pop @opens; | |||||||||||||||||||||||||||
| 233 | } else { | ||||||||||||||||||||||||||||
| 234 | 1 | croak "Unmatched closing brace at position $value"; | |||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | 29 | ▶ | if ($level) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 240 | 2 | ▶ | croak sprintf '%d unmatched opening brace%s, last at position %d', | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 241 | $level, $level == 1 ? q{} : 's', pop @opens; | ||||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | 27 | $self->{_compiled} = $stack[0]; | |||||||||||||||||||||||||||
| 245 | 27 | return $self; | |||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | # Tokenize the object's template into a sequence of (type, value) pairs that | ||||||||||||||||||||||||||||
| 249 | # identify the opening and closing braces, and ordinary strings. | ||||||||||||||||||||||||||||
| 250 | 30 | ▶ | sub _tokenize { | ||||||||||||||||||||||||||
|
Subroutine
_tokenize: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 251 | 30 | my $self = shift; | |||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | 30 | my (@list, $base, $pos, $len); | |||||||||||||||||||||||||||
| 254 | 30 | my $str = $self->template; | |||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | 30 | $base = 0; | |||||||||||||||||||||||||||
| 257 | 30 | ▶ | while ($str =~ /(?<!\\)(?=[{}])/g) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 258 | 328 | $pos = pos $str; | |||||||||||||||||||||||||||
| 259 | 328 | ▶ | if ($len = $pos - $base) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 260 | 111 | (my $piece = substr $str, $base, $len) =~ s/\\([{}])/$1/g; | |||||||||||||||||||||||||||
| 261 | 111 | push @list, 'STRING', $piece; | |||||||||||||||||||||||||||
| 262 | } | ||||||||||||||||||||||||||||
| 263 | 328 | ▶ | push @list, ('{' eq substr $str, $pos, 1) ? 'OPEN' : 'CLOSE'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 264 | 328 | push @list, $pos; | |||||||||||||||||||||||||||
| 265 | 328 | $base = $pos + 1; | |||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||
| 268 | 30 | ▶ | if (length($str) > $base) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 269 | 2 | (my $piece = substr $str, $base) =~ s/\\([{}])/$1/g; | |||||||||||||||||||||||||||
| 270 | 2 | push @list, 'STRING', $piece; | |||||||||||||||||||||||||||
| 271 | } | ||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||
| 273 | 30 | return @list; | |||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | # Does the hard and recursive part of the actual formatting. Not actually that | ||||||||||||||||||||||||||||
| 277 | # hard, but a little recursive. | ||||||||||||||||||||||||||||
| 278 | 255 | ▶ | sub _format { | ||||||||||||||||||||||||||
|
Subroutine
_format: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 279 | 255 | my ($self, $bindings, @elements) = @_; | |||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | # Slight duplication of code from format() here, but it saves having to | ||||||||||||||||||||||||||||
| 282 | # keep track of depth and do a conditional on every return. | ||||||||||||||||||||||||||||
| 283 | my $expr = join q{} => | ||||||||||||||||||||||||||||
| 284 | 255 | ▶ | map { ref() ? $self->_format($bindings, @{$_}) : $_ } @elements; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||
| 286 | 255 | return $self->_expr_to_value($bindings, $expr); | |||||||||||||||||||||||||||
| 287 | } | ||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||
| 289 | # Converts an expression like "a.b.c" into a value from the bindings | ||||||||||||||||||||||||||||
| 290 | 255 | ▶ | sub _expr_to_value { | ||||||||||||||||||||||||||
|
Subroutine
_expr_to_value: called CC=12 SCAR=24.8
| |||||||||||||||||||||||||||||
| 291 | 255 | my ($self, $bindings, $string) = @_; | |||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||
| 293 | 255 | my ($expr, $format) = split /:/ => $string, 2; | |||||||||||||||||||||||||||
| 294 | # For now, $format is ignored | ||||||||||||||||||||||||||||
| 295 | 255 | my @hier = split /[.]/ => $expr; | |||||||||||||||||||||||||||
| 296 | 255 | my $node = $bindings; | |||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | 255 | for my $key (@hier) { | |||||||||||||||||||||||||||
| 299 | 258 | ▶ | if ($key =~ /^\d+$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 300 | 19 | ▶ | if (ref $node eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 301 | 18 | $node = $node->[$key]; | |||||||||||||||||||||||||||
| 302 | } else { | ||||||||||||||||||||||||||||
| 303 | 1 | croak "Key-type mismatch (key $key) in $expr, node is not " . | |||||||||||||||||||||||||||
| 304 | 'an ARRAY ref'; | ||||||||||||||||||||||||||||
| 305 | } | ||||||||||||||||||||||||||||
| 306 | } else { | ||||||||||||||||||||||||||||
| 307 | 239 | ▶ | if (ref $node eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 308 | 159 | $node = $node->{$key}; | |||||||||||||||||||||||||||
| 309 | } elsif (ref $node eq 'ARRAY') { | ||||||||||||||||||||||||||||
| 310 | 1 | croak "Key-type mismatch (key $key) in $expr, node is an " . | |||||||||||||||||||||||||||
| 311 | 'ARRAY ref when expecting HASH or object'; | ||||||||||||||||||||||||||||
| 312 | } elsif (ref $node) { | ||||||||||||||||||||||||||||
| 313 | 78 | $node = $node->$key(); | |||||||||||||||||||||||||||
| 314 | } else { | ||||||||||||||||||||||||||||
| 315 | 1 | croak "Key-type mismatch (key $key) in $expr, node is not " . | |||||||||||||||||||||||||||
| 316 | 'a HASH ref or object'; | ||||||||||||||||||||||||||||
| 317 | } | ||||||||||||||||||||||||||||
| 318 | } | ||||||||||||||||||||||||||||
| 319 | } | ||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||
| 321 | # Because all the key-substitution has been done before this sub is called, | ||||||||||||||||||||||||||||
| 322 | # it's probably a bad thing if $node is a ref. It's gonna get stringified | ||||||||||||||||||||||||||||
| 323 | # as a ref, which is probably not what the caller intended. | ||||||||||||||||||||||||||||
| 324 | 252 | ▶ | if (ref $node) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 325 | 1 | carp "Format expression $expr yielded a reference value rather than " . | |||||||||||||||||||||||||||
| 326 | 'a scalar'; | ||||||||||||||||||||||||||||
| 327 | } | ||||||||||||||||||||||||||||
| 328 | # If $node is undef, react accordingly based on the "on_undef" property. | ||||||||||||||||||||||||||||
| 329 | 252 | ▶ | if (! defined $node) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 330 | # We validated this value in new(), so no need to do so here. | ||||||||||||||||||||||||||||
| 331 | 6 | my $what_to_do = $self->on_undef; | |||||||||||||||||||||||||||
| 332 | 6 | ▶ | if ($what_to_do eq 'ignore') { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 333 | 1 | $node = q{}; | |||||||||||||||||||||||||||
| 334 | } elsif ($what_to_do eq 'warn') { | ||||||||||||||||||||||||||||
| 335 | 2 | carp "No binding for reference to '$expr'"; | |||||||||||||||||||||||||||
| 336 | 2 | $node = q{}; | |||||||||||||||||||||||||||
| 337 | } elsif ($what_to_do eq 'die') { | ||||||||||||||||||||||||||||
| 338 | 1 | croak "No binding for reference to '$expr'"; | |||||||||||||||||||||||||||
| 339 | } else { | ||||||||||||||||||||||||||||
| 340 | # Leave the token unchanged | ||||||||||||||||||||||||||||
| 341 | 2 | $node = "{$expr}"; | |||||||||||||||||||||||||||
| 342 | } | ||||||||||||||||||||||||||||
| 343 | } | ||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||
| 345 | 251 | return $node; | |||||||||||||||||||||||||||
| 346 | } | ||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||
| 348 | # Actual operator-overload functions: | ||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||
| 350 | # Handle the object stringification (the "" operator) | ||||||||||||||||||||||||||||
| 351 | 44 | ▶ | sub _stringify { | ||||||||||||||||||||||||||
|
Subroutine
_stringify: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 352 | 44 | my $self = shift; | |||||||||||||||||||||||||||
| 353 | 44 | my $bindings = $self->bindings; | |||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||
| 355 | 44 | ▶ | return $bindings ? $self->format($bindings) : $self->template; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 356 | } | ||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||
| 358 | # Handle the % interpolation operator | ||||||||||||||||||||||||||||
| 359 | 10 | ▶ | sub _interpolate { | ||||||||||||||||||||||||||
|
Subroutine
_interpolate: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 360 | 10 | my ($self, $bindings, $swap) = @_; | |||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||
| 362 | 10 | ▶ | if ($swap) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 363 | 1 | my $class = ref $self; | |||||||||||||||||||||||||||
| 364 | 1 | croak "$class object must come first in % interpolation"; | |||||||||||||||||||||||||||
| 365 | } | ||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||
| 367 | 9 | return $self->format($bindings); | |||||||||||||||||||||||||||
| 368 | } | ||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||
| 370 | # Handle the 'cmp' operator | ||||||||||||||||||||||||||||
| 371 | 2 | ▶ | sub _cmp { | ||||||||||||||||||||||||||
|
Subroutine
_cmp: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 372 | 2 | my ($self, $other, $swap) = @_; | |||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||
| 374 | 2 | ▶ | return $swap ? | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 375 | ($other cmp $self->_stringify) : ($self->_stringify cmp $other); | ||||||||||||||||||||||||||||
| 376 | } | ||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||
| 378 | # Handle the 'eq' operator | ||||||||||||||||||||||||||||
| 379 | 3 | ▶ | sub _eq { | ||||||||||||||||||||||||||
|
Subroutine
_eq: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 380 | 3 | my ($self, $other, $swap) = @_; | |||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||
| 382 | 3 | return $self->_stringify eq $other; | |||||||||||||||||||||||||||
| 383 | } | ||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||
| 385 | # Handle the 'ne' operator | ||||||||||||||||||||||||||||
| 386 | 4 | ▶ | sub _ne { | ||||||||||||||||||||||||||
|
Subroutine
_ne: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 387 | 4 | my ($self, $other, $swap) = @_; | |||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||
| 389 | 4 | return $self->_stringify ne $other; | |||||||||||||||||||||||||||
| 390 | } | ||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||
| 392 | # Handle the 'lt' operator | ||||||||||||||||||||||||||||
| 393 | 4 | ▶ | sub _lt { | ||||||||||||||||||||||||||
|
Subroutine
_lt: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 394 | 4 | my ($self, $other, $swap) = @_; | |||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||
| 396 | 4 | ▶ | return $swap ? | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 397 | ($other lt $self->_stringify) : ($self->_stringify lt $other); | ||||||||||||||||||||||||||||
| 398 | } | ||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||
| 400 | # Handle the 'le' operator | ||||||||||||||||||||||||||||
| 401 | 8 | ▶ | sub _le { | ||||||||||||||||||||||||||
|
Subroutine
_le: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 402 | 8 | my ($self, $other, $swap) = @_; | |||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||
| 404 | 8 | ▶ | return $swap ? | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 405 | ($other le $self->_stringify) : ($self->_stringify le $other); | ||||||||||||||||||||||||||||
| 406 | } | ||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||
| 408 | # Handle the 'gt' operator | ||||||||||||||||||||||||||||
| 409 | 4 | ▶ | sub _gt { | ||||||||||||||||||||||||||
|
Subroutine
_gt: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 410 | 4 | my ($self, $other, $swap) = @_; | |||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||
| 412 | 4 | ▶ | return $swap ? | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 413 | ($other gt $self->_stringify) : ($self->_stringify gt $other); | ||||||||||||||||||||||||||||
| 414 | } | ||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||
| 416 | # Handle the 'ge' operator | ||||||||||||||||||||||||||||
| 417 | 8 | ▶ | sub _ge { | ||||||||||||||||||||||||||
|
Subroutine
_ge: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 418 | 8 | my ($self, $other, $swap) = @_; | |||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||
| 420 | 8 | ▶ | return $swap ? | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 421 | ($other ge $self->_stringify) : ($self->_stringify ge $other); | ||||||||||||||||||||||||||||
| 422 | } | ||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||
| 424 | # Handle the '.' operator | ||||||||||||||||||||||||||||
| 425 | 5 | ▶ | sub _dot { | ||||||||||||||||||||||||||
|
Subroutine
_dot: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 426 | 5 | my ($self, $other, $swap) = @_; | |||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||
| 428 | 5 | ▶ | return $swap ? | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 429 | $other . $self->_stringify : $self->_stringify . $other; | ||||||||||||||||||||||||||||
| 430 | } | ||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||
| 432 | # Handle the '.=' operator | ||||||||||||||||||||||||||||
| 433 | 2 | ▶ | sub _dotequal { | ||||||||||||||||||||||||||
|
Subroutine
_dotequal: called CC=2 SCAR=7.4
| |||||||||||||||||||||||||||||
| 434 | 2 | my ($self, $other, $swap) = @_; | |||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||
| 436 | 2 | ▶ | if (! $swap) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 437 | 2 | my $class = ref $self; | |||||||||||||||||||||||||||
| 438 | 2 | croak "$class object cannot be on the left of .="; | |||||||||||||||||||||||||||
| 439 | } | ||||||||||||||||||||||||||||
| 440 | |||||||||||||||||||||||||||||
| 441 | 0 | return $other . $self->_stringify; | |||||||||||||||||||||||||||
| 442 | } | ||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||
| 444 | 7 | 1; | |||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||
| 446 | __END__ | ||||||||||||||||||||||||||||