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