| 1 | package Switch::Reftype; | ||||||||||||||||||||||||||||
| 2 | 1 | $Switch::Reftype::VERSION = '0.001'; | |||||||||||||||||||||||||||
| 3 | # ABSTRACT: Execute code based on which type of reference is given. | ||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Scalar::Util 'reftype'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 1 | require Exporter; | |||||||||||||||||||||||||||
| 9 | 1 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 10 | 1 | our %EXPORT_TAGS = ( 'all' => [ qw( | |||||||||||||||||||||||||||
| 11 | switch_reftype | ||||||||||||||||||||||||||||
| 12 | if_SCALAR | ||||||||||||||||||||||||||||
| 13 | if_ARRAY | ||||||||||||||||||||||||||||
| 14 | if_HASH | ||||||||||||||||||||||||||||
| 15 | if_CODE | ||||||||||||||||||||||||||||
| 16 | if_REF | ||||||||||||||||||||||||||||
| 17 | if_GLOB | ||||||||||||||||||||||||||||
| 18 | if_LVALUE | ||||||||||||||||||||||||||||
| 19 | if_FORMAT | ||||||||||||||||||||||||||||
| 20 | if_IO | ||||||||||||||||||||||||||||
| 21 | if_VSTRING | ||||||||||||||||||||||||||||
| 22 | if_REGEXP | ||||||||||||||||||||||||||||
| 23 | )]); | ||||||||||||||||||||||||||||
| 24 | 1 | our @EXPORT_OK = map { @{$EXPORT_TAGS{$_}} } keys %EXPORT_TAGS; | |||||||||||||||||||||||||||
| 25 | 1 | our @EXPORT = qw(switch_reftype); | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 14 | ▶ | sub switch_reftype { | ||||||||||||||||||||||||||
|
Subroutine
switch_reftype: called CC=8 SCAR=20.9
Pod
documented
| |||||||||||||||||||||||||||||
| 29 | 14 | my $ref = shift; | |||||||||||||||||||||||||||
| 30 | 14 | my %dispatch = @_; | |||||||||||||||||||||||||||
| 31 | 14 | ▶ | my $reftype = defined($ref) && (reftype($ref) || "scalar") || "undef"; | ||||||||||||||||||||||||||
|
Condition85%6/7
defined $ref && (builtin::reftype($ref) || 'scalar') || 'undef'
defined $ref and builtin::reftype($ref) || "scalar"
builtin::reftype($ref) or "scalar"
MC/DC100%2/2
defined $ref && (builtin::reftype($ref) || 'scalar') || 'undef'
defined $ref
builtin::reftype($ref)
Truth table100%3/3
A and B
A= defined $refB= builtin::reftype($ref)
| |||||||||||||||||||||||||||||
| 32 | 14 | for ($ref) { # Alias $ref to $_ | |||||||||||||||||||||||||||
| 33 | 14 | for my $key ($reftype, 'default') { | |||||||||||||||||||||||||||
| 34 | 16 | ▶ | if (exists $dispatch{$key}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | 14 | my $result = $dispatch{$key}; | |||||||||||||||||||||||||||
| 36 | 14 | ▶ | $result = $result->() if reftype $result eq "CODE"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 14 | return $result; | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | }; | ||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 11 | ▶ | sub _if_reftype { | ||||||||||||||||||||||||||
|
Subroutine
_if_reftype: called CC=1 SCAR=0.4
| |||||||||||||||||||||||||||||
| 45 | 11 | my $ref = shift; | |||||||||||||||||||||||||||
| 46 | switch_reftype $ref, | ||||||||||||||||||||||||||||
| 47 | @_, | ||||||||||||||||||||||||||||
| 48 | 0 | ▶ | default => sub { $ref } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 49 | 11 | ; | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 1 | ▶ | sub if_SCALAR (&$) { return _if_reftype($_[1], SCALAR => $_[0]); } | ||||||||||||||||||||||||||
|
Subroutine
if_SCALAR: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 53 | 1 | ▶ | sub if_ARRAY (&$) { return _if_reftype($_[1], ARRAY => $_[0]); } | ||||||||||||||||||||||||||
|
Subroutine
if_ARRAY: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 54 | 1 | ▶ | sub if_HASH (&$) { return _if_reftype($_[1], HASH => $_[0]); } | ||||||||||||||||||||||||||
|
Subroutine
if_HASH: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 55 | 1 | ▶ | sub if_CODE (&$) { return _if_reftype($_[1], CODE => $_[0]); } | ||||||||||||||||||||||||||
|
Subroutine
if_CODE: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 56 | 1 | ▶ | sub if_REF (&$) { return _if_reftype($_[1], REF => $_[0]); } | ||||||||||||||||||||||||||
|
Subroutine
if_REF: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 57 | 1 | ▶ | sub if_GLOB (&$) { return _if_reftype($_[1], GLOB => $_[0]); } | ||||||||||||||||||||||||||
|
Subroutine
if_GLOB: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 58 | 1 | ▶ | sub if_LVALUE (&$) { return _if_reftype($_[1], LVALUE => $_[0]); } | ||||||||||||||||||||||||||
|
Subroutine
if_LVALUE: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 59 | 1 | ▶ | sub if_FORMAT (&$) { return _if_reftype($_[1], FORMAT => $_[0]); } | ||||||||||||||||||||||||||
|
Subroutine
if_FORMAT: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 60 | 1 | ▶ | sub if_IO (&$) { return _if_reftype($_[1], IO => $_[0]); } | ||||||||||||||||||||||||||
|
Subroutine
if_IO: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 61 | 1 | ▶ | sub if_VSTRING (&$) { return _if_reftype($_[1], VSTRING => $_[0]); } | ||||||||||||||||||||||||||
|
Subroutine
if_VSTRING: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 62 | 1 | ▶ | sub if_REGEXP (&$) { return _if_reftype($_[1], REGEXP => $_[0]); } | ||||||||||||||||||||||||||
|
Subroutine
if_REGEXP: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 1 | 1; | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | __END__ | ||||||||||||||||||||||||||||