1Class::Data::Accessor(3U)ser Contributed Perl DocumentatiColnass::Data::Accessor(3)
2
3
4
6 Class::Data::Accessor - Inheritable, overridable class and instance
7 data accessor creation
8
10 package Stuff;
11 use base qw(Class::Data::Accessor);
12
13 # Set up DataFile as inheritable class data.
14 Stuff->mk_classaccessor('DataFile');
15
16 # Declare the location of the data file for this class.
17 Stuff->DataFile('/etc/stuff/data');
18
19 # Or, all in one shot:
20 Stuff->mk_classaccessor(DataFile => '/etc/stuff/data');
21
22 Stuff->DataFile; # returns /etc/stuff/data
23
24 my $stuff = Stuff->new; # your new, not ours
25
26 $stuff->DataFile; # returns /etc/stuff/data
27
28 $stuff->DataFile('/etc/morestuff'); # sets it on the object
29
30 Stuff->DataFile; # still returns /etc/stuff/data
31
33 Class::Data::Accessor is the marriage of Class::Accessor and
34 Class::Data::Inheritable into a single module. It is used for creating
35 accessors to class data that overridable in subclasses as well as in
36 class instances.
37
38 For example:
39
40 Pere::Ubu->mk_classaccessor('Suitcase');
41
42 will generate the method Suitcase() in the class Pere::Ubu.
43
44 This new method can be used to get and set a piece of class data.
45
46 Pere::Ubu->Suitcase('Red');
47 $suitcase = Pere::Ubu->Suitcase;
48
49 Taking this one step further, you can make a subclass that inherits
50 from Pere::Ubu:
51
52 package Raygun;
53 use base qw(Pere::Ubu);
54
55 # Raygun's suitcase is Red.
56 $suitcase = Raygun->Suitcase;
57
58 Raygun inherits its Suitcase class data from Pere::Ubu.
59
60 Inheritance of class data works analogous to method inheritance. As
61 long as Raygun does not "override" its inherited class data (by using
62 Suitcase() to set a new value) it will continue to use whatever is set
63 in Pere::Ubu and inherit further changes:
64
65 # Both Raygun's and Pere::Ubu's suitcases are now Blue
66 Pere::Ubu->Suitcase('Blue');
67
68 However, should Raygun decide to set its own Suitcase() it has now
69 "overridden" Pere::Ubu and is on its own, just like if it had overrid‐
70 den a method:
71
72 # Raygun has an orange suitcase, Pere::Ubu's is still Blue.
73 Raygun->Suitcase('Orange');
74
75 Now that Raygun has overridden Pere::Ubu, further changes by Pere::Ubu
76 no longer effect Raygun.
77
78 # Raygun still has an orange suitcase, but Pere::Ubu is using Samsonite.
79 Pere::Ubu->Suitcase('Samsonite');
80
81 You can also override this class data on a per-object basis. If $obj
82 isa Pere::Ubu then
83
84 $obj->Suitcase; # will return Samsonite
85
86 $obj->Suitcase('Purple'); # will set Suitcase *for this object only*
87
88 And after you've done that,
89
90 $obj->Suitcase; # will return Purple
91
92 but
93
94 Pere::Ubu->Suitcase; # will still return Samsonite
95
96 If you don't want this behaviour use Class::Data::Inheritable instead.
97
98 "mk_classaccessor" will die if used as an object method instead of as a
99 class method.
100
102 mk_classaccessor
103
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 $data_acces‐
109 sor_name, and optionally initially setting it to the given value.
110
111 To facilitate overriding, mk_classaccessor creates an alias to the
112 accessor, _field_accessor(). So Suitcase() would have an alias _Suit‐
113 case_accessor() that does the exact same thing as Suitcase(). This is
114 useful if you want to alter the behavior of a single accessor yet still
115 get the benefits of inheritable class data. For example.
116
117 sub Suitcase {
118 my($self) = shift;
119 warn "Fashion tragedy" if @_ and $_[0] eq 'Plaid';
120
121 $self->_Suitcase_accessor(@_);
122 }
123
124 Overriding accessors does not work in the same class as you declare the
125 accessor in. It only works in subclasses due to the fact that subrou‐
126 tines are loaded at compile time and accessors are loaded at runtime,
127 thus overriding any subroutines with the same name in the same class.
128
129 mk_classaccessors(@accessornames)
130
131 Takes a list of names and generates an accessor for each name in the
132 list using "mk_classaccessor".
133
135 Based on the creative stylings of Damian Conway, Michael G Schwern,
136 Tony Bowden (Class::Data::Inheritable) and Michael G Schwern, Marty
137 Pauley (Class::Accessor).
138
139 Coded by Matt S Trout Tweaks by Christopher H. Laco.
140
142 If your object isn't hash-based, this will currently break. My modifi‐
143 cations aren't exactly sophisticated so far.
144
145 mstrout@cpan.org or bug me on irc.perl.org, nick mst claco@cpan.org or
146 irc.perl.org, nick claco
147
149 This module is free software. It may be used, redistributed and/or mod‐
150 ified under the terms of the Perl Artistic License (see
151 http://www.perl.com/perl/misc/Artistic.html)
152
154 perltootc has a very elaborate discussion of class data in Perl.
155 Class::Accessor, Class::Data::Inheritable
156
157
158
159perl v5.8.8 2007-05-17 Class::Data::Accessor(3)