line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Class::AccessorMaker; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
## |
4
|
|
|
|
|
|
|
## Class::AccessorMaker by Hartog "Sinister" de Mik |
5
|
|
|
|
|
|
|
## |
6
|
|
|
|
|
|
|
|
7
|
4
|
|
|
4
|
|
2500
|
use strict; |
|
4
|
|
|
|
|
7
|
|
|
4
|
|
|
|
|
134
|
|
8
|
4
|
|
|
4
|
|
19
|
no strict 'refs'; |
|
4
|
|
|
|
|
6
|
|
|
4
|
|
|
|
|
134
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our $VERSION = "1.11"; |
11
|
|
|
|
|
|
|
|
12
|
4
|
|
|
4
|
|
30
|
use Carp; |
|
4
|
|
|
|
|
5
|
|
|
4
|
|
|
|
|
2556
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub import { |
15
|
6
|
|
|
6
|
|
58
|
my ($class, $subs, $xtra) = @_; |
16
|
6
|
|
33
|
|
|
35
|
my $pkg = ref(caller) || caller; |
17
|
6
|
|
100
|
|
|
20
|
$xtra ||=""; |
18
|
|
|
|
|
|
|
|
19
|
6
|
50
|
|
|
|
17
|
croak "Can't make methods out of an empty hash-ref\n" if !defined $subs; |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
## define a constructor |
22
|
6
|
100
|
|
|
|
17
|
if ( $xtra ne "no_new" ) { |
23
|
|
|
|
|
|
|
# we have a green light for 'new()' creation |
24
|
5
|
100
|
|
|
|
15
|
if ( $xtra ne "new_init" ) { |
|
|
50
|
|
|
|
|
|
25
|
4
|
|
|
|
|
20
|
*{"${pkg}::new"} = sub { |
26
|
4
|
|
33
|
4
|
|
44
|
my $class = ref($_[0]) || $_[0]; shift; |
|
4
|
|
|
|
|
6
|
|
27
|
4
|
|
|
|
|
10
|
my $self = bless({}, $class); |
28
|
|
|
|
|
|
|
|
29
|
4
|
|
|
|
|
14
|
while ( @_ ) { |
30
|
3
|
|
|
|
|
7
|
my ($sub, $value) = (shift, shift); |
31
|
3
|
|
|
|
|
30
|
$self->$sub($value); |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
|
34
|
4
|
|
|
|
|
18
|
return $self; |
35
|
4
|
|
|
|
|
15
|
}; |
36
|
|
|
|
|
|
|
} elsif ( $xtra eq "new_init" ) { |
37
|
1
|
|
|
|
|
5
|
*{"${pkg}::new"} = sub { |
38
|
1
|
|
33
|
1
|
|
16
|
my $class = ref($_[0]) || $_[0]; shift; |
|
1
|
|
|
|
|
2
|
|
39
|
1
|
|
|
|
|
4
|
my $self = bless({}, $class); |
40
|
|
|
|
|
|
|
|
41
|
1
|
|
|
|
|
6
|
while ( @_ ) { |
42
|
0
|
|
|
|
|
0
|
my ($sub, $value) = (shift, shift); |
43
|
0
|
|
|
|
|
0
|
$self->$sub($value); |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
1
|
|
|
|
|
51
|
$self->init(); |
47
|
1
|
|
|
|
|
9
|
return $self; |
48
|
1
|
|
|
|
|
8
|
}; |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
|
53
|
6
|
|
|
|
|
18
|
foreach my $sub ( keys %$subs ) { |
54
|
|
|
|
|
|
|
## construct the method if it is not defined yet. |
55
|
13
|
50
|
|
|
|
15
|
next if ( defined &{"${pkg}::$sub"} ); |
|
13
|
|
|
|
|
73
|
|
56
|
|
|
|
|
|
|
|
57
|
13
|
|
|
|
|
70
|
*{"${pkg}::$sub"} = sub { |
58
|
25
|
|
|
25
|
|
1206
|
my ( $self, $value ) = @_; |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
# fill with default at first run |
61
|
25
|
100
|
|
|
|
88
|
if ( !exists $self->{$sub} ) { |
62
|
12
|
|
|
|
|
46
|
my $val = $subs->{$sub}; |
63
|
12
|
|
|
|
|
22
|
my $rval = ref($val); |
64
|
12
|
100
|
|
|
|
43
|
if (!$rval) { |
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
65
|
|
|
|
|
|
|
#scalar |
66
|
6
|
|
|
|
|
15
|
$self->{$sub} = $val; |
67
|
|
|
|
|
|
|
} elsif ($rval =~ /ARRAY/) { |
68
|
3
|
|
|
|
|
5
|
my @val = @{$val}; |
|
3
|
|
|
|
|
8
|
|
69
|
3
|
|
|
|
|
7
|
$self->{$sub} = \@val; |
70
|
|
|
|
|
|
|
} elsif($rval =~ /HASH/) { |
71
|
3
|
|
|
|
|
6
|
my %val = %{$val}; |
|
3
|
|
|
|
|
10
|
|
72
|
3
|
|
|
|
|
11
|
$self->{$sub} = \%val; |
73
|
|
|
|
|
|
|
} else { |
74
|
|
|
|
|
|
|
#object ref.. use at own risk. |
75
|
0
|
|
|
|
|
0
|
$self->{$sub} = $val; |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
# more then just self, something has to be set. |
80
|
25
|
100
|
|
|
|
66
|
if ($#_ > 0) { |
81
|
10
|
50
|
66
|
|
|
53
|
warn "The value supplied to '$sub()' is not of propper type" |
82
|
|
|
|
|
|
|
if (ref($subs->{$sub}) and !ref($value)); |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
# set the value and return the object. |
85
|
10
|
|
|
|
|
18
|
$self->{$sub} = $value; |
86
|
10
|
|
|
|
|
46
|
return $self; |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
# return the value; |
90
|
15
|
|
|
|
|
72
|
return $self->{$sub}; |
91
|
|
|
|
|
|
|
|
92
|
13
|
50
|
|
|
|
43
|
} or warn "Method: $sub not implemented\n"; |
93
|
|
|
|
|
|
|
} |
94
|
6
|
|
|
|
|
249
|
return 1; # import succeeded |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
1; |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
__END__ |