1Class::MethodMaker::V1CUosmepratC(o3n)tributed Perl DocuCmleanstsa:t:iMoenthodMaker::V1Compat(3)
2
3
4
6 Class::MethodMaker::V1Compat - V1 compatibility code for C::MM
7
9 This class is for internal implementation only. It is not a public
10 API.
11
13 Class::MethodMaker version 2 strives for backward-compatibility with
14 version 1 as far as possible. That is to say, classes built with
15 version 1 should work with few if any changes. However, the calling
16 conventions for building new classes are significantly different: this
17 is necessary to achieve a greater consistency of arguments.
18
19 Version 2 takes all arguments within a single arrayref:
20
21 use Class::MethodMaker
22 [ scalar => 'a' ];
23
24 If arguments are presented as a list, then Class::MethodMaker assumes
25 that this is a version 1 call, and acts accordingly. Version 1
26 arguments are passed and internally rephrased to version 2 arguments,
27 and passed off to the version 2 engine. Thus, the majority of version
28 1 calls can be upgraded to version 2 merely by rephrasing. However,
29 there are a number of behaviours that in version 1 that are internally
30 inconsistent. These behaviours are mimicked in version 1 mode as far
31 as possible, but are not reproducible in version 2 (to allow version 2
32 clients to rely on a more internally consistent interface).
33
34 Version 2 Implementations
35 The nearest equivalent to each 1 component (slot) available in version
36 2 is shown below using the indicated data-structures & options to
37 create a component called "a" that mimics the V1 component behaviour as
38 closely as possible:
39
40 abstract
41 use Class::MethodMaker
42 [ abstract => 'a' ];
43
44 boolean
45 Boolean is available as a backwards compatibility hack, but there
46 is currently no V2 equivalent. It is likely that some replacement
47 mechanism will be introduced in the future, but that it will be
48 incompatible with the version 1 boolean.
49
50 code
51 use Class::MethodMaker
52 [ scalar => 'a' ];
53
54 Let's face it, the v1 store-if-it's-a-coderef-else-retrieve
55 semantics are rather broken. How do you pass a coderef as argument
56 to one of these? It is on the TODO list to recognize code as
57 fundamental restricted type (analogous to INTEGER), which would add
58 in a *_invoke method.
59
60 copy
61 use Class::MethodMaker
62 [ copy => 'a' ];
63
64 The v2 method is the same as v1.
65
66 counter
67 use Class::MethodMaker
68 [ scalar => [{-type => Class::MethodMaker::Constants::INTEGER}, 'a'] ];
69
70 copy
71 deep_copy
72 use Class::MethodMaker
73 [ copy => [ -deep => 'a' ] ];
74
75 get_concat
76 use Class::MethodMaker
77 [ scalar => [{ -store_cb => sub {
78 defined $_[1] ? ( defined $_[3] ?
79 "$_[3] $_[1]" : $_[1] )
80 : undef;
81 }
82 },
83 'a' ]
84 ];
85
86 get_set
87 use Class::MethodMaker
88 [ scalar => 'a' ];
89
90 hash
91 use Class::MethodMaker
92 [ hash => 'a' ];
93
94 key_attrib
95 Although v1 calls will continue to work, this is not supported in
96 v2.
97
98 key_with_create
99 Although v1 calls will continue to work, this is not supported in
100 v2.
101
102 list
103 use Class::MethodMaker
104 [ list => 'a' ];
105
106 Note that the "*" method now sets the whole array if given
107 arguments.
108
109 method
110 See "code".
111
112 new
113 use Class::MethodMaker
114 [ new => 'a' ];
115
116 new_hash_init
117 use Class::MethodMaker
118 [ new => [ -hash => 'a' ] ];
119
120 new_hash_with_init
121 use Class::MethodMaker
122 [ new => [ -hash => -init => 'a' ] ];
123
124 new_with_args
125 Although v1 calls will continue to work, this is not supported in
126 v2, for it is a trivial application of "new_with_init".
127
128 new_with_init
129 use Class::MethodMaker
130 [ new => [ -init => 'a' ] ];
131
132 object
133 use Class::MethodMaker
134 [ scalar => [{ -type => 'MyClass',
135 -forward => [qw/ method1 method2 /] }, 'a' ]
136 ];
137
138 object_tie_hash
139 use Class::MethodMaker
140 [ hash => [{ -type => 'MyClass',
141 -forward => [qw/ method1 method2 /],
142 -tie_class => 'Tie::MyTie',
143 -tie_args => [qw/ foo bar baz /],
144 }, 'a' ]
145 ];
146
147 object_tie_list
148 use Class::MethodMaker
149 [ array => [{ -type => 'MyClass',
150 -forward => [qw/ method1 method2 /],
151 -tie_class => 'Tie::MyTie',
152 -tie_args => [qw/ foo bar baz /],
153 }, 'a' ]
154 ];
155
156 set_once
157 use Class::MethodMaker
158 [ scalar => [{ -store_cb => sub {
159 die "Already stored $_[3]"
160 if @_ > 3;
161 }
162 },
163 'a' ]
164 ];
165
166 set_once_static
167 use Class::MethodMaker
168 [ scalar => [{ -store_cb => sub {
169 die "Already stored $_[3]"
170 if @_ > 3;
171 },
172 -static => 1,
173 },
174 'a' ]
175 ];
176
177 singleton
178 use Class::MethodMaker
179 [ new => [ -singleton => -hash => -init => 'a' ] ];
180
181 static_get_set
182 use Class::MethodMaker
183 [ scalar => [ -static => 'a' ], ];
184
185 static_hash
186 use Class::MethodMaker
187 [ hash => [ -static => 'a' ], ];
188
189 static_list
190 use Class::MethodMaker
191 [ list => [ -static => 'a' ], ];
192
193 tie_hash
194 use Class::MethodMaker
195 [ hash => [ { -tie_class => 'MyTie',
196 -tie_args => [qw/ foo bar baz /],
197 } => 'a' ], ];
198
199 tie_list
200 use Class::MethodMaker
201 [ array => [ { -tie_class => 'MyTie',
202 -tie_args => [qw/ foo bar baz /],
203 } => 'a' ], ];
204
205 tie_scalar
206 use Class::MethodMaker
207 [ scalar => [ { -tie_class => 'MyTie',
208 -tie_args => [qw/ foo bar baz /],
209 } => 'a' ], ];
210
211 Caveats & Expected Breakages
212 The following version 1 component (slot) types are not currently
213 supported in version 2:
214
215 grouped_fields
216 hash_of_lists
217 listed_attrib
218 struct
219
223 Email the development mailing list
224 "class-mmaker-devel@lists.sourceforge.net".
225
227 Martyn J. Pearce
228
230 Copyright (c) 2003, 2004 Martyn J. Pearce. This program is free
231 software; you can redistribute it and/or modify it under the same terms
232 as Perl itself.
233
235perl v5.30.1 2020-01-29 Class::MethodMaker::V1Compat(3)