| 1 | package Net::Z3950::FOLIO::lodashGet; | ||||||||||
| 2 | |||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 3 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 3 | ▶ | use Data::Dumper; $Data::Dumper::INDENT = 2; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | |||||||||||
| 8 | 27 | ▶ | sub _compilePath { | ||||||||
|
Subroutine
_compilePath: called CC=5 SCAR=16.1
| |||||||||||
| 9 | 27 | my($path) = @_; | |||||||||
| 10 | 27 | my @components; | |||||||||
| 11 | |||||||||||
| 12 | 27 | ▶ | while ($path) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 13 | 60 | my @match = $path =~ /^([^.\[]*)(\.|\[\d+\]\.?)(.*)/; | |||||||||
| 14 | 60 | ▶ | if (!@match) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 15 | 21 | push @components, $path; | |||||||||
| 16 | 21 | last; | |||||||||
| 17 | } | ||||||||||
| 18 | |||||||||||
| 19 | 39 | my($head, $sep, $tail) = @match; | |||||||||
| 20 | 39 | ▶ | push @components, $head if $head ne ''; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 21 | 39 | ▶ | if ($sep ne '.') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 22 | 11 | $sep =~ s/\]\.?//; | |||||||||
| 23 | 11 | push @components, substr($sep, 1) + 0; | |||||||||
| 24 | } | ||||||||||
| 25 | |||||||||||
| 26 | 39 | $path = $tail; | |||||||||
| 27 | } | ||||||||||
| 28 | |||||||||||
| 29 | 27 | return @components; | |||||||||
| 30 | } | ||||||||||
| 31 | |||||||||||
| 32 | |||||||||||
| 33 | 12 | ▶ | sub lodashGet { | ||||||||
|
Subroutine
lodashGet: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||
| 34 | 12 | my($data, $path) = @_; | |||||||||
| 35 | # warn "starting with ", Dumper($data); | ||||||||||
| 36 | |||||||||||
| 37 | 12 | my @components = _compilePath($path); | |||||||||
| 38 | 12 | ▶ | while (@components) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 39 | 34 | my $component = shift @components; | |||||||||
| 40 | 34 | ▶ | if (ref $data eq 'HASH') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 41 | 24 | $data = $data->{$component}; | |||||||||
| 42 | } else { | ||||||||||
| 43 | # Assume it's an array | ||||||||||
| 44 | 10 | $data = $data->[$component]; | |||||||||
| 45 | } | ||||||||||
| 46 | # warn "moved down from '$component' to ", Dumper($data); | ||||||||||
| 47 | 34 | ▶ | return undef if !defined $data; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 48 | } | ||||||||||
| 49 | |||||||||||
| 50 | # warn "got ", (defined $data ? "'$data'" : 'UNDEF'); | ||||||||||
| 51 | |||||||||||
| 52 | 12 | return $data; | |||||||||
| 53 | } | ||||||||||
| 54 | |||||||||||
| 55 | |||||||||||
| 56 | 3 | ▶ | use Exporter qw(import); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 57 | 3 | our @EXPORT_OK = qw(_compilePath lodashGet); | |||||||||
| 58 | |||||||||||
| 59 | |||||||||||
| 60 | 3 | 1; | |||||||||