line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojar::ClassShare; |
2
|
2
|
|
|
2
|
|
7565
|
use strict; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
43
|
|
3
|
2
|
|
|
2
|
|
5
|
use warnings; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
58
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
our $VERSION = 0.011; |
6
|
|
|
|
|
|
|
# Adapted from Mojo::Base::attr |
7
|
|
|
|
|
|
|
|
8
|
2
|
|
|
2
|
|
6
|
use Carp 'croak'; |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
108
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
sub import { |
11
|
2
|
|
|
2
|
|
13
|
my $class = shift; |
12
|
2
|
100
|
66
|
|
|
16
|
if (@_ and shift eq 'have') { |
13
|
1
|
|
|
|
|
2
|
my $caller = caller; |
14
|
2
|
|
|
2
|
|
6
|
no strict 'refs'; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
508
|
|
15
|
1
|
|
|
3
|
|
3
|
*{"${caller}::have"} = sub { class_attr($caller, @_) }; |
|
1
|
|
|
|
|
52
|
|
|
3
|
|
|
|
|
14
|
|
16
|
|
|
|
|
|
|
} |
17
|
|
|
|
|
|
|
} |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
# Public method |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub class_attr { |
22
|
3
|
|
|
3
|
0
|
5
|
my ($class, $attrs, $default) = @_; |
23
|
3
|
50
|
33
|
|
|
19
|
return unless ($class = ref $class || $class) && $attrs; |
|
|
|
33
|
|
|
|
|
24
|
|
|
|
|
|
|
|
25
|
3
|
50
|
66
|
|
|
9
|
croak 'Default has to be a code reference or constant value' |
26
|
|
|
|
|
|
|
if ref $default && ref $default ne 'CODE'; |
27
|
|
|
|
|
|
|
|
28
|
3
|
50
|
|
|
|
4
|
for my $attr (@{ref $attrs eq 'ARRAY' ? $attrs : [$attrs]}) { |
|
3
|
|
|
|
|
7
|
|
29
|
3
|
50
|
|
|
|
13
|
croak qq{Attribute "$attr" invalid} unless $attr =~ /^[a-zA-Z_]\w*$/; |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
# Header (check arguments) |
32
|
3
|
|
|
|
|
6
|
my $code = "package $class;\nsub $attr {\n no strict 'refs';\n"; |
33
|
3
|
|
|
|
|
4
|
$code .= " if (\@_ == 1) {\n"; |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
# No default value (return value) |
36
|
3
|
100
|
|
|
|
4
|
unless (defined $default) { $code .= " return \$_[0]{'$attr'};" } |
|
1
|
|
|
|
|
2
|
|
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
# Default value |
39
|
|
|
|
|
|
|
else { |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
# Return value |
42
|
2
|
|
|
|
|
4
|
$code .= " return \$_[0]{'$attr'} if exists \$_[0]{'$attr'};\n"; |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
# Return default value |
45
|
2
|
|
|
|
|
2
|
$code .= " return \$_[0]{'$attr'} = "; |
46
|
2
|
100
|
|
|
|
5
|
$code .= ref $default eq 'CODE' ? '$default->($_[0]);' : '$default;'; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
# Store value |
50
|
3
|
|
|
|
|
5
|
$code .= "\n }\n \$_[0]{'$attr'} = \$_[1];\n"; |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
# Footer (return invocant) |
53
|
3
|
|
|
|
|
1
|
$code .= " \$_[0];\n}"; |
54
|
|
|
|
|
|
|
|
55
|
3
|
50
|
|
|
|
7
|
warn "-- Attribute $attr in $class\n$code\n\n" if $ENV{MOJO_BASE_DEBUG}; |
56
|
3
|
50
|
|
18
|
|
162
|
croak "Mojo::Base error: $@" unless eval "$code;1"; |
|
18
|
100
|
|
15
|
|
4235
|
|
|
11
|
100
|
|
4
|
|
36
|
|
|
8
|
100
|
|
|
|
68
|
|
|
8
|
100
|
|
|
|
21
|
|
|
15
|
50
|
|
|
|
837
|
|
|
11
|
|
|
|
|
104
|
|
|
3
|
|
|
|
|
10
|
|
|
5
|
|
|
|
|
6
|
|
|
5
|
|
|
|
|
136
|
|
|
3
|
|
|
|
|
8
|
|
|
3
|
|
|
|
|
10
|
|
|
2
|
|
|
|
|
3
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
1; |
61
|
|
|
|
|
|
|
__END__ |