Object::Tiny(3pm)

πŸ“¦
version 1.09 Β· 4 versions
, in debian bookworm, debian trixie, ubuntu jammy, ubuntu noble

πŸ’– Object::Tiny πŸ’–

okay bestie, so this is Object::Tiny β€” a User Contributed Perl Documentation page, and it's literally the tiniest class builder that ever did it. Class building as simple as it gets, fr fr. πŸŽ€


✨ NAME ✨

Object::Tiny β€” class building as simple as it gets. that's it, that's the whole vibe. πŸ’…


🌸 SYNOPSIS 🌸

define a class:

# Define a class
package Foo;

use Object::Tiny qw{ bar baz };

1;

then actually use it:

# Use the class
my $object = Foo->new( bar => 1 );

print "bar is " . $object->bar . "\n";

πŸ¦‹ DESCRIPTION πŸ¦‹

so here's the tea: there's a WHOLE bunch of class builders out there. in fact, writing a class builder is kinda a rite of passage β€” this one is like, the fifth, at least. 😭

but the thing is, most of the time when you want a class builder you're in a hurry, sketching out a bunch of fairly simple data classes with fairly simple structure, mostly just read-only accessors, and that's about it. that's the whole assignment. 🫢

a lot of the time this is for code that won't even end up on CPAN, so adding a small dependency doesn't matter much. you just want to define these classes FAST. ⚑

and by FAST we mean LESS typing than writing them by hand, not more. and you do NOT need all those weird complex features that bloat out the code and take over the whole way you build modules. no thank you. πŸ™ˆ

and so, here's yet another member of the Tiny family of modules: Object::Tiny. 🌈

the goal here is really just to save some typing. there's others that could do the job just fine, but the vibe is: do as little as possible and create code the same way you'd have written it by hand anyway.

to use Object::Tiny, just call it with a list of accessors to be created.

use Object::Tiny 'foo', 'bar';

for a large list, lay it out like this...

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
};

this will create a bunch of simple accessors, and set the inheritance to be the child of Object::Tiny.

Object::Tiny is empty other than a basic new constructor which does the following:

sub new {
    my $class = shift;
    return bless { @_ }, $class;
}

in fact, if doing the following in your class gets annoying...

sub new {
    my $class = shift;
    my $self  = $class->SUPER::new( @_ );

    # Extra checking and such
    ...

    return $self;
}

... then feel free to ditch the SUPER call and just create the hash yourself! it's not going to make a lick of difference and there's nothing magic going on under the covers you might break. ✨

and that's really all there is to it. let a million simple data classes bloom. 🌸 features? we don't need no stinking features. periodt.


πŸ‘‘ Handling Subclasses πŸ‘‘

if the class you're 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.

so in this case, Object::Tiny will create the accessors you specify, but WON'T make it a subclass of Object::Tiny.


πŸ’€ Why bother when Class::Accessor::* already does the same thing? πŸ’€

as a class builder, Object::Tiny inevitably gets compared to Class::Accessor and related modules. they seem so similar, so why 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

Object::Tiny is used with the least possible number of keystrokes (short of making the actual name Object::Tiny smaller).

and it requires no ugly constructor methods.

like, 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 };

further, Object::Tiny lets you pass your params in directly, without having to wrap them in an additional HASH reference that will just be copied ANYWAY inside the constructor. πŸ™ˆ

using Class::Accessor::Fast...

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

Object::Tiny accessors are identical in speed to Class::Accessor::Fast accessors, but Object::Tiny constructors are TWICE as fast as Class::Accessor::Fast constructors, DESPITE C:A:Fast forcing you to pass by reference (which is typically done for speed reasons).

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

Object::Tiny adds two methods to your class, new and import. the new constructor is so trivial you can just ignore it and use your own if you wish, and the import will shortcut and do nothing (it is used to implement the use Object::Tiny qw{ foo bar baz }; syntax itself).

so if you make your own import, you can ignore the Object::Tiny one. 🫢

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?).

here's what the classes used in the benchmark end up like.

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'

as you can see, Object::Tiny adds 2 methods to your class, Class::Accessor adds 16 methods, plus one extra one for every accessor. 😭 the difference is literally giving "minimalist queen" energy. πŸ‘‘

Object::Tiny doesn't have any of the caveats of Class::Accessor::Fast

when you call use Object::Tiny qw{ foo bar baz } it isn't treated as some sort of specification for the class, it's just a list of accessors you want made for you.

so if you want to customize foo you don't need to get into contortions with pure base classes or calling alternate internal methods. just make your own foo method and remove foo from the list passed to the use call. easy. πŸŽ€

Object::Tiny is more back-compatible than Class::Accessor::Fast

Class::Accessor::Fast has a minimum Perl dependency of 5.005002.

Object::Tiny has a minimum Perl dependency of 5.004.

Object::Tiny has no module dependencies whatsoever

Object::Tiny does not load ANYTHING at all outside of its own single .pm file.

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). πŸ¦‹


🫢 SUPPORT 🫢

bugs should be reported via the CPAN bug tracker at

<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Object-Tiny>

for other issues, contact the author.


⭐ AUTHOR ⭐

Adam Kennedy <adamk@cpan.org>


🌈 SEE ALSO 🌈

  • Config::Tiny

Copyright 2007 - 2011 Adam Kennedy.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

The full text of the license can be found in the LICENSE file included with this module.


okay bestie that's the whole page, we ate and left no crumbs. πŸ’…βœ¨ this doc is from perl v5.28.0, dated 2018-11-21, and it's Object::Tiny(3pm) β€” stay tiny, stay iconic. πŸŽ€πŸ¦‹πŸŒΈ

πŸ“¦ where this page comes from