1Class::Data::Accessor(3U)ser Contributed Perl DocumentatiColnass::Data::Accessor(3)
2
3
4

NAME

6       Class::Data::Accessor - Inheritable, overridable class and instance
7       data accessor creation
8

SYNOPSIS

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
23         Stuff->DataFile; # returns /etc/stuff/data
24
25         my $stuff = Stuff->new; # your new, not ours
26
27         $stuff->DataFile; # returns /etc/stuff/data
28
29         $stuff->DataFile('/etc/morestuff'); # sets it on the object
30
31         Stuff->DataFile; # still returns /etc/stuff/data
32

DESCRIPTION

34       This module is now deprecated!
35
36       Please consider using Class::Accessor::Grouped or Moose
37
38       Class::Data::Accessor is the marriage of Class::Accessor and
39       Class::Data::Inheritable into a single module. It is used for creating
40       accessors to class data that overridable in subclasses as well as in
41       class instances.
42
43       For example:
44
45         Pere::Ubu->mk_classaccessor('Suitcase');
46
47       will generate the method Suitcase() in the class Pere::Ubu.
48
49       This new method can be used to get and set a piece of class data.
50
51         Pere::Ubu->Suitcase('Red');
52         $suitcase = Pere::Ubu->Suitcase;
53
54       Taking this one step further, you can make a subclass that inherits
55       from Pere::Ubu:
56
57         package Raygun;
58         use base qw(Pere::Ubu);
59
60         # Raygun's suitcase is Red.
61         $suitcase = Raygun->Suitcase;
62
63       Raygun inherits its Suitcase class data from Pere::Ubu.
64
65       Inheritance of class data works analogous to method inheritance.  As
66       long as Raygun does not "override" its inherited class data (by using
67       Suitcase() to set a new value) it will continue to use whatever is set
68       in Pere::Ubu and inherit further changes:
69
70         # Both Raygun's and Pere::Ubu's suitcases are now Blue
71         Pere::Ubu->Suitcase('Blue');
72
73       However, should Raygun decide to set its own Suitcase() it has now
74       "overridden" Pere::Ubu and is on its own, just like if it had
75       overridden a method:
76
77         # Raygun has an orange suitcase, Pere::Ubu's is still Blue.
78         Raygun->Suitcase('Orange');
79
80       Now that Raygun has overridden Pere::Ubu, further changes by Pere::Ubu
81       no longer effect Raygun.
82
83         # Raygun still has an orange suitcase, but Pere::Ubu is using Samsonite.
84         Pere::Ubu->Suitcase('Samsonite');
85
86       You can also override this class data on a per-object basis.  If $obj
87       isa Pere::Ubu then
88
89         $obj->Suitcase; # will return Samsonite
90
91         $obj->Suitcase('Purple'); # will set Suitcase *for this object only*
92
93       And after you've done that,
94
95         $obj->Suitcase; # will return Purple
96
97       but
98
99         Pere::Ubu->Suitcase; # will still return Samsonite
100
101       If you don't want this behaviour use Class::Data::Inheritable instead.
102
103       "mk_classaccessor" will die if used as an object method instead of as a
104       class method.
105

METHODS

107   mk_classaccessor
108         Class->mk_classaccessor($data_accessor_name);
109         Class->mk_classaccessor($data_accessor_name => $value);
110
111       This is a class method used to declare new class data accessors.  A new
112       accessor will be created in the Class using the name from
113       $data_accessor_name, and optionally initially setting it to the given
114       value.
115
116       To facilitate overriding, mk_classaccessor creates an alias to the
117       accessor, _field_accessor().  So Suitcase() would have an alias
118       _Suitcase_accessor() that does the exact same thing as Suitcase().
119       This is useful if you want to alter the behavior of a single accessor
120       yet still get the benefits of inheritable class data.  For example.
121
122         sub Suitcase {
123             my($self) = shift;
124             warn "Fashion tragedy" if @_ and $_[0] eq 'Plaid';
125
126             $self->_Suitcase_accessor(@_);
127         }
128
129       Overriding accessors does not work in the same class as you declare the
130       accessor in.  It only works in subclasses due to the fact that
131       subroutines are loaded at compile time and accessors are loaded at
132       runtime, thus overriding any subroutines with the same name in the same
133       class.
134
135   mk_classaccessors(@accessornames)
136       Takes a list of names and generates an accessor for each name in the
137       list using "mk_classaccessor".
138

AUTHORS

140       Based on the creative stylings of Damian Conway, Michael G Schwern,
141       Tony Bowden (Class::Data::Inheritable) and Michael G Schwern, Marty
142       Pauley (Class::Accessor).
143
144       Coded by Matt S Trout Tweaks by Christopher H. Laco.
145

BUGS and QUERIES

147       If your object isn't hash-based, this will currently break. My
148       modifications aren't exactly sophisticated so far.
149
150       mstrout@cpan.org or bug me on irc.perl.org, nick mst claco@cpan.org or
151       irc.perl.org, nick claco
152

LICENSE

154       This module is free software. It may be used, redistributed and/or
155       modified under the same terms as Perl itself. (see
156       http://dev.perl.org/licenses/artistic.html and
157       http://www.opensource.org/licenses/gpl-license.php)
158

SEE ALSO

160       perltootc has a very elaborate discussion of class data in Perl.
161       Class::Accessor, Class::Data::Inheritable
162
163
164
165perl v5.32.1                      2021-01-27          Class::Data::Accessor(3)
Impressum