In what I'm sure he meant to be a throwaway rhetorical question, Jonathan asks:
> Why bother when Class::Accessor::* already does the same thing?
OH IT'S SO ON! :)
So after writing some quick benchmarks (in the examples directory of the new Object::Tiny 1.03) and doing some investigations and tweaking (stole a small trick from Class::Accessor::Fast to make my accessors slightly faster), here is the answer to Jonathan's question, from the Object::Tiny docs.
------------------------------------------------------------------------------
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?
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.
Object::Tiny is 93% smaller than Class::Accessor::Fast
Class::Accessor::Fast requires about 125k of memory to load.
Object::Tiny requires about 8k of memory to load.
Object::Tiny is 75% more terse to use than Class::Accessor::Fast
Using Object::Tiny requires the least possible number of keystrokes
(short of making the actual name Object::Tiny smaller).
And it requires no ugly constructor methods.
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.
Using Class::Accessor::Fast...
package Foo::Bar; use base 'Class::Accessor::Fast'; Foo::Bar->mk_ro_accessors(qw{ foo bar baz });Using Object::Tiny...
package Foo::Bar; use Object::Tiny qw{ foo bar baz };You might note I've been a little generous there by reusing the class for the method call (which is how the SYNOPSIS lists it). The alternative commonly used by a lot of people, to avoid Repeating Yourself (which is one of those things you Don't do) is to use the longer (in this case) and even uglier __PACKAGE__ compiler flag.
my $object = Foo::Bar->new( { foo => 1, bar => 2, baz => 3, } );Using Object::Tiny...
my $object = Foo::Bar->new( foo => 1, bar => 2, baz => 3, );Object::Tiny constructors are 110% faster than Class::Accessor::Fast
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% --Object::Tiny pollutes your API 95% less than Class::Accessor::Fast
DB<1> use Class::Inspector DB<2> x Class::Inspector->methods('Foo_Bar_Tiny'); 0 ARRAY(0xfda780) 0 'bar' 1 'baz' 2 'foo' 3 'import' 4 'new' DB<3> x Class::Inspector->methods('Foo_Bar_Accessor'); 0 ARRAY(0xfdb3c8) 0 '_bar_accessor' 1 '_baz_accessor' 2 '_carp' 3 '_croak' 4 '_foo_accessor' 5 '_mk_accessors' 6 'accessor_name_for' 7 'bar' 8 'baz' 9 'best_practice_accessor_name_for' 10 'best_practice_mutator_name_for' 11 'follow_best_practice' 12 'foo' 13 'get' 14 'make_accessor' 15 'make_ro_accessor' 16 'make_wo_accessor' 17 'mk_accessors' 18 'mk_ro_accessors' 19 'mk_wo_accessors' 20 'mutator_name_for' 21 'new' 22 'set'Object::Tiny adds 2 extra methods to your class.