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 πŸ‘‘

go read those too, bestie, they're the cousins πŸ’œ


πŸ“ AUTHOR πŸ“

chocolateboy <chocolate@cpan.org>

(iconic name honestly, ate)


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