line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Switch::Reftype; |
2
|
|
|
|
|
|
|
$Switch::Reftype::VERSION = '0.001'; |
3
|
|
|
|
|
|
|
# ABSTRACT: Execute code based on which type of reference is given. |
4
|
1
|
|
|
1
|
|
21783
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
33
|
|
5
|
1
|
|
|
1
|
|
7
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
38
|
|
6
|
1
|
|
|
1
|
|
6
|
use Scalar::Util 'reftype'; |
|
1
|
|
|
|
|
8
|
|
|
1
|
|
|
|
|
932
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
require Exporter; |
9
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
10
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
our @EXPORT_OK = map { @{$EXPORT_TAGS{$_}} } keys %EXPORT_TAGS; |
25
|
|
|
|
|
|
|
our @EXPORT = qw(switch_reftype); |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
sub switch_reftype { |
29
|
14
|
|
|
14
|
1
|
6421
|
my $ref = shift; |
30
|
14
|
|
|
|
|
37
|
my %dispatch = @_; |
31
|
14
|
|
100
|
|
|
91
|
my $reftype = defined($ref) && (reftype($ref) || "scalar") || "undef"; |
32
|
14
|
|
|
|
|
34
|
for ($ref) { # Alias $ref to $_ |
33
|
14
|
|
|
|
|
20
|
for my $key ($reftype, 'default') { |
34
|
16
|
100
|
|
|
|
38
|
if (exists $dispatch{$key}) { |
35
|
14
|
|
|
|
|
20
|
my $result = $dispatch{$key}; |
36
|
14
|
50
|
|
|
|
59
|
$result = $result->() if reftype $result eq "CODE"; |
37
|
14
|
|
|
|
|
142
|
return $result; |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
}; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sub _if_reftype { |
45
|
11
|
|
|
11
|
|
17
|
my $ref = shift; |
46
|
|
|
|
|
|
|
switch_reftype $ref, |
47
|
|
|
|
|
|
|
@_, |
48
|
0
|
|
|
0
|
|
0
|
default => sub { $ref } |
49
|
11
|
|
|
|
|
40
|
; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
1
|
|
|
1
|
1
|
50
|
sub if_SCALAR (&$) { return _if_reftype($_[1], SCALAR => $_[0]); } |
53
|
1
|
|
|
1
|
1
|
4
|
sub if_ARRAY (&$) { return _if_reftype($_[1], ARRAY => $_[0]); } |
54
|
1
|
|
|
1
|
1
|
3
|
sub if_HASH (&$) { return _if_reftype($_[1], HASH => $_[0]); } |
55
|
1
|
|
|
1
|
1
|
4
|
sub if_CODE (&$) { return _if_reftype($_[1], CODE => $_[0]); } |
56
|
1
|
|
|
1
|
1
|
4
|
sub if_REF (&$) { return _if_reftype($_[1], REF => $_[0]); } |
57
|
1
|
|
|
1
|
1
|
3
|
sub if_GLOB (&$) { return _if_reftype($_[1], GLOB => $_[0]); } |
58
|
1
|
|
|
1
|
1
|
2
|
sub if_LVALUE (&$) { return _if_reftype($_[1], LVALUE => $_[0]); } |
59
|
1
|
|
|
1
|
1
|
3
|
sub if_FORMAT (&$) { return _if_reftype($_[1], FORMAT => $_[0]); } |
60
|
1
|
|
|
1
|
1
|
3
|
sub if_IO (&$) { return _if_reftype($_[1], IO => $_[0]); } |
61
|
1
|
|
|
1
|
1
|
4
|
sub if_VSTRING (&$) { return _if_reftype($_[1], VSTRING => $_[0]); } |
62
|
1
|
|
|
1
|
1
|
3
|
sub if_REGEXP (&$) { return _if_reftype($_[1], REGEXP => $_[0]); } |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
1; |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
__END__ |