| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Class::Data::Inheritable; |
|
2
|
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
317580
|
use strict qw(vars subs); |
|
|
2
|
|
|
|
|
2
|
|
|
|
2
|
|
|
|
|
462
|
|
|
4
|
|
|
|
|
|
|
our $VERSION = '0.10'; |
|
5
|
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
sub mk_classdata { |
|
7
|
5
|
|
|
5
|
1
|
142088
|
my ($declaredclass, $attribute, $data) = @_; |
|
8
|
|
|
|
|
|
|
|
|
9
|
5
|
100
|
|
|
|
10
|
if( ref $declaredclass ) { |
|
10
|
1
|
|
|
|
|
7
|
require Carp; |
|
11
|
1
|
|
|
|
|
199
|
Carp::croak("mk_classdata() is a class method, not an object method"); |
|
12
|
|
|
|
|
|
|
} |
|
13
|
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
my $accessor = sub { |
|
15
|
15
|
|
66
|
15
|
|
1477
|
my $wantclass = ref($_[0]) || $_[0]; |
|
16
|
|
|
|
|
|
|
|
|
17
|
15
|
100
|
100
|
|
|
41
|
return $wantclass->mk_classdata($attribute)->(@_) |
|
18
|
|
|
|
|
|
|
if @_>1 && $wantclass ne $declaredclass; |
|
19
|
|
|
|
|
|
|
|
|
20
|
13
|
100
|
|
|
|
23
|
$data = $_[1] if @_>1; |
|
21
|
13
|
|
|
|
|
40
|
return $data; |
|
22
|
4
|
|
|
|
|
12
|
}; |
|
23
|
|
|
|
|
|
|
|
|
24
|
4
|
|
|
|
|
17
|
my $alias = "_${attribute}_accessor"; |
|
25
|
4
|
|
|
|
|
5
|
*{$declaredclass.'::'.$attribute} = $accessor; |
|
|
4
|
|
|
|
|
17
|
|
|
26
|
4
|
|
|
|
|
5
|
*{$declaredclass.'::'.$alias} = $accessor; |
|
|
4
|
|
|
|
|
21
|
|
|
27
|
|
|
|
|
|
|
} |
|
28
|
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
1; |
|
30
|
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
__END__ |
|
32
|
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
=head1 NAME |
|
34
|
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
Class::Data::Inheritable - Inheritable, overridable class data |
|
36
|
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
38
|
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
package Stuff; |
|
40
|
|
|
|
|
|
|
use base qw(Class::Data::Inheritable); |
|
41
|
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
# Set up DataFile as inheritable class data. |
|
43
|
|
|
|
|
|
|
Stuff->mk_classdata('DataFile'); |
|
44
|
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
# Declare the location of the data file for this class. |
|
46
|
|
|
|
|
|
|
Stuff->DataFile('/etc/stuff/data'); |
|
47
|
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
# Or, all in one shot: |
|
49
|
|
|
|
|
|
|
Stuff->mk_classdata(DataFile => '/etc/stuff/data'); |
|
50
|
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
52
|
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
Class::Data::Inheritable is for creating accessor/mutators to class |
|
54
|
|
|
|
|
|
|
data. That is, if you want to store something about your class as a |
|
55
|
|
|
|
|
|
|
whole (instead of about a single object). This data is then inherited |
|
56
|
|
|
|
|
|
|
by your subclasses and can be overridden. |
|
57
|
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
For example: |
|
59
|
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
Pere::Ubu->mk_classdata('Suitcase'); |
|
61
|
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
will generate the method Suitcase() in the class Pere::Ubu. |
|
63
|
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
This new method can be used to get and set a piece of class data. |
|
65
|
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
Pere::Ubu->Suitcase('Red'); |
|
67
|
|
|
|
|
|
|
$suitcase = Pere::Ubu->Suitcase; |
|
68
|
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
The interesting part happens when a class inherits from Pere::Ubu: |
|
70
|
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
package Raygun; |
|
72
|
|
|
|
|
|
|
use base qw(Pere::Ubu); |
|
73
|
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
# Raygun's suitcase is Red. |
|
75
|
|
|
|
|
|
|
$suitcase = Raygun->Suitcase; |
|
76
|
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
Raygun inherits its Suitcase class data from Pere::Ubu. |
|
78
|
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
Inheritance of class data works analogous to method inheritance. As |
|
80
|
|
|
|
|
|
|
long as Raygun does not "override" its inherited class data (by using |
|
81
|
|
|
|
|
|
|
Suitcase() to set a new value) it will continue to use whatever is set |
|
82
|
|
|
|
|
|
|
in Pere::Ubu and inherit further changes: |
|
83
|
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
# Both Raygun's and Pere::Ubu's suitcases are now Blue |
|
85
|
|
|
|
|
|
|
Pere::Ubu->Suitcase('Blue'); |
|
86
|
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
However, should Raygun decide to set its own Suitcase() it has now |
|
88
|
|
|
|
|
|
|
"overridden" Pere::Ubu and is on its own, just like if it had |
|
89
|
|
|
|
|
|
|
overridden a method: |
|
90
|
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
# Raygun has an orange suitcase, Pere::Ubu's is still Blue. |
|
92
|
|
|
|
|
|
|
Raygun->Suitcase('Orange'); |
|
93
|
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
Now that Raygun has overridden Pere::Ubu further changes by Pere::Ubu |
|
95
|
|
|
|
|
|
|
no longer effect Raygun. |
|
96
|
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
# Raygun still has an orange suitcase, but Pere::Ubu is using Samsonite. |
|
98
|
|
|
|
|
|
|
Pere::Ubu->Suitcase('Samsonite'); |
|
99
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=head1 Methods |
|
101
|
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=head2 mk_classdata |
|
103
|
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
Class->mk_classdata($data_accessor_name); |
|
105
|
|
|
|
|
|
|
Class->mk_classdata($data_accessor_name => $value); |
|
106
|
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
This is a class method used to declare new class data accessors. |
|
108
|
|
|
|
|
|
|
A new accessor will be created in the Class using the name from |
|
109
|
|
|
|
|
|
|
$data_accessor_name, and optionally initially setting it to the given |
|
110
|
|
|
|
|
|
|
value. |
|
111
|
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
To facilitate overriding, mk_classdata creates an alias to the |
|
113
|
|
|
|
|
|
|
accessor, _field_accessor(). So Suitcase() would have an alias |
|
114
|
|
|
|
|
|
|
_Suitcase_accessor() that does the exact same thing as Suitcase(). |
|
115
|
|
|
|
|
|
|
This is useful if you want to alter the behavior of a single accessor |
|
116
|
|
|
|
|
|
|
yet still get the benefits of inheritable class data. For example. |
|
117
|
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
sub Suitcase { |
|
119
|
|
|
|
|
|
|
my($self) = shift; |
|
120
|
|
|
|
|
|
|
warn "Fashion tragedy" if @_ and $_[0] eq 'Plaid'; |
|
121
|
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
$self->_Suitcase_accessor(@_); |
|
123
|
|
|
|
|
|
|
} |
|
124
|
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
=head1 AUTHOR |
|
126
|
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
Original code by Damian Conway. |
|
128
|
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
Maintained by Michael G Schwern until September 2005. |
|
130
|
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
Now maintained by Tony Bowden. |
|
132
|
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
=head1 BUGS and QUERIES |
|
134
|
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
Please direct all correspondence regarding this module to: |
|
136
|
|
|
|
|
|
|
bug-Class-Data-Inheritable@rt.cpan.org |
|
137
|
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=head1 COPYRIGHT and LICENSE |
|
139
|
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
Copyright (c) 2000-2005, Damian Conway and Michael G Schwern. |
|
141
|
|
|
|
|
|
|
All Rights Reserved. |
|
142
|
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
This module is free software. It may be used, redistributed and/or |
|
144
|
|
|
|
|
|
|
modified under the same terms as Perl itself. |
|
145
|
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=head1 SEE ALSO |
|
147
|
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
L<perltooc> has a very elaborate discussion of class data in Perl. |
|
149
|
|
|
|
|
|
|
|