README
1NAME
2 Class::Data::Accessor - Inheritable, overridable class and instance data
3 accessor creation
4
5SYNOPSIS
6 package Stuff;
7 use base qw(Class::Data::Accessor);
8
9 # Set up DataFile as inheritable class data.
10 Stuff->mk_classaccessor('DataFile');
11
12 # Declare the location of the data file for this class.
13 Stuff->DataFile('/etc/stuff/data');
14
15 # Or, all in one shot:
16 Stuff->mk_classaccessor(DataFile => '/etc/stuff/data');
17
18
19 Stuff->DataFile; # returns /etc/stuff/data
20
21 my $stuff = Stuff->new; # your new, not ours
22
23 $stuff->DataFile; # returns /etc/stuff/data
24
25 $stuff->DataFile('/etc/morestuff'); # sets it on the object
26
27 Stuff->DataFile; # still returns /etc/stuff/data
28
29DESCRIPTION
30 This module is now deprecated!
31
32 Please consider using Class::Accessor::Grouped or Moose
33
34 Class::Data::Accessor is the marriage of Class::Accessor and
35 Class::Data::Inheritable into a single module. It is used for creating
36 accessors to class data that overridable in subclasses as well as in
37 class instances.
38
39 For example:
40
41 Pere::Ubu->mk_classaccessor('Suitcase');
42
43 will generate the method Suitcase() in the class Pere::Ubu.
44
45 This new method can be used to get and set a piece of class data.
46
47 Pere::Ubu->Suitcase('Red');
48 $suitcase = Pere::Ubu->Suitcase;
49
50 Taking this one step further, you can make a subclass that inherits from
51 Pere::Ubu:
52
53 package Raygun;
54 use base qw(Pere::Ubu);
55
56 # Raygun's suitcase is Red.
57 $suitcase = Raygun->Suitcase;
58
59 Raygun inherits its Suitcase class data from Pere::Ubu.
60
61 Inheritance of class data works analogous to method inheritance. As long
62 as Raygun does not "override" its inherited class data (by using
63 Suitcase() to set a new value) it will continue to use whatever is set
64 in Pere::Ubu and inherit further changes:
65
66 # Both Raygun's and Pere::Ubu's suitcases are now Blue
67 Pere::Ubu->Suitcase('Blue');
68
69 However, should Raygun decide to set its own Suitcase() it has now
70 "overridden" Pere::Ubu and is on its own, just like if it had overridden
71 a method:
72
73 # Raygun has an orange suitcase, Pere::Ubu's is still Blue.
74 Raygun->Suitcase('Orange');
75
76 Now that Raygun has overridden Pere::Ubu, further changes by Pere::Ubu
77 no longer effect Raygun.
78
79 # Raygun still has an orange suitcase, but Pere::Ubu is using Samsonite.
80 Pere::Ubu->Suitcase('Samsonite');
81
82 You can also override this class data on a per-object basis. If $obj isa
83 Pere::Ubu then
84
85 $obj->Suitcase; # will return Samsonite
86
87 $obj->Suitcase('Purple'); # will set Suitcase *for this object only*
88
89 And after you've done that,
90
91 $obj->Suitcase; # will return Purple
92
93 but
94
95 Pere::Ubu->Suitcase; # will still return Samsonite
96
97 If you don't want this behaviour use Class::Data::Inheritable instead.
98
99 "mk_classaccessor" will die if used as an object method instead of as a
100 class method.
101
102METHODS
103 mk_classaccessor
104 Class->mk_classaccessor($data_accessor_name);
105 Class->mk_classaccessor($data_accessor_name => $value);
106
107 This is a class method used to declare new class data accessors. A new
108 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_classaccessor 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(). This
115 is useful if you want to alter the behavior of a single accessor yet
116 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 Overriding accessors does not work in the same class as you declare the
126 accessor in. It only works in subclasses due to the fact that
127 subroutines are loaded at compile time and accessors are loaded at
128 runtime, thus overriding any subroutines with the same name in the same
129 class.
130
131 mk_classaccessors(@accessornames)
132 Takes a list of names and generates an accessor for each name in the
133 list using "mk_classaccessor".
134
135AUTHORS
136 Based on the creative stylings of Damian Conway, Michael G Schwern, Tony
137 Bowden (Class::Data::Inheritable) and Michael G Schwern, Marty Pauley
138 (Class::Accessor).
139
140 Coded by Matt S Trout Tweaks by Christopher H. Laco.
141
142BUGS and QUERIES
143 If your object isn't hash-based, this will currently break. My
144 modifications aren't exactly sophisticated so far.
145
146 mstrout@cpan.org or bug me on irc.perl.org, nick mst claco@cpan.org or
147 irc.perl.org, nick claco
148
149LICENSE
150 This module is free software. It may be used, redistributed and/or
151 modified under the same terms as Perl itself. (see
152 http://dev.perl.org/licenses/artistic.html and
153 http://www.opensource.org/licenses/gpl-license.php)
154
155SEE ALSO
156 perltootc has a very elaborate discussion of class data in Perl.
157 Class::Accessor, Class::Data::Inheritable
158
159