Object::Extend(3pm)
π Object::Extend π
okay bestie, so this is the page for Object::Extend from the perl manual (that's the 3pm one, for the Perl module docs, not the commands, iykyk) β¨
πΈ NAME πΈ
Object::Extend β add and override per-object methods π«Ά
like, per. object. methods. only that one object gets them. nobody else. it's giving exclusive π
π SYNOPSIS π
use Object::Extend qw(extend);
my $foo1 = Foo->new;
my $foo2 = Foo->new;
extend $foo1 => {
bar => sub { ... },
};
$foo1->bar; # OK
$foo2->bar; # error
see how $foo1 got a whole new method and $foo2 is just standing there like π? yeah. that's the whole vibe. main character energy for exactly one object π
π¦ DESCRIPTION π¦
This module lets objects get extended with per-object methods β which is basically the same energy as using singleton methods in Ruby (there's a whole explainer linked as "eigenclasses demystified" at http://madebydna.com/all/code/2011/06/24/eigenclasses-demystified.html if you wanna go read it, bestie π).
So here's the tea β: object methods get added to an object-specific shim class β that's what everyone calls an "eigenclass" β and that shim class extends the object's original class. The original class itself? left completely unchanged. untouched. unbothered. pristine. periodt π
β¨ EXPORTS β¨
extend π
extend takes an object, plus a hash or a hashref of method names and method values (coderefs), and it adds those methods to the object's shim class. Then the object gets blessed into that class and returned. slay π₯
You can use it as a standalone statement:
extend $object, foo => sub { ... }, bar => \&bar;
Or in an expression, like:
return extend($object => { bar => sub { ... } })->bar;
In both cases, extend operates on and returns the object you handed it β a new object is never created, no cap. So if you actually want a new object, make one yourself, like:
my $object2 = Object->new($object1);
my $object3 = clone($object1);
extend($object2, foo => sub { ... })->foo;
return extend($object3 => ...);
And you can extend objects as many times as you want, with new methods or overridden ones:
# call the original method
my $object = Foo->new;
$object->foo;
# override the original method
extend $object, foo => sub { ... };
$object->foo;
# add a new method
extend $object, bar => sub { ... };
$object->bar;
with π
This sub can optionally be imported, and it just makes your extend usage read nicer β more descriptive, yk? It takes and returns a hashref of method names and coderefs:
use Object::Extend qw(extend with);
extend $object => with { foo => sub { ... } };
cute, right? π«Ά
π SINGLETON π
Every extended object's shim class includes an additional (empty) class in its @ISA, and that's the little flag that says "hey, this object has been extended". You can get at the name of that class by importing the SINGLETON constant, like:
use Object::Extend qw(SINGLETON);
if ($object->isa(SINGLETON)) { ... } # object extended with object-specific methods
so you can vibe-check any object and instantly know if it's got object-specific methods on it β¨
β VERSION β
0.4.0
π SEE ALSO π
Class::SingletonMethodMooseX::SingletonMethodMouseX::SingletonMethodSingletonMethodathttps://github.com/tom-lpsd/p5-singleton-method
go read those too, bestie, they're the cousins π
π AUTHOR π
chocolateboy <chocolate@cpan.org>
(iconic name honestly, ate)
π COPYRIGHT AND LICENSE π
Copyright (C) 2013 by chocolateboy
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.14.2 or, at your option, any later version of Perl 5 you may have available.
ok so like, legally speaking that means: free software, same terms as Perl, 5.14.2 or whatever newer Perl 5 you've got. don't be weird about it π
and that's the whole page, bestie!! this one's from perl v5.36.0, dated 2023-02-03, and it's Object::Extend(3pm) β thanks for reading, ily, go extend some objects π¦πβ¨
π¦ where this page comes from
- debian bookworm: libobject-extend-perl 0.4.0-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libobject-extend-perl 0.4.0-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libobject-extend-perl 0.4.0-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libobject-extend-perl 0.4.0-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libobject-extend-perl 0.4.0-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms