| 1 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | package Data::InputMonster::Util 0.011; | ||||||||||||||||||||||||||||||||||||||||||
| 4 | # ABSTRACT: handy routines for use with the input monster | ||||||||||||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Sub::Exporter::Util qw(curry_method); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Sub::Exporter -setup => { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | exports => { | ||||||||||||||||||||||||||||||||||||||||||
| 9 | dig => curry_method, | ||||||||||||||||||||||||||||||||||||||||||
| 10 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 11 | 1 | }; | |||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | #pod =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 14 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 15 | #pod These methods, which provide some helpers for use with InputMonster, can be | ||||||||||||||||||||||||||||||||||||||||||
| 16 | #pod exported as routines upon request. | ||||||||||||||||||||||||||||||||||||||||||
| 17 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 18 | #pod =cut | ||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | #pod =method dig | ||||||||||||||||||||||||||||||||||||||||||
| 21 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 22 | #pod my $source = dig( [ $key1, $key2, $key2 ]); | ||||||||||||||||||||||||||||||||||||||||||
| 23 | #pod my $source = dig( sub { ... } ); | ||||||||||||||||||||||||||||||||||||||||||
| 24 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 25 | #pod A C<dig> source looks through the input using the given locator. If it's a | ||||||||||||||||||||||||||||||||||||||||||
| 26 | #pod coderef, the code is called and passed the input. If it's an arrayref, each | ||||||||||||||||||||||||||||||||||||||||||
| 27 | #pod entry is used, in turn, to subscript the input as a deep data structure. If | ||||||||||||||||||||||||||||||||||||||||||
| 28 | #pod it's a plain scalar, it's treated like a one-element arrayref would have been. | ||||||||||||||||||||||||||||||||||||||||||
| 29 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 30 | #pod For example, given: | ||||||||||||||||||||||||||||||||||||||||||
| 31 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 32 | #pod $input = [ { ... }, { ... }, { foo => [ { bar => 13, baz => undef } ] } ]; | ||||||||||||||||||||||||||||||||||||||||||
| 33 | #pod $source = dig( [ qw( 2 foo 0 bar ) ] ); | ||||||||||||||||||||||||||||||||||||||||||
| 34 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 35 | #pod The source would find 13. | ||||||||||||||||||||||||||||||||||||||||||
| 36 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 37 | #pod =cut | ||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | 5 | ▶ | sub dig { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
dig: called CC=5 SCAR=16.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | 5 | my ($self, $locator) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | 5 | ▶ | Carp::confess("no locator given") unless defined $locator; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | 5 | ▶ | $locator = [ $locator ] unless ref $locator; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | 5 | ▶ | if (ref $locator eq 'CODE') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | 0 | ▶ | return sub { $locator->($_[1]) }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | } elsif (ref $locator eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||
| 49 | 5 | ▶ | return sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=10 SCAR=23.3
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | 5 | my ($monster, $input) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 51 | 5 | my $next = $input; | |||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | 5 | for my $k (@$locator) { | |||||||||||||||||||||||||||||||||||||||||
| 54 | 14 | ▶ | return unless my $ref = ref $next; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | 13 | ▶ | return unless $ref and (($ref eq 'ARRAY') or ($ref eq 'HASH')); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
$ref and $ref eq "ARRAY" || $ref eq "HASH"
$ref eq "ARRAY" or $ref eq "HASH"
MC/DC0%0/3
$ref and $ref eq "ARRAY" || $ref eq "HASH"
$ref
$ref eq "ARRAY"
$ref eq "HASH"
Truth table50%2/4
A and B or C
A= $refB= $ref eq "ARRAY"C= $ref eq "HASH"
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | 13 | ▶ | return if $ref eq 'ARRAY' and $k !~ /\A-?\d+\z/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$ref eq "ARRAY" and not $k =~ /\A-?\d+\z/
MC/DC100%2/2
$ref eq "ARRAY" and not $k =~ /\A-?\d+\z/
$ref eq "ARRAY"
not $k =~ /\A-?\d+\z/
Truth table100%3/3
A and not(B)
A= $ref eq "ARRAY"B= not $k =~ /\A-?\d+\z/
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | 12 | ▶ | $next = $next->[ $k ] if $ref eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | 12 | ▶ | $next = $next->{ $k } if $ref eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | 3 | return $next; | |||||||||||||||||||||||||||||||||||||||||
| 63 | 5 | }; | |||||||||||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | Carp::confess("locator must be either a code or array reference"); | |||||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | 1 | 'hi, domm!'; | |||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | __END__ | ||||||||||||||||||||||||||||||||||||||||||