line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
10
|
|
|
10
|
|
10247
|
use 5.006; |
|
9
|
|
|
|
|
21
|
|
2
|
9
|
|
|
9
|
|
27
|
use strict; |
|
9
|
|
|
|
|
10
|
|
|
9
|
|
|
|
|
141
|
|
3
|
9
|
|
|
9
|
|
23
|
no strict 'refs'; |
|
9
|
|
|
|
|
7
|
|
|
9
|
|
|
|
|
218
|
|
4
|
9
|
|
|
9
|
|
27
|
use warnings; |
|
9
|
|
|
|
|
14
|
|
|
9
|
|
|
|
|
387
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
package Class::Tiny; |
7
|
|
|
|
|
|
|
# ABSTRACT: Minimalist class construction |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
our $VERSION = '1.006'; |
10
|
|
|
|
|
|
|
|
11
|
9
|
|
|
9
|
|
26
|
use Carp (); |
|
9
|
|
|
|
|
9
|
|
|
9
|
|
|
|
|
6064
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
# load as .pm to hide from min version scanners |
14
|
|
|
|
|
|
|
require( $] >= 5.010 ? "mro.pm" : "MRO/Compat.pm" ); ## no critic: |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
my %CLASS_ATTRIBUTES; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
sub import { |
19
|
16
|
|
|
16
|
|
761
|
my $class = shift; |
20
|
16
|
|
|
|
|
27
|
my $pkg = caller; |
21
|
16
|
|
|
|
|
34
|
$class->prepare_class($pkg); |
22
|
16
|
100
|
|
|
|
72
|
$class->create_attributes( $pkg, @_ ) if @_; |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub prepare_class { |
26
|
16
|
|
|
16
|
0
|
17
|
my ( $class, $pkg ) = @_; |
27
|
16
|
100
|
|
|
|
15
|
@{"${pkg}::ISA"} = "Class::Tiny::Object" unless @{"${pkg}::ISA"}; |
|
9
|
|
|
|
|
76
|
|
|
16
|
|
|
|
|
73
|
|
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
# adapted from Object::Tiny and Object::Tiny::RW |
31
|
|
|
|
|
|
|
sub create_attributes { |
32
|
16
|
|
|
16
|
0
|
26
|
my ( $class, $pkg, @spec ) = @_; |
33
|
16
|
100
|
|
|
|
19
|
my %defaults = map { ref $_ eq 'HASH' ? %$_ : ( $_ => undef ) } @spec; |
|
31
|
|
|
|
|
108
|
|
34
|
|
|
|
|
|
|
my @attr = grep { |
35
|
16
|
50
|
66
|
|
|
36
|
defined and !ref and /^[^\W\d]\w*$/s |
|
35
|
|
33
|
|
|
362
|
|
36
|
|
|
|
|
|
|
or Carp::croak "Invalid accessor name '$_'" |
37
|
|
|
|
|
|
|
} keys %defaults; |
38
|
16
|
|
|
|
|
64
|
$CLASS_ATTRIBUTES{$pkg}{$_} = $defaults{$_} for @attr; |
39
|
16
|
|
|
|
|
19
|
$class->_gen_accessor( $pkg, $_ ) for grep { !*{"$pkg\::$_"}{CODE} } @attr; |
|
35
|
|
|
|
|
21
|
|
|
35
|
|
|
|
|
151
|
|
40
|
16
|
50
|
|
|
|
614
|
Carp::croak("Failed to generate attributes for $pkg: $@\n") if $@; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub _gen_accessor { |
44
|
33
|
|
|
33
|
|
43
|
my ( $class, $pkg, $name ) = @_; |
45
|
33
|
|
|
|
|
37
|
my $outer_default = $CLASS_ATTRIBUTES{$pkg}{$name}; |
46
|
|
|
|
|
|
|
|
47
|
33
|
|
|
|
|
55
|
my $sub = |
48
|
|
|
|
|
|
|
$class->__gen_sub_body( $name, defined($outer_default), ref($outer_default) ); |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
# default = outer_default avoids "won't stay shared" bug |
51
|
33
|
50
|
33
|
34
|
0
|
2705
|
eval "package $pkg; my \$default=\$outer_default; $sub"; ## no critic |
|
34
|
100
|
33
|
49
|
0
|
381
|
|
|
49
|
100
|
0
|
27
|
0
|
8679
|
|
|
27
|
100
|
|
12
|
0
|
128
|
|
|
12
|
50
|
|
0
|
0
|
348
|
|
|
0
|
100
|
|
0
|
0
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
52
|
33
|
50
|
|
|
|
137
|
Carp::croak("Failed to generate attributes for $pkg: $@\n") if $@; |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
# NOTE: overriding __gen_sub_body in a subclass of Class::Tiny is risky and |
56
|
|
|
|
|
|
|
# could break if the internals of Class::Tiny need to change for any |
57
|
|
|
|
|
|
|
# reason. That said, I currently see no reason why this would be likely to |
58
|
|
|
|
|
|
|
# change. |
59
|
|
|
|
|
|
|
# |
60
|
|
|
|
|
|
|
# The generated sub body should assume that a '$default' variable will be |
61
|
|
|
|
|
|
|
# in scope (i.e. when the sub is evaluated) with any default value/coderef |
62
|
|
|
|
|
|
|
sub __gen_sub_body { |
63
|
33
|
|
|
33
|
|
36
|
my ( $self, $name, $has_default, $default_type ) = @_; |
64
|
|
|
|
|
|
|
|
65
|
33
|
100
|
100
|
|
|
110
|
if ( $has_default && $default_type eq 'CODE' ) { |
|
|
100
|
|
|
|
|
|
66
|
4
|
|
|
|
|
16
|
return << "HERE"; |
67
|
|
|
|
|
|
|
sub $name { |
68
|
|
|
|
|
|
|
return ( |
69
|
|
|
|
|
|
|
( \@_ == 1 && exists \$_[0]{$name} ) |
70
|
|
|
|
|
|
|
? ( \$_[0]{$name} ) |
71
|
|
|
|
|
|
|
: ( \$_[0]{$name} = ( \@_ == 2 ) ? \$_[1] : \$default->( \$_[0] ) ) |
72
|
|
|
|
|
|
|
); |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
HERE |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
elsif ($has_default) { |
77
|
4
|
|
|
|
|
33
|
return << "HERE"; |
78
|
|
|
|
|
|
|
sub $name { |
79
|
|
|
|
|
|
|
return ( |
80
|
|
|
|
|
|
|
( \@_ == 1 && exists \$_[0]{$name} ) |
81
|
|
|
|
|
|
|
? ( \$_[0]{$name} ) |
82
|
|
|
|
|
|
|
: ( \$_[0]{$name} = ( \@_ == 2 ) ? \$_[1] : \$default ) |
83
|
|
|
|
|
|
|
); |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
HERE |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
else { |
88
|
25
|
|
|
|
|
69
|
return << "HERE"; |
89
|
|
|
|
|
|
|
sub $name { |
90
|
|
|
|
|
|
|
return \@_ == 1 ? \$_[0]{$name} : ( \$_[0]{$name} = \$_[1] ); |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
HERE |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
sub get_all_attributes_for { |
97
|
13
|
|
|
13
|
0
|
4459
|
my ( $class, $pkg ) = @_; |
98
|
|
|
|
|
|
|
my %attr = |
99
|
54
|
|
|
|
|
68
|
map { $_ => undef } |
100
|
13
|
100
|
|
|
|
14
|
map { keys %{ $CLASS_ATTRIBUTES{$_} || {} } } @{ mro::get_linear_isa($pkg) }; |
|
37
|
|
|
|
|
24
|
|
|
37
|
|
|
|
|
133
|
|
|
13
|
|
|
|
|
54
|
|
101
|
13
|
|
|
|
|
72
|
return keys %attr; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
sub get_all_attribute_defaults_for { |
105
|
3
|
|
|
3
|
0
|
3170
|
my ( $class, $pkg ) = @_; |
106
|
3
|
|
|
|
|
10
|
my $defaults = {}; |
107
|
3
|
|
|
|
|
5
|
for my $p ( reverse @{ mro::get_linear_isa($pkg) } ) { |
|
3
|
|
|
|
|
11
|
|
108
|
7
|
100
|
|
|
|
8
|
while ( my ( $k, $v ) = each %{ $CLASS_ATTRIBUTES{$p} || {} } ) { |
|
21
|
|
|
|
|
64
|
|
109
|
14
|
|
|
|
|
15
|
$defaults->{$k} = $v; |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
} |
112
|
3
|
|
|
|
|
9
|
return $defaults; |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
package Class::Tiny::Object; |
116
|
|
|
|
|
|
|
# ABSTRACT: Base class for classes built with Class::Tiny |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
our $VERSION = '1.006'; |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
my ( %HAS_BUILDARGS, %BUILD_CACHE, %DEMOLISH_CACHE, %ATTR_CACHE ); |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
my $_PRECACHE = sub { |
123
|
9
|
|
|
9
|
|
36
|
no warnings 'once'; # needed to avoid downstream warnings |
|
9
|
|
|
|
|
14
|
|
|
9
|
|
|
|
|
4002
|
|
124
|
|
|
|
|
|
|
my ($class) = @_; |
125
|
|
|
|
|
|
|
my $linear_isa = |
126
|
|
|
|
|
|
|
@{"$class\::ISA"} == 1 && ${"$class\::ISA"}[0] eq "Class::Tiny::Object" |
127
|
|
|
|
|
|
|
? [$class] |
128
|
|
|
|
|
|
|
: mro::get_linear_isa($class); |
129
|
|
|
|
|
|
|
$DEMOLISH_CACHE{$class} = [ |
130
|
|
|
|
|
|
|
map { ( *{$_}{CODE} ) ? ( *{$_}{CODE} ) : () } |
131
|
|
|
|
|
|
|
map { "$_\::DEMOLISH" } @$linear_isa |
132
|
|
|
|
|
|
|
]; |
133
|
|
|
|
|
|
|
$BUILD_CACHE{$class} = [ |
134
|
|
|
|
|
|
|
map { ( *{$_}{CODE} ) ? ( *{$_}{CODE} ) : () } |
135
|
|
|
|
|
|
|
map { "$_\::BUILD" } reverse @$linear_isa |
136
|
|
|
|
|
|
|
]; |
137
|
|
|
|
|
|
|
$HAS_BUILDARGS{$class} = $class->can("BUILDARGS"); |
138
|
|
|
|
|
|
|
return $ATTR_CACHE{$class} = |
139
|
|
|
|
|
|
|
{ map { $_ => 1 } Class::Tiny->get_all_attributes_for($class) }; |
140
|
|
|
|
|
|
|
}; |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
sub new { |
143
|
41
|
|
|
41
|
|
36753
|
my $class = shift; |
144
|
41
|
|
66
|
|
|
127
|
my $valid_attrs = $ATTR_CACHE{$class} || $_PRECACHE->($class); |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
# handle hash ref or key/value arguments |
147
|
41
|
|
|
|
|
37
|
my $args; |
148
|
41
|
50
|
|
|
|
64
|
if ( $HAS_BUILDARGS{$class} ) { |
149
|
0
|
|
|
|
|
0
|
$args = $class->BUILDARGS(@_); |
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
else { |
152
|
41
|
100
|
66
|
|
|
161
|
if ( @_ == 1 && ref $_[0] ) { |
|
|
100
|
|
|
|
|
|
153
|
11
|
|
|
|
|
27
|
my %copy = eval { %{ $_[0] } }; # try shallow copy |
|
11
|
|
|
|
|
10
|
|
|
11
|
|
|
|
|
40
|
|
154
|
11
|
100
|
|
|
|
128
|
Carp::croak("Argument to $class->new() could not be dereferenced as a hash") if $@; |
155
|
10
|
|
|
|
|
16
|
$args = \%copy; |
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
elsif ( @_ % 2 == 0 ) { |
158
|
29
|
|
|
|
|
59
|
$args = {@_}; |
159
|
|
|
|
|
|
|
} |
160
|
|
|
|
|
|
|
else { |
161
|
1
|
|
|
|
|
182
|
Carp::croak("$class->new() got an odd number of elements"); |
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
} |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
# create object and invoke BUILD (unless we were given __no_BUILD__) |
166
|
|
|
|
|
|
|
my $self = |
167
|
39
|
|
|
|
|
73
|
bless { map { $_ => $args->{$_} } grep { exists $valid_attrs->{$_} } keys %$args }, |
|
59
|
|
|
|
|
101
|
|
|
61
|
|
|
|
|
94
|
|
168
|
|
|
|
|
|
|
$class; |
169
|
39
|
100
|
100
|
|
|
102
|
$self->BUILDALL($args) if !delete $args->{__no_BUILD__} && @{ $BUILD_CACHE{$class} }; |
|
38
|
|
|
|
|
123
|
|
170
|
|
|
|
|
|
|
|
171
|
37
|
|
|
|
|
91
|
return $self; |
172
|
|
|
|
|
|
|
} |
173
|
|
|
|
|
|
|
|
174
|
10
|
|
|
10
|
|
20
|
sub BUILDALL { $_->(@_) for @{ $BUILD_CACHE{ ref $_[0] } } } |
|
10
|
|
|
|
|
35
|
|
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
# Adapted from Moo and its dependencies |
177
|
|
|
|
|
|
|
require Devel::GlobalDestruction unless defined ${^GLOBAL_PHASE}; |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
sub DESTROY { |
180
|
39
|
|
|
39
|
|
11315
|
my $self = shift; |
181
|
39
|
|
|
|
|
51
|
my $class = ref $self; |
182
|
39
|
50
|
|
|
|
108
|
my $in_global_destruction = |
183
|
|
|
|
|
|
|
defined ${^GLOBAL_PHASE} |
184
|
|
|
|
|
|
|
? ${^GLOBAL_PHASE} eq 'DESTRUCT' |
185
|
|
|
|
|
|
|
: Devel::GlobalDestruction::in_global_destruction(); |
186
|
39
|
|
|
|
|
29
|
for my $demolisher ( @{ $DEMOLISH_CACHE{$class} } ) { |
|
39
|
|
|
|
|
134
|
|
187
|
16
|
|
|
|
|
11
|
my $e = do { |
188
|
16
|
|
|
|
|
24
|
local ( $?, $@ ); |
189
|
16
|
|
|
|
|
11
|
eval { $demolisher->( $self, $in_global_destruction ) }; |
|
16
|
|
|
|
|
23
|
|
190
|
16
|
|
|
|
|
71
|
$@; |
191
|
|
|
|
|
|
|
}; |
192
|
9
|
|
|
9
|
|
33
|
no warnings 'misc'; # avoid (in cleanup) warnings |
|
9
|
|
|
|
|
14
|
|
|
9
|
|
|
|
|
506
|
|
193
|
16
|
50
|
|
|
|
51
|
die $e if $e; # rethrow |
194
|
|
|
|
|
|
|
} |
195
|
|
|
|
|
|
|
} |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
1; |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
# vim: ts=4 sts=4 sw=4 et: |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
__END__ |