line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Bubblegum::Object::Role::Coercive; |
2
|
|
|
|
|
|
|
|
3
|
36
|
|
|
36
|
|
17010
|
use 5.10.0; |
|
36
|
|
|
|
|
98
|
|
|
36
|
|
|
|
|
1397
|
|
4
|
36
|
|
|
36
|
|
157
|
use namespace::autoclean; |
|
36
|
|
|
|
|
45
|
|
|
36
|
|
|
|
|
284
|
|
5
|
36
|
|
|
36
|
|
1779
|
use Bubblegum::Role; |
|
36
|
|
|
|
|
52
|
|
|
36
|
|
|
|
|
244
|
|
6
|
|
|
|
|
|
|
|
7
|
36
|
|
|
36
|
|
24848
|
use Carp 'confess'; |
|
36
|
|
|
|
|
60
|
|
|
36
|
|
|
|
|
63005
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
our $VERSION = '0.45'; # VERSION |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
my $coercable = { |
12
|
|
|
|
|
|
|
'UNDEF' => { |
13
|
|
|
|
|
|
|
'UNDEF' => sub { $_[0] }, |
14
|
|
|
|
|
|
|
'CODE' => sub { my $this = $_[0]; sub { $this } }, |
15
|
|
|
|
|
|
|
'NUMBER' => sub { 0 }, |
16
|
|
|
|
|
|
|
'HASH' => sub { +{} }, |
17
|
|
|
|
|
|
|
'ARRAY' => sub { [undef] }, |
18
|
|
|
|
|
|
|
'STRING' => sub { "" }, |
19
|
|
|
|
|
|
|
}, |
20
|
|
|
|
|
|
|
'CODE' => { |
21
|
|
|
|
|
|
|
'UNDEF' => sub { undef }, |
22
|
|
|
|
|
|
|
'CODE' => sub { $_[0] }, |
23
|
|
|
|
|
|
|
'ARRAY' => sub { [$_[0]] }, |
24
|
|
|
|
|
|
|
'NUMBER' => sub { confess 'code to number coercion not possible' }, |
25
|
|
|
|
|
|
|
'HASH' => sub { confess 'code to hash coercion not possible' }, |
26
|
|
|
|
|
|
|
'STRING' => sub { confess 'code to string coercion not possible' }, |
27
|
|
|
|
|
|
|
}, |
28
|
|
|
|
|
|
|
'NUMBER' => { |
29
|
|
|
|
|
|
|
'UNDEF' => sub { undef }, |
30
|
|
|
|
|
|
|
'CODE' => sub { my $this = $_[0]; sub { $this } }, |
31
|
|
|
|
|
|
|
'NUMBER' => sub { $_[0] }, |
32
|
|
|
|
|
|
|
'HASH' => sub { +{ $_[0] => 1 } }, |
33
|
|
|
|
|
|
|
'ARRAY' => sub { [$_[0]] }, |
34
|
|
|
|
|
|
|
'STRING' => sub { "$_[0]" }, |
35
|
|
|
|
|
|
|
}, |
36
|
|
|
|
|
|
|
'HASH' => { |
37
|
|
|
|
|
|
|
'UNDEF' => sub { undef }, |
38
|
|
|
|
|
|
|
'CODE' => sub { my $this = $_[0]; sub { $this } }, |
39
|
|
|
|
|
|
|
'NUMBER' => sub { keys %{$_[0]} }, |
40
|
|
|
|
|
|
|
'HASH' => sub { $_[0] }, |
41
|
|
|
|
|
|
|
'ARRAY' => sub { [$_[0]] }, |
42
|
|
|
|
|
|
|
'STRING' => sub { $_[0]->dump }, |
43
|
|
|
|
|
|
|
}, |
44
|
|
|
|
|
|
|
'ARRAY' => { |
45
|
|
|
|
|
|
|
'UNDEF' => sub { undef }, |
46
|
|
|
|
|
|
|
'CODE' => sub { my $this = $_[0]; sub { $this } }, |
47
|
|
|
|
|
|
|
'NUMBER' => sub { scalar @{$_[0]} }, |
48
|
|
|
|
|
|
|
'HASH' => sub { +{ (@{$_[0]} % 2) ? (@{$_[0]}, undef) : @{$_[0]} } }, |
49
|
|
|
|
|
|
|
'ARRAY' => sub { $_[0] }, |
50
|
|
|
|
|
|
|
'STRING' => sub { $_[0]->dump }, |
51
|
|
|
|
|
|
|
}, |
52
|
|
|
|
|
|
|
'STRING' => { |
53
|
|
|
|
|
|
|
'UNDEF' => sub { undef }, |
54
|
|
|
|
|
|
|
'CODE' => sub { my $this = $_[0]; sub { $this } }, |
55
|
|
|
|
|
|
|
'NUMBER' => sub { 0 + (join('', $_[0] =~ /[\d\.]/g) || 0) }, |
56
|
|
|
|
|
|
|
'HASH' => sub { +{ $_[0] => 1 } }, |
57
|
|
|
|
|
|
|
'ARRAY' => sub { [$_[0]] }, |
58
|
|
|
|
|
|
|
'STRING' => sub { $_[0] }, |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
}; |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
$coercable->{INTEGER} = $coercable->{NUMBER}; |
63
|
|
|
|
|
|
|
$coercable->{FLOAT} = $coercable->{NUMBER}; |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub to_array { |
66
|
14
|
|
|
14
|
0
|
39139
|
my $self = shift; |
67
|
14
|
|
|
|
|
23
|
my $coerce = 'ARRAY'; |
68
|
14
|
50
|
|
|
|
74
|
return unless my $type = $self->type; |
69
|
14
|
|
|
|
|
56
|
return $coercable->{$type}{$coerce}->($self); |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
sub to_code { |
73
|
16
|
|
|
16
|
0
|
37674
|
my $self = shift; |
74
|
16
|
|
|
|
|
23
|
my $coerce = 'CODE'; |
75
|
16
|
50
|
|
|
|
60
|
return unless my $type = $self->type; |
76
|
16
|
|
|
|
|
54
|
return $coercable->{$type}{$coerce}->($self); |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
sub to_hash { |
80
|
14
|
|
|
14
|
0
|
37839
|
my $self = shift; |
81
|
14
|
|
|
|
|
25
|
my $coerce = 'HASH'; |
82
|
14
|
50
|
|
|
|
48
|
return unless my $type = $self->type; |
83
|
14
|
|
|
|
|
52
|
return $coercable->{$type}{$coerce}->($self); |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
sub to_number { |
87
|
15
|
|
|
15
|
0
|
38263
|
my $self = shift; |
88
|
15
|
|
|
|
|
24
|
my $coerce = 'NUMBER'; |
89
|
15
|
50
|
|
|
|
55
|
return unless my $type = $self->type; |
90
|
15
|
|
|
|
|
52
|
return $coercable->{$type}{$coerce}->($self); |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
sub to_string { |
94
|
14
|
|
|
14
|
0
|
38448
|
my $self = shift; |
95
|
14
|
|
|
|
|
25
|
my $coerce = 'STRING'; |
96
|
14
|
50
|
|
|
|
52
|
return unless my $type = $self->type; |
97
|
14
|
|
|
|
|
48
|
return $coercable->{$type}{$coerce}->($self); |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
sub to_undef { |
101
|
12
|
|
|
12
|
0
|
30888
|
my $self = shift; |
102
|
12
|
|
|
|
|
20
|
my $coerce = 'UNDEF'; |
103
|
12
|
50
|
|
|
|
41
|
return unless my $type = $self->type; |
104
|
12
|
|
|
|
|
38
|
return $coercable->{$type}{$coerce}->($self); |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
{ |
108
|
36
|
|
|
36
|
|
210
|
no warnings 'once'; |
|
36
|
|
|
|
|
46
|
|
|
36
|
|
|
|
|
4269
|
|
109
|
|
|
|
|
|
|
*to_a = \&to_array; |
110
|
|
|
|
|
|
|
*to_c = \&to_code; |
111
|
|
|
|
|
|
|
*to_h = \&to_hash; |
112
|
|
|
|
|
|
|
*to_n = \&to_number; |
113
|
|
|
|
|
|
|
*to_s = \&to_string; |
114
|
|
|
|
|
|
|
*to_u = \&to_undef; |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
1; |