blib/lib/oEdtk/Restitution.pm

filter: Statementstmt 51.0% 24 / 47 Branchbran 0.0% 0 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 77.7% 7 / 9 Podpod 100% 2 / 2 totaltotal 43.4% 33 / 76 CC 11 SCAR 38.0
CC
11
Cov
37%
follow_path
90.0
restitution_value
12.0
BEGIN
1.0
CRAP 44.9
« lib/oEdtk/TexTag.pm ↑ Summary blib/lib/oEdtk/EDMS.pm »
1 package oEdtk::Restitution;
2 1 ▶use base qw( Exporter );
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use feature 'say';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Carp qw( carp croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Data::Dumper qw( Dumper );
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 our $VERSION = 2.1126;
11  
12 1 our @EXPORT_OK = qw( follow_path restitution_value );
13  
14 # follows a 'path' in a hash:
15 # in case h = { a => b => { "value" } } then follow_path(h, [ "a", "b" ]) returns "value"
16 # value returned: '' if list undef; execution on current value if code ref
17 0 ▶sub follow_path {
Subroutine follow_path: not called CC=9 SCAR=45.0
Pod documented
18 0 my ( $h, $l, $flag_quiet ) = @_;
19 0 ▶ $flag_quiet = 0 unless defined $flag_quiet;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $flag_quiet 0 0 0
20 0 my $res = $h;
21 0 foreach my $key ( @{$l} ) {
22 0 my $ref = ref $key;
23 0 ▶ if ( $ref eq '' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ref eq '') { } 0 0 0
elsif ($ref eq 'CODE') { } 0 0 0
24 0 ▶ if ( exists $res->{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $res->{$key}) { } 0 0 0
25 0 $res = $res->{$key};
26 }
27 else {
28 0 ▶ say STDERR "INFO: no key '$key' found." unless $flag_quiet;
Branch: 1 branch
BranchTrueFalseTotal
unless $flag_quiet 0 0 0
29 0 return '';
30 }
31 }
32 elsif ( $ref eq 'CODE' ) {
33 0 return &{$key}($res);
34 }
35 0 else { croak "Unexpected key (ref='$ref'): " . Dumper($key); }
36 }
37 0 my $ref = ref $res;
38 return ( $ref eq 'ARRAY' )
39 0 ▶ ? join( "\n", @{$res} ) # adresse_payeur
Branch: 2 branches
BranchTrueFalseTotal
$ref eq 'ARRAY' ? : 0 0 0
$ref ? : 0 0 0
40 : ($ref) ? croak "Unexpected result: " . Dumper($res)
41 : $res;
42 }
43  
44 0 ▶sub restitution_value {
Subroutine restitution_value: not called CC=3 SCAR=24.8
Pod documented
45 0 my ( $h, $obj, $flag_quiet ) = @_;
46 0 ▶ return '' if !defined $obj;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $obj 0 0 0
47 0 my $ref = ref $obj;
48 0 ▶ if ( $ref eq 'ARRAY' ) { return follow_path( $h, $obj, $flag_quiet ) }
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq 'ARRAY') { } 0 0 0
49 0 else { croak "Unexpected restitution rule: " . Dumper($obj); }
50 }
51  
52 1 1;
53  
54 =head1 NAME
55  
56 oEdtk::Restitution - read a value out of nested data with a key path
57  
58 =head1 SYNOPSIS
59  
60 use oEdtk::Restitution qw(follow_path restitution_value);
61  
62 my $h = { a => { b => { c => 'value' } } };
63 follow_path($h, [ 'a', 'b', 'c' ]); # 'value'
64 restitution_value($h, [ 'a', 'b' ], 1); # 'value', quiet
65  
66 =head1 DESCRIPTION
67  
68 A small path walker used by the templates to pull a value out of a nested
69 structure described by a list of keys. Ported from the 0.8032 fork at S20 of the
70 fork-merge plan.
71  
72 =head1 FUNCTIONS
73  
74 =head2 follow_path
75  
76 my $value = follow_path($h, $list, $flag_quiet);
77  
78 Walk C<$list> over C<$h>. A plain (non-reference) key descends into a hash. A
79 code reference is called with the value gathered so far and its result is
80 returned immediately. A missing key returns C<''> (and prints an INFO line on
81 STDERR unless C<$flag_quiet> is true); a key of any other kind is an error. At
82 the end, an array value is joined with newlines, any other reference is an
83 error, and a plain scalar is returned as is.
84  
85 =head2 restitution_value
86  
87 my $value = restitution_value($h, $rule, $flag_quiet);
88  
89 Wrapper for a restitution rule: an undefined rule yields C<''>, a list rule is
90 passed to L</follow_path>, and anything else is an error.
91  
92 =head1 AUTHOR
93  
94 The oEdtk team.
95  
96 =cut
« lib/oEdtk/TexTag.pm ↑ Summary blib/lib/oEdtk/EDMS.pm »