| 1 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | package Template::Resolver; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 2 | $Template::Resolver::VERSION = '1.05'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | # ABSTRACT: A powerful, and simple, library for resolving placeholders in templated files | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | # PODNAME: Template::Resolver | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use Log::Any; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use Template::Transformer; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 2 | my $logger = Log::Any->get_logger(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | sub new { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 5 | ▶ | return bless( {}, shift )->_init(@_); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | sub _entity_to_properties { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 31 | ▶ | my ( $entity, $properties, $prefix ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_entity_to_properties: called CC=12 SCAR=30.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 31 | ▶ | $properties = {} unless $properties; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 31 | my $ref = ref($entity); | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 31 | ▶ | if ( ( $ref && $ref eq 'HASH' ) || blessed($entity) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition66%4/6
$ref and $ref eq "HASH" or builtin::blessed($entity)
$ref and $ref eq "HASH"
MC/DC33%1/3
$ref and $ref eq "HASH" or builtin::blessed($entity)
$ref
$ref eq "HASH"
builtin::blessed($entity)
Truth table40%2/5
A and B or C
A= $refB= $ref eq "HASH"C= builtin::blessed($entity)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 22 | foreach my $key ( keys( %{$entity} ) ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 26 | ▶ | _entity_to_properties( $entity->{$key}, $properties, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | ( $prefix ? "$prefix.$key" : $key ) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | ▶ | elsif ( $ref && $ref eq 'ARRAY' ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$ref and $ref eq "ARRAY"
MC/DC0%0/2
$ref and $ref eq "ARRAY"
$ref
$ref eq "ARRAY"
Truth table33%1/3
A and B
A= $refB= $ref eq "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 0 | my $index = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 0 | foreach my $array_entity ( @{$entity} ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 0 | ▶ | _entity_to_properties( $array_entity, $properties, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | ( $prefix ? "$prefix\[$index\]" : "[$index]" ) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 0 | $index++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | elsif ($ref) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 0 | croak("unsupported ref type '$ref'"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 9 | $properties->{$prefix} = $entity; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 31 | return $properties; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | sub _get_property { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 7 | ▶ | my ( $self, $value, $transform ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_get_property: called CC=3 SCAR=12.5
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 7 | my $transformed = $self->{transformer}->transform( $value, $transform ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 7 | ▶ | croak( "undefined value $value" . ( $transform ? ", using transform $transform" : '' ) ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | unless ( defined($transformed) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 7 | return $transformed; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | sub _init { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 5 | ▶ | my ( $self, $entity, %options ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_init: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 5 | ▶ | my $os = $options{os} || $^O; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$options{'os'} || $^O
MC/DC0%0/2
$options{'os'} || $^O
$options{'os'}
$^O
Truth table33%1/3
A || B
A= $options{'os'}B= $^O
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 5 | $logger->debug('creating new Resolver'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 5 | $self->{entity} = $entity; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 5 | $self->{transformer} = Template::Transformer->new( $os, _entity_to_properties($entity) ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 5 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | sub resolve { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 7 | ▶ | my ( $self, %options ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
resolve: called CC=5 SCAR=17.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 7 | ▶ | my $key = $options{key} || 'TEMPLATE'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$options{'key'} || 'TEMPLATE'
MC/DC0%0/1
$options{'key'} || 'TEMPLATE'
$options{'key'}
Truth table50%1/2
A
A= $options{'key'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 7 | my $content; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 7 | ▶ | if ( $options{content} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 1 | $content = $options{content}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | elsif ( $options{handle} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | $content = do { local ($/) = undef; <$options{handle}> }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | elsif ( $options{filename} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 6 | $content = do { local ( @ARGV, $/ ) = $options{filename}; <> }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | croak('Must provide one of [content, handle, filename]'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 7 | $content =~ s/\$\{$key(?:_(.*?))?\{(.*?)\}\}/$self->_get_property($2,$1)/egs; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 7 | return $content; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||