line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojar::ClassShare; |
2
|
2
|
|
|
2
|
|
8655
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
55
|
|
3
|
2
|
|
|
2
|
|
9
|
use warnings; |
|
2
|
|
|
|
|
14
|
|
|
2
|
|
|
|
|
79
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
our $VERSION = 0.011; |
6
|
|
|
|
|
|
|
# Adapted from Mojo::Base::attr |
7
|
|
|
|
|
|
|
|
8
|
2
|
|
|
2
|
|
9
|
use Carp 'croak'; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
127
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
sub import { |
11
|
2
|
|
|
2
|
|
13
|
my $class = shift; |
12
|
2
|
100
|
66
|
|
|
19
|
if (@_ and shift eq 'have') { |
13
|
1
|
|
|
|
|
1
|
my $caller = caller; |
14
|
2
|
|
|
2
|
|
7
|
no strict 'refs'; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
557
|
|
15
|
1
|
|
|
3
|
|
3
|
*{"${caller}::have"} = sub { class_attr($caller, @_) }; |
|
1
|
|
|
|
|
60
|
|
|
3
|
|
|
|
|
17
|
|
16
|
|
|
|
|
|
|
} |
17
|
|
|
|
|
|
|
} |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
# Public method |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub class_attr { |
22
|
3
|
|
|
3
|
0
|
5
|
my ($class, $attrs, $default) = @_; |
23
|
3
|
50
|
33
|
|
|
22
|
return unless ($class = ref $class || $class) && $attrs; |
|
|
|
33
|
|
|
|
|
24
|
|
|
|
|
|
|
|
25
|
3
|
50
|
66
|
|
|
11
|
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
|
|
|
|
|
8
|
|
29
|
3
|
50
|
|
|
|
14
|
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
|
|
|
|
|
3
|
$code .= " return \$_[0]{'$attr'} = "; |
46
|
2
|
100
|
|
|
|
4
|
$code .= ref $default eq 'CODE' ? '$default->($_[0]);' : '$default;'; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
# Store value |
50
|
3
|
|
|
|
|
4
|
$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
|
|
4577
|
|
|
11
|
100
|
|
4
|
|
41
|
|
|
8
|
100
|
|
|
|
85
|
|
|
8
|
100
|
|
|
|
21
|
|
|
15
|
50
|
|
|
|
870
|
|
|
11
|
|
|
|
|
135
|
|
|
3
|
|
|
|
|
12
|
|
|
5
|
|
|
|
|
7
|
|
|
5
|
|
|
|
|
163
|
|
|
3
|
|
|
|
|
12
|
|
|
3
|
|
|
|
|
11
|
|
|
2
|
|
|
|
|
5
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
1; |
61
|
|
|
|
|
|
|
__END__ |