.\" Automatically generated by Pod::Man 2.22 (Pod::Simple 3.07) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is turned on, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .ie \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . nr % 0 . rr F .\} .el \{\ . de IX .. .\} .\" .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). .\" Fear. Run. Save yourself. No user-serviceable parts. . \" fudge factors for nroff and troff .if n \{\ . ds #H 0 . ds #V .8m . ds #F .3m . ds #[ \f1 . ds #] \fP .\} .if t \{\ . ds #H ((1u-(\\\\n(.fu%2u))*.13m) . ds #V .6m . ds #F 0 . ds #[ \& . ds #] \& .\} . \" simple accents for nroff and troff .if n \{\ . ds ' \& . ds ` \& . ds ^ \& . ds , \& . ds ~ ~ . ds / .\} .if t \{\ . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' .\} . \" troff and (daisy-wheel) nroff accents .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' .ds 8 \h'\*(#H'\(*b\h'-\*(#H' .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] .ds ae a\h'-(\w'a'u*4/10)'e .ds Ae A\h'-(\w'A'u*4/10)'E . \" corrections for vroff .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' . \" for low resolution devices (crt and lpr) .if \n(.H>23 .if \n(.V>19 \ \{\ . ds : e . ds 8 ss . ds o a . ds d- d\h'-1'\(ga . ds D- D\h'-1'\(hy . ds th \o'bp' . ds Th \o'LP' . ds ae ae . ds Ae AE .\} .rm #[ #] #H #V #F C .\" ======================================================================== .\" .IX Title "Object::Tiny 3pm" .TH Object::Tiny 3pm "2008-08-16" "perl v5.10.1" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Object::Tiny \- Class building as simple as it gets .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 2 \& # Define a class \& package Foo; \& \& use Object::Tiny qw{ bar baz }; \& \& 1; \& \& \& # Use the class \& my $object = Foo\->new( bar => 1 ); \& \& print "bar is " . $object\->bar . "\en"; .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" There's a whole bunch of class builders out there. In fact, creating a class builder seems to be something of a rite of passage (this is my fifth, at least). .PP Unfortunately, most of the time I want a class builder I'm in a hurry and sketching out lots of fairly simple data classes with fairly simple structure, mostly just read-only accessors, and that's about it. .PP Often this is for code that won't end up on \s-1CPAN\s0, so adding a small dependency doesn't matter much. I just want to be able to define these classes \s-1FAST\s0. .PP By which I mean \s-1LESS\s0 typing than writing them by hand, not more. And I don't need all those weird complex features that bloat out the code and take over the whole way I build modules. .PP And so, I present yet another member of the Tiny family of modules, Object::Tiny. .PP The goal here is really just to save me some typing. There's others that could do the job just fine, but I want something that does as little as possible and creates code the same way I'd have written it by hand anyway. .PP To use Object::Tiny, just call it with a list of accessors to be created. .PP .Vb 1 \& use Object::Tiny \*(Aqfoo\*(Aq, \*(Aqbar\*(Aq; .Ve .PP For a large list, I lay it out like this... .PP .Vb 11 \& use Object::Tiny qw{ \& item_font_face \& item_font_color \& item_font_size \& item_text_content \& item_display_time \& seperator_font_face \& seperator_font_color \& seperator_font_size \& seperator_text_content \& }; .Ve .PP This will create a bunch of simple accessors, and set the inheritance to be the child of Object::Tiny. .PP Object::Tiny is empty other than a basic \f(CW\*(C`new\*(C'\fR constructor which does the following .PP .Vb 4 \& sub new { \& my $class = shift; \& return bless { @_ }, $class; \& } .Ve .PP In fact, if doing the following in your class gets annoying... .PP .Vb 3 \& sub new { \& my $class = shift; \& my $self = $class\->SUPER::new( @_ ); \& \& # Extra checking and such \& ... \& \& return $self; \& } .Ve .PP \&... then feel free to ditch the \s-1SUPER\s0 call and just create the hash yourself! It's not going to make a lick of different and there's nothing magic going on under the covers you might break. .PP And that's really all there is to it. Let a million simple data classes bloom. Features? We don't need no stinking features. .SS "Handling Subclasses" .IX Subsection "Handling Subclasses" If the class you are using Object::Tiny for is already a subclass of another Object::Tiny class (or a subclass of anything else) it doesn't really work to make the class use multiple inheritance. .PP So in this case, Object::Tiny will create the accessors you specify, but \&\s-1WON\s0'T make it a subclass of Object::Tiny. .SS "Why bother when Class::Accessor::* already does the same thing?" .IX Subsection "Why bother when Class::Accessor::* already does the same thing?" As a class builder, Object::Tiny inevitably is compared to Class::Accessor and related modules. They seem so similar, so why would I reimplement it? .PP The answer is that for experienced developers that don't need or want hand-holding, Object::Tiny is just outright better, faster or cheaper on every single metric than Class::Accessor::Fast, which is the most comparable member of the Class::Accessor::* family. .PP \&\fBObject::Tiny is 93% smaller than Class::Accessor::Fast\fR .PP Class::Accessor::Fast requires about 125k of memory to load. .PP Object::Tiny requires about 8k of memory to load. .PP \&\fBObject::Tiny is 75% more terse to use than Class::Accessor::Fast\fR .PP Object::Tiny is used with the least possible number of keystrokes (short of making the actual name Object::Tiny smaller). .PP And it requires no ugly constructor methods. .PP I mean really, what sort of a method name is 'mk_ro_accessors'. That sort of thing went out of style in the early nineties. .PP Using Class::Accessor::Fast... .PP .Vb 3 \& package Foo::Bar; \& use base \*(AqClass::Accessor::Fast\*(Aq; \& Foo::Bar\->mk_ro_accessors(qw{ foo bar baz }); .Ve .PP Using Object::Tiny... .PP .Vb 2 \& package Foo::Bar; \& use Object::Tiny qw{ foo bar baz }; .Ve .PP Further, Object::Tiny lets you pass your params in directly, without having to wrap them in an additional \s-1HASH\s0 reference that will just be copied \s-1ANYWAY\s0 inside the constructor. .PP Using Class::Accessor::Fast... .PP .Vb 5 \& my $object = Foo::Bar\->new( { \& foo => 1, \& bar => 2, \& baz => 3, \& } ); .Ve .PP Using Object::Tiny... .PP .Vb 5 \& my $object = Foo::Bar\->new( \& foo => 1, \& bar => 2, \& baz => 3, \& ); .Ve .PP \&\fBObject::Tiny constructors are 110% faster than Class::Accessor::Fast\fR .PP Object::Tiny accessors are identical in speed to Class::Accessor::Fast accessors, but Object::Tiny constructors are \s-1TWICE\s0 as fast as Class::Accessor::Fast constructors, \s-1DESPITE\s0 C:A:Fast forcing you to pass by reference (which is typically done for speed reasons). .PP .Vb 4 \& Benchmarking constructor plus accessors... \& Rate accessor tiny \& accessor 100949/s \-\- \-45% \& tiny 182382/s 81% \-\- \& \& Benchmarking constructor alone... \& Rate accessor tiny \& accessor 156470/s \-\- \-54% \& tiny 342231/s 119% \-\- \& \& Benchmarking accessors alone... \& Rate tiny accessor \& tiny 81.0/s \-\- \-0% \& accessor 81.0/s 0% \-\- .Ve .PP \&\fBObject::Tiny pollutes your \s-1API\s0 95% less than Class::Accessor::Fast\fR .PP Object::Tiny adds two methods to your class, \f(CW\*(C`new\*(C'\fR and \f(CW\*(C`import\*(C'\fR. The \&\f(CW\*(C`new\*(C'\fR constructor is so trivial you can just ignore it and use your own if you wish, and the \f(CW\*(C`import\*(C'\fR will shortcut and do nothing (it is used to implement the \f(CW"use Object::Tiny qw{ foo bar baz };"\fR syntax itself). .PP So if you make your own import, you can ignore the Object::Tiny one. .PP Class::Accessor::Fast isn't quite as light, adding all sorts of useless extra public methods (why on earth would you want to add method accessors at run-time?). .PP Here's what the classes used in the benchmark end up like. .PP .Vb 1 \& DB<1> use Class::Inspector \& \& DB<2> x Class::Inspector\->methods(\*(AqFoo_Bar_Tiny\*(Aq); \& 0 ARRAY(0xfda780) \& 0 \*(Aqbar\*(Aq \& 1 \*(Aqbaz\*(Aq \& 2 \*(Aqfoo\*(Aq \& 3 \*(Aqimport\*(Aq \& 4 \*(Aqnew\*(Aq \& \& DB<3> x Class::Inspector\->methods(\*(AqFoo_Bar_Accessor\*(Aq); \& 0 ARRAY(0xfdb3c8) \& 0 \*(Aq_bar_accessor\*(Aq \& 1 \*(Aq_baz_accessor\*(Aq \& 2 \*(Aq_carp\*(Aq \& 3 \*(Aq_croak\*(Aq \& 4 \*(Aq_foo_accessor\*(Aq \& 5 \*(Aq_mk_accessors\*(Aq \& 6 \*(Aqaccessor_name_for\*(Aq \& 7 \*(Aqbar\*(Aq \& 8 \*(Aqbaz\*(Aq \& 9 \*(Aqbest_practice_accessor_name_for\*(Aq \& 10 \*(Aqbest_practice_mutator_name_for\*(Aq \& 11 \*(Aqfollow_best_practice\*(Aq \& 12 \*(Aqfoo\*(Aq \& 13 \*(Aqget\*(Aq \& 14 \*(Aqmake_accessor\*(Aq \& 15 \*(Aqmake_ro_accessor\*(Aq \& 16 \*(Aqmake_wo_accessor\*(Aq \& 17 \*(Aqmk_accessors\*(Aq \& 18 \*(Aqmk_ro_accessors\*(Aq \& 19 \*(Aqmk_wo_accessors\*(Aq \& 20 \*(Aqmutator_name_for\*(Aq \& 21 \*(Aqnew\*(Aq \& 22 \*(Aqset\*(Aq .Ve .PP As you can see, Object::Tiny adds 2 methods to your class, Class::Accessor adds 16 methods, plus one extra one for every accessor. .PP \&\fBObject::Tiny doesn't have any of the caveats of Class::Accessor::Fast\fR .PP When you call \fBuse Object::Tiny qw{ foo bar baz }\fR it isn't treated as some sort of specification for the class, it's just a list of accessors you want made for you. .PP So if you want to customize \f(CW\*(C`foo\*(C'\fR you don't need to get into contortions with \&\*(L"pure\*(R" base classes or calling alternate internal methods. Just make your own \&\f(CW\*(C`foo\*(C'\fR method and remove \f(CW\*(C`foo\*(C'\fR from the list passed to the \f(CW\*(C`use\*(C'\fR call. .PP \&\fBObject::Tiny is more back-compatible than Class::Accessor::Fast\fR .PP Class::Accessor::Fast has a minimum Perl dependency of 5.005002. .PP Object::Tiny has a minimum Perl dependency of 5.004. .PP \&\fBObject::Tiny has no module dependencies whatsoever\fR .PP Object::Tiny does not load \s-1ANYTHING\s0 at all outside of its own single .pm file. .PP So Object::Tiny will never get confused in odd situations due to old or weird versions of other modules (Class::Accessor::Fast has a dependency on base.pm, which has some caveats of its own). .SH "SUPPORT" .IX Header "SUPPORT" Bugs should be reported via the \s-1CPAN\s0 bug tracker at .PP .PP For other issues, contact the author. .SH "AUTHOR" .IX Header "AUTHOR" Adam Kennedy .SH "SEE ALSO" .IX Header "SEE ALSO" Config::Tiny .SH "COPYRIGHT" .IX Header "COPYRIGHT" Copyright 2007 \- 2008 Adam Kennedy. .PP This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. .PP The full text of the license can be found in the \&\s-1LICENSE\s0 file included with this module.