| 1 | 7 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | 7 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | package Macro::Micro; | ||||||||||||||||||||||||||||
| 4 | { | ||||||||||||||||||||||||||||
| 5 | 7 | $Macro::Micro::VERSION = '0.054'; | |||||||||||||||||||||||||||
| 6 | } | ||||||||||||||||||||||||||||
| 7 | # ABSTRACT: really simple templating for really simple templates | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 7 | ▶ | use Carp (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 7 | my $DEFAULT_MACRO_FORMAT = qr/(?<!\\)([\[<] (\w+) [>\]])/x; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | sub new { | ||||||||||||||||||||||||||||
| 15 | 6 | ▶ | my ($class, %arg) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 6 | my $self = bless { } => $class; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 6 | ▶ | $arg{macro_format} = $DEFAULT_MACRO_FORMAT unless $arg{macro_format}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 6 | $self->macro_format($arg{macro_format}); | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 6 | return $self; | |||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | sub macro_format { | ||||||||||||||||||||||||||||
| 28 | 25 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
macro_format: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 25 | ▶ | return $self->{macro_format} unless @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 8 | my $macro_format = shift; | |||||||||||||||||||||||||||
| 33 | 8 | ▶ | Carp::croak "macro format must be a regexp reference" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | unless ref $macro_format eq 'Regexp'; | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 6 | $self->{macro_format} = $macro_format; | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | sub register_macros { | ||||||||||||||||||||||||||||
| 41 | 8 | ▶ | my ($self, @macros) = @_; | ||||||||||||||||||||||||||
|
Subroutine
register_macros: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 8 | ▶ | for (my $i = 0; $i < @macros; $i += 2) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | 27 | my ($name, $value) = @macros[ $i, $i+1 ]; | |||||||||||||||||||||||||||
| 45 | 27 | ▶ | Carp::croak "macro value must be a string or code reference" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
ref $value and ref $value ne "CODE"
MC/DC100%2/2
ref $value and ref $value ne "CODE"
ref $value
ref $value ne "CODE"
Truth table100%3/3
A and B
A= ref $valueB= ref $value ne "CODE"
| |||||||||||||||||||||||||||||
| 46 | if (ref $value) and (ref $value ne 'CODE'); | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 26 | ▶ | if (not ref $name) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 49 | 20 | $self->{macro}{$name} = $value; | |||||||||||||||||||||||||||
| 50 | } elsif (ref $name eq 'Regexp') { | ||||||||||||||||||||||||||||
| 51 | 5 | $self->{macro_regexp}{$name} = [ $name, $value ]; | |||||||||||||||||||||||||||
| 52 | } else { | ||||||||||||||||||||||||||||
| 53 | 1 | Carp::croak "macro name '$name' must be a string or a regexp"; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 6 | return $self; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | sub clear_macros { | ||||||||||||||||||||||||||||
| 62 | 0 | ▶ | my ($self, @macros) = @_; | ||||||||||||||||||||||||||
|
Subroutine
clear_macros: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 0 | ▶ | if (@macros) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 0 | Carp::croak "partial deletion not implemented"; | |||||||||||||||||||||||||||
| 66 | } else { | ||||||||||||||||||||||||||||
| 67 | 0 | delete @$self{qw(macro macro_regexp)}; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 0 | return; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | sub get_macro { | ||||||||||||||||||||||||||||
| 75 | 27 | ▶ | my ($self, $macro_name) = @_; | ||||||||||||||||||||||||||
|
Subroutine
get_macro: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 27 | ▶ | return $self->{macro}{$macro_name} if exists $self->{macro}{$macro_name}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 9 | foreach my $regexp (values %{ $self->{macro_regexp} }) { | |||||||||||||||||||||||||||
| 80 | 8 | ▶ | return $regexp->[1] if $macro_name =~ $regexp->[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 3 | return; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | sub expand_macros { | ||||||||||||||||||||||||||||
| 88 | 9 | ▶ | my ($self, $object, $stash) = @_; | ||||||||||||||||||||||||||
|
Subroutine
expand_macros: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 9 | ▶ | if (eval { $object->isa('Macro::Micro::Template') }) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 1 | return $self->_expand_template($object, $stash); | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 8 | $self->fast_expander($stash)->($object); | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | sub _expand_template { | ||||||||||||||||||||||||||||
| 98 | 1 | ▶ | my ($self, $object, $stash) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_expand_template: called CC=3 SCAR=11.3
| |||||||||||||||||||||||||||||
| 99 | # expects to be passed ($whole_macro, $macro_inside_delim, $whole_text) | ||||||||||||||||||||||||||||
| 100 | my $expander = sub { | ||||||||||||||||||||||||||||
| 101 | 8 | ▶ | my $macro = $self->get_macro($_[1]); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=14.3
| |||||||||||||||||||||||||||||
| 102 | 8 | ▶ | return $_[0] unless defined $macro; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | 8 | ▶ | return ref $macro ? $macro->($_[1], $_[2], $stash)||'' : $macro; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
&$macro($_[1], $_[2], $stash) or ""
MC/DC0%0/1
&$macro($_[1], $_[2], $stash) or ""
&$macro($_[1], $_[2], $stash)
Truth table50%1/2
A
A= &$macro($_[1], $_[2], $stash)
| |||||||||||||||||||||||||||||
| 104 | 1 | }; | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 1 | ▶ | return ${ $object->_text } unless $object->_parts; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 1 | ▶ | return join '', map { ref $_ ? $expander->(@$_[0, 1], $object->_text) : $_ } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | $object->_parts; | ||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | sub expand_macros_in { | ||||||||||||||||||||||||||||
| 114 | 6 | ▶ | my ($self, $object, $stash) = @_; | ||||||||||||||||||||||||||
|
Subroutine
expand_macros_in: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 6 | ▶ | Carp::croak "object of in-place expansion must be a scalar reference" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not ref $object or ref $object ne "SCALAR"
MC/DC100%2/2
not ref $object or ref $object ne "SCALAR"
not ref $object
ref $object ne "SCALAR"
Truth table100%3/3
not(A) or B
A= not ref $objectB= ref $object ne "SCALAR"
| |||||||||||||||||||||||||||||
| 117 | if (not ref $object) | ||||||||||||||||||||||||||||
| 118 | or (ref $object ne 'SCALAR'); | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 4 | my $fast_expander = $self->fast_expander($stash); | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 4 | $$object = $fast_expander->($$object); | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | sub string_expander { | ||||||||||||||||||||||||||||
| 127 | 13 | ▶ | my ($self, $stash) = @_; | ||||||||||||||||||||||||||
|
Subroutine
string_expander: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 13 | my $expander = $self->macro_expander($stash); | |||||||||||||||||||||||||||
| 130 | 13 | my $regex = $self->macro_format; | |||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | my $applicator = sub { | ||||||||||||||||||||||||||||
| 133 | 13 | ▶ | my ($object) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 13 | ▶ | return unless defined $object; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | 13 | ▶ | Carp::croak "object of expansion must not be a reference" if ref $object; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 12 | $object =~ s/$regex/$expander->($1,$2)/eg; | |||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 12 | return $object; | |||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | 13 | } | |||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 7 | ▶ | BEGIN { *fast_expander = \&string_expander } | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | sub macro_expander { | ||||||||||||||||||||||||||||
| 148 | 13 | ▶ | my ($self, $stash) = @_; | ||||||||||||||||||||||||||
|
Subroutine
macro_expander: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 13 | my %cached; | |||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 13 | ▶ | if (values %{ $self->{macro_regexp} }) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 153 | return sub { | ||||||||||||||||||||||||||||
| 154 | 17 | ▶ | return $cached{ $_[0] } if defined $cached{ $_[0] }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.3
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 17 | my $macro = $self->get_macro($_[1]); | |||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 17 | ▶ | $cached{ $_[0] } = defined $macro | ||||||||||||||||||||||||||
|
Branch: 2 branches
Condition50%1/2
&$macro($_[1], $_[2], $stash) or ""
MC/DC0%0/1
&$macro($_[1], $_[2], $stash) or ""
&$macro($_[1], $_[2], $stash)
Truth table50%1/2
A
A= &$macro($_[1], $_[2], $stash)
| |||||||||||||||||||||||||||||
| 159 | ? ref $macro | ||||||||||||||||||||||||||||
| 160 | ? $macro->($_[1], $_[2], $stash)||'' : $macro | ||||||||||||||||||||||||||||
| 161 | : $_[0]; | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 17 | return $cached{ $_[0] }; | |||||||||||||||||||||||||||
| 164 | 11 | }; | |||||||||||||||||||||||||||
| 165 | } else { | ||||||||||||||||||||||||||||
| 166 | return sub { | ||||||||||||||||||||||||||||
| 167 | 7 | ▶ | return $cached{ $_[0] } if defined $cached{ $_[0] }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=19.2
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 7 | my $macro = $self->{macro}{ $_[1] }; | |||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 7 | ▶ | $cached{ $_[0] } = defined $macro | ||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/2
&$macro($_[1], $_[2], $stash) or ""
MC/DC0%0/1
&$macro($_[1], $_[2], $stash) or ""
&$macro($_[1], $_[2], $stash)
Truth table0%0/2
A
A= &$macro($_[1], $_[2], $stash)
| |||||||||||||||||||||||||||||
| 172 | ? ref $macro | ||||||||||||||||||||||||||||
| 173 | ? $macro->($_[1], $_[2], $stash)||'' : $macro | ||||||||||||||||||||||||||||
| 174 | : $_[0]; | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 7 | return $cached{ $_[0] }; | |||||||||||||||||||||||||||
| 177 | 2 | }; | |||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | sub study { | ||||||||||||||||||||||||||||
| 184 | 4 | ▶ | my ($self, $text) = @_; | ||||||||||||||||||||||||||
|
Subroutine
study: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | 4 | my $macro_format = $self->macro_format; | |||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | 4 | my @total; | |||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | my $pos; | ||||||||||||||||||||||||||||
| 191 | 4 | ▶ | while ($text =~ m/\G(.*?)$macro_format/gsm) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 192 | 8 | my ($snippet, $whole, $name) = ($1, $2, $3); | |||||||||||||||||||||||||||
| 193 | 8 | ▶ | push @total, (length $snippet ? $snippet : ()), | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 194 | ($whole ? [ $whole, $name ] : ()); | ||||||||||||||||||||||||||||
| 195 | 8 | $pos = pos $text; | |||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | 4 | ▶ | push @total, substr $text, $pos if defined $pos; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | 4 | return Macro::Micro::Template->_new(\$text, \@total); | |||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | { | ||||||||||||||||||||||||||||
| 204 | package Macro::Micro::Template; | ||||||||||||||||||||||||||||
| 205 | { | ||||||||||||||||||||||||||||
| 206 | 7 | $Macro::Micro::Template::VERSION = '0.054'; | |||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||
| 208 | 4 | ▶ | sub _new { bless [ $_[1], $_[2] ] => $_[0] } | ||||||||||||||||||||||||||
|
Subroutine
_new: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 209 | 2 | ▶ | sub _parts { @{ $_[0][1] } } | ||||||||||||||||||||||||||
|
Subroutine
_parts: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 210 | 8 | ▶ | sub _text { $_[0][0] } | ||||||||||||||||||||||||||
|
Subroutine
_text: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | 7 | "[MAGIC_TRUE_VALUE]"; | |||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | __END__ | ||||||||||||||||||||||||||||