1Class::Data::InheritablUes(e3r)Contributed Perl DocumentCaltaisosn::Data::Inheritable(3)
2
3
4
6 Class::Data::Inheritable - Inheritable, overridable class data
7
9 package Stuff;
10 use base qw(Class::Data::Inheritable);
11
12 # Set up DataFile as inheritable class data.
13 Stuff->mk_classdata('DataFile');
14
15 # Declare the location of the data file for this class.
16 Stuff->DataFile('/etc/stuff/data');
17
18 # Or, all in one shot:
19 Stuff->mk_classdata(DataFile => '/etc/stuff/data');
20
22 Class::Data::Inheritable is for creating accessor/mutators to class
23 data. That is, if you want to store something about your class as a
24 whole (instead of about a single object). This data is then inherited
25 by your subclasses and can be overriden.
26
27 For example:
28
29 Pere::Ubu->mk_classdata('Suitcase');
30
31 will generate the method Suitcase() in the class Pere::Ubu.
32
33 This new method can be used to get and set a piece of class data.
34
35 Pere::Ubu->Suitcase('Red');
36 $suitcase = Pere::Ubu->Suitcase;
37
38 The interesting part happens when a class inherits from Pere::Ubu:
39
40 package Raygun;
41 use base qw(Pere::Ubu);
42
43 # Raygun's suitcase is Red.
44 $suitcase = Raygun->Suitcase;
45
46 Raygun inherits its Suitcase class data from Pere::Ubu.
47
48 Inheritance of class data works analogous to method inheritance. As
49 long as Raygun does not "override" its inherited class data (by using
50 Suitcase() to set a new value) it will continue to use whatever is set
51 in Pere::Ubu and inherit further changes:
52
53 # Both Raygun's and Pere::Ubu's suitcases are now Blue
54 Pere::Ubu->Suitcase('Blue');
55
56 However, should Raygun decide to set its own Suitcase() it has now
57 "overridden" Pere::Ubu and is on its own, just like if it had overriden
58 a method:
59
60 # Raygun has an orange suitcase, Pere::Ubu's is still Blue.
61 Raygun->Suitcase('Orange');
62
63 Now that Raygun has overridden Pere::Ubu futher changes by Pere::Ubu no
64 longer effect Raygun.
65
66 # Raygun still has an orange suitcase, but Pere::Ubu is using Samsonite.
67 Pere::Ubu->Suitcase('Samsonite');
68
70 mk_classdata
71 Class->mk_classdata($data_accessor_name);
72 Class->mk_classdata($data_accessor_name => $value);
73
74 This is a class method used to declare new class data accessors. A new
75 accessor will be created in the Class using the name from
76 $data_accessor_name, and optionally initially setting it to the given
77 value.
78
79 To facilitate overriding, mk_classdata creates an alias to the
80 accessor, _field_accessor(). So Suitcase() would have an alias
81 _Suitcase_accessor() that does the exact same thing as Suitcase().
82 This is useful if you want to alter the behavior of a single accessor
83 yet still get the benefits of inheritable class data. For example.
84
85 sub Suitcase {
86 my($self) = shift;
87 warn "Fashion tragedy" if @_ and $_[0] eq 'Plaid';
88
89 $self->_Suitcase_accessor(@_);
90 }
91
93 Original code by Damian Conway.
94
95 Maintained by Michael G Schwern until September 2005.
96
97 Now maintained by Tony Bowden.
98
100 Please direct all correspondence regarding this module to:
101 bug-Class-Data-Inheritable@rt.cpan.org
102
104 Copyright (c) 2000-2005, Damian Conway and Michael G Schwern. All
105 Rights Reserved.
106
107 This module is free software. It may be used, redistributed and/or
108 modified under the same terms as Perl itself.
109
111 perltooc has a very elaborate discussion of class data in Perl.
112
113
114
115perl v5.30.1 2020-01-29 Class::Data::Inheritable(3)