1Object::Tiny(3) User Contributed Perl Documentation Object::Tiny(3)
2
3
4
6 Object::Tiny - Class building as simple as it gets
7
9 # Define a class
10 package Foo;
11
12 use Object::Tiny qw{ bar baz };
13
14 1;
15
16
17 # Use the class
18 my $object = Foo->new( bar => 1 );
19
20 print "bar is " . $object->bar . "\n";
21
23 There's a whole bunch of class builders out there. In fact, creating a
24 class builder seems to be something of a rite of passage (this is my
25 fifth, at least).
26
27 Unfortunately, most of the time I want a class builder I'm in a hurry
28 and sketching out lots of fairly simple data classes with fairly simple
29 structure, mostly just read-only accessors, and that's about it.
30
31 Often this is for code that won't end up on CPAN, so adding a small
32 dependency doesn't matter much. I just want to be able to define these
33 classes FAST.
34
35 By which I mean LESS typing than writing them by hand, not more. And I
36 don't need all those weird complex features that bloat out the code and
37 take over the whole way I build modules.
38
39 And so, I present yet another member of the Tiny family of modules,
40 Object::Tiny.
41
42 The goal here is really just to save me some typing. There's others
43 that could do the job just fine, but I want something that does as
44 little as possible and creates code the same way I'd have written it by
45 hand anyway.
46
47 To use Object::Tiny, just call it with a list of accessors to be
48 created.
49
50 use Object::Tiny 'foo', 'bar';
51
52 For a large list, I lay it out like this...
53
54 use Object::Tiny qw{
55 item_font_face
56 item_font_color
57 item_font_size
58 item_text_content
59 item_display_time
60 seperator_font_face
61 seperator_font_color
62 seperator_font_size
63 seperator_text_content
64 };
65
66 This will create a bunch of simple accessors, and set the inheritance
67 to be the child of Object::Tiny.
68
69 Object::Tiny is empty other than a basic "new" constructor which does
70 the following
71
72 sub new {
73 my $class = shift;
74 return bless { @_ }, $class;
75 }
76
77 In fact, if doing the following in your class gets annoying...
78
79 sub new {
80 my $class = shift;
81 my $self = $class->SUPER::new( @_ );
82
83 # Extra checking and such
84 ...
85
86 return $self;
87 }
88
89 ... then feel free to ditch the SUPER call and just create the hash
90 yourself! It's not going to make a lick of different and there's
91 nothing magic going on under the covers you might break.
92
93 And that's really all there is to it. Let a million simple data classes
94 bloom. Features? We don't need no stinking features.
95
96 Handling Subclasses
97 If the class you are using Object::Tiny for is already a subclass of
98 another Object::Tiny class (or a subclass of anything else) it doesn't
99 really work to make the class use multiple inheritance.
100
101 So in this case, Object::Tiny will create the accessors you specify,
102 but WON'T make it a subclass of Object::Tiny.
103
104 Why bother when Class::Accessor::* already does the same thing?
105 As a class builder, Object::Tiny inevitably is compared to
106 Class::Accessor and related modules. They seem so similar, so why would
107 I reimplement it?
108
109 The answer is that for experienced developers that don't need or want
110 hand-holding, Object::Tiny is just outright better, faster or cheaper
111 on every single metric than Class::Accessor::Fast, which is the most
112 comparable member of the Class::Accessor::* family.
113
114 Object::Tiny is 93% smaller than Class::Accessor::Fast
115
116 Class::Accessor::Fast requires about 125k of memory to load.
117
118 Object::Tiny requires about 8k of memory to load.
119
120 Object::Tiny is 75% more terse to use than Class::Accessor::Fast
121
122 Object::Tiny is used with the least possible number of keystrokes
123 (short of making the actual name Object::Tiny smaller).
124
125 And it requires no ugly constructor methods.
126
127 I mean really, what sort of a method name is 'mk_ro_accessors'. That
128 sort of thing went out of style in the early nineties.
129
130 Using Class::Accessor::Fast...
131
132 package Foo::Bar;
133 use base 'Class::Accessor::Fast';
134 Foo::Bar->mk_ro_accessors(qw{ foo bar baz });
135
136 Using Object::Tiny...
137
138 package Foo::Bar;
139 use Object::Tiny qw{ foo bar baz };
140
141 Further, Object::Tiny lets you pass your params in directly, without
142 having to wrap them in an additional HASH reference that will just be
143 copied ANYWAY inside the constructor.
144
145 Using Class::Accessor::Fast...
146
147 my $object = Foo::Bar->new( {
148 foo => 1,
149 bar => 2,
150 baz => 3,
151 } );
152
153 Using Object::Tiny...
154
155 my $object = Foo::Bar->new(
156 foo => 1,
157 bar => 2,
158 baz => 3,
159 );
160
161 Object::Tiny constructors are 110% faster than Class::Accessor::Fast
162
163 Object::Tiny accessors are identical in speed to Class::Accessor::Fast
164 accessors, but Object::Tiny constructors are TWICE as fast as
165 Class::Accessor::Fast constructors, DESPITE C:A:Fast forcing you to
166 pass by reference (which is typically done for speed reasons).
167
168 Benchmarking constructor plus accessors...
169 Rate accessor tiny
170 accessor 100949/s -- -45%
171 tiny 182382/s 81% --
172
173 Benchmarking constructor alone...
174 Rate accessor tiny
175 accessor 156470/s -- -54%
176 tiny 342231/s 119% --
177
178 Benchmarking accessors alone...
179 Rate tiny accessor
180 tiny 81.0/s -- -0%
181 accessor 81.0/s 0% --
182
183 Object::Tiny pollutes your API 95% less than Class::Accessor::Fast
184
185 Object::Tiny adds two methods to your class, "new" and "import". The
186 "new" constructor is so trivial you can just ignore it and use your own
187 if you wish, and the "import" will shortcut and do nothing (it is used
188 to implement the "use Object::Tiny qw{ foo bar baz };" syntax itself).
189
190 So if you make your own import, you can ignore the Object::Tiny one.
191
192 Class::Accessor::Fast isn't quite as light, adding all sorts of useless
193 extra public methods (why on earth would you want to add method
194 accessors at run-time?).
195
196 Here's what the classes used in the benchmark end up like.
197
198 DB<1> use Class::Inspector
199
200 DB<2> x Class::Inspector->methods('Foo_Bar_Tiny');
201 0 ARRAY(0xfda780)
202 0 'bar'
203 1 'baz'
204 2 'foo'
205 3 'import'
206 4 'new'
207
208 DB<3> x Class::Inspector->methods('Foo_Bar_Accessor');
209 0 ARRAY(0xfdb3c8)
210 0 '_bar_accessor'
211 1 '_baz_accessor'
212 2 '_carp'
213 3 '_croak'
214 4 '_foo_accessor'
215 5 '_mk_accessors'
216 6 'accessor_name_for'
217 7 'bar'
218 8 'baz'
219 9 'best_practice_accessor_name_for'
220 10 'best_practice_mutator_name_for'
221 11 'follow_best_practice'
222 12 'foo'
223 13 'get'
224 14 'make_accessor'
225 15 'make_ro_accessor'
226 16 'make_wo_accessor'
227 17 'mk_accessors'
228 18 'mk_ro_accessors'
229 19 'mk_wo_accessors'
230 20 'mutator_name_for'
231 21 'new'
232 22 'set'
233
234 As you can see, Object::Tiny adds 2 methods to your class,
235 Class::Accessor adds 16 methods, plus one extra one for every accessor.
236
237 Object::Tiny doesn't have any of the caveats of Class::Accessor::Fast
238
239 When you call use Object::Tiny qw{ foo bar baz } it isn't treated as
240 some sort of specification for the class, it's just a list of accessors
241 you want made for you.
242
243 So if you want to customize "foo" you don't need to get into
244 contortions with "pure" base classes or calling alternate internal
245 methods. Just make your own "foo" method and remove "foo" from the list
246 passed to the "use" call.
247
248 Object::Tiny is more back-compatible than Class::Accessor::Fast
249
250 Class::Accessor::Fast has a minimum Perl dependency of 5.005002.
251
252 Object::Tiny has a minimum Perl dependency of 5.004.
253
254 Object::Tiny has no module dependencies whatsoever
255
256 Object::Tiny does not load ANYTHING at all outside of its own single
257 .pm file.
258
259 So Object::Tiny will never get confused in odd situations due to old or
260 weird versions of other modules (Class::Accessor::Fast has a dependency
261 on base.pm, which has some caveats of its own).
262
264 Bugs should be reported via the CPAN bug tracker at
265
266 <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Object-Tiny>
267
268 For other issues, contact the author.
269
271 Adam Kennedy <adamk@cpan.org>
272
274 Config::Tiny
275
277 Copyright 2007 - 2011 Adam Kennedy.
278
279 This program is free software; you can redistribute it and/or modify it
280 under the same terms as Perl itself.
281
282 The full text of the license can be found in the LICENSE file included
283 with this module.
284
285
286
287perl v5.36.0 2022-07-22 Object::Tiny(3)