line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Class::ExtraAttributes; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# version |
4
|
|
|
|
|
|
|
$VERSION= '1.00'; |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
# be as strict and verbose as possible |
7
|
2
|
|
|
2
|
|
2175
|
use strict; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
48
|
|
8
|
2
|
|
|
2
|
|
9
|
use warnings; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
49
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
# modules that we need |
11
|
2
|
|
|
2
|
|
922
|
use OOB qw( OOB_get OOB_set ); |
|
2
|
|
|
|
|
7836
|
|
|
2
|
|
|
|
|
10
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
# additionals attributes per class |
14
|
|
|
|
|
|
|
my %attributes; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
# satisfy -require- |
17
|
|
|
|
|
|
|
1; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
#------------------------------------------------------------------------------- |
20
|
|
|
|
|
|
|
# |
21
|
|
|
|
|
|
|
# Class Methods |
22
|
|
|
|
|
|
|
# |
23
|
|
|
|
|
|
|
#------------------------------------------------------------------------------- |
24
|
|
|
|
|
|
|
# attributes |
25
|
|
|
|
|
|
|
# |
26
|
|
|
|
|
|
|
# Return the extra attributes for the given class |
27
|
|
|
|
|
|
|
# |
28
|
|
|
|
|
|
|
# IN: 1 Class::ExtraAttributes (ignored) |
29
|
|
|
|
|
|
|
# 2 class to return extra attributes of (optional) |
30
|
|
|
|
|
|
|
# OUT: 1 list (or list ref) with extra attributes |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub attributes { |
33
|
|
|
|
|
|
|
# fetch attributes |
34
|
3
|
100
|
|
3
|
1
|
2529
|
my @attributes= keys %{ $attributes{ $_[1] ? $_[1] : caller() } || {} }; |
|
3
|
100
|
|
|
|
43
|
|
35
|
|
|
|
|
|
|
|
36
|
3
|
100
|
|
|
|
11
|
return wantarray ? @attributes : \@attributes; |
37
|
|
|
|
|
|
|
} #attributes |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
#------------------------------------------------------------------------------- |
40
|
|
|
|
|
|
|
# |
41
|
|
|
|
|
|
|
# Standard Perl features |
42
|
|
|
|
|
|
|
# |
43
|
|
|
|
|
|
|
#------------------------------------------------------------------------------- |
44
|
|
|
|
|
|
|
# import |
45
|
|
|
|
|
|
|
# |
46
|
|
|
|
|
|
|
# Export any constants requested |
47
|
|
|
|
|
|
|
# |
48
|
|
|
|
|
|
|
# IN: 1 class (ignored) |
49
|
|
|
|
|
|
|
# 2..N attributes to be defined |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
sub import { |
52
|
3
|
|
|
3
|
|
556
|
my $class= shift; |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
# nothing to export |
55
|
3
|
100
|
|
|
|
18
|
return if !@_; |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
# determine namespace to export to |
58
|
2
|
|
|
|
|
4
|
my $namespace= caller(); |
59
|
2
|
|
100
|
|
|
9
|
my $attributes= $attributes{$namespace} ||= {}; |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
# create accessor / mutator for given attributes |
62
|
2
|
|
|
2
|
|
230
|
no strict 'refs'; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
253
|
|
63
|
2
|
|
|
|
|
4
|
foreach my $method ( grep { !exists $attributes->{$_} } @_ ) { |
|
2
|
|
|
|
|
7
|
|
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
# make sure we don't cloak anything |
66
|
2
|
100
|
|
|
|
17
|
die "Can already do '$method' on class $namespace" |
67
|
|
|
|
|
|
|
if $namespace->can($method); |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
# install accessor / mutator |
70
|
1
|
|
|
|
|
2
|
$attributes->{$method}= undef; |
71
|
1
|
|
|
|
|
4
|
*{ $namespace . '::' . $method }= sub { |
72
|
2
|
100
|
|
2
|
|
668
|
return @_ == 2 |
73
|
|
|
|
|
|
|
? OOB_set( $_[0], $method => $_[1], $namespace ) |
74
|
|
|
|
|
|
|
: OOB_get( $_[0], $method, $namespace ); |
75
|
1
|
|
|
|
|
4
|
}; |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
1
|
|
|
|
|
2
|
return; |
79
|
|
|
|
|
|
|
} #import |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
#------------------------------------------------------------------------------- |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
__END__ |