Clownfish::Obj(3pm)

πŸ“¦
version 0.8.0 Β· 3 versions
, in fedora 43, fedora 44

πŸ’– Clownfish::Obj πŸ’–

okay bestie, gather round, because we're talking about Clownfish::Obj β€” the base class for all objects β€” straight out of the User Contributed Perl Documentation, perl v5.42.3 energy, no cap. 🫢


✨ SYNOPSIS ✨

like, here's the whole vibe in one go:

package MyObj;
use base qw( Clownfish::Obj );

# Inside-out member var.
my %foo;

sub new {
    my ( $class, %args ) = @_;
    my $foo = delete $args{foo};
    my $self = $class->SUPER::new(%args);
    $foo{$$self} = $foo;
    return $self;
}

sub get_foo {
    my $self = shift;
    return $foo{$$self};
}

sub DESTROY {
    my $self = shift;
    delete $foo{$$self};
    $self->SUPER::DESTROY;
}

🌸 DESCRIPTION 🌸

Clownfish::Obj is the base class of the Clownfish object hierarchy. the mother of all the things. the blueprint. the moment. πŸ‘‘

and here's the tea from the standpoint of a Perl programmer: all classes are implemented as blessed scalar references, where that scalar is storing a pointer to a C struct. that's it. that's the whole architecture. slay. πŸ’…

πŸ¦‹ Subclassing πŸ¦‹

the recommended way to subclass Clownfish::Obj and all its descendants is to use the inside-out design pattern. (if you want an intro to inside-out techniques, go peek at Class::InsideOut β€” it's giving tutorial, fr fr.)

and here's why it works so good: since the blessed scalar stores a C pointer value that's unique per-object, you can literally use $$self as an inside-out ID. iconic behavior honestly.

# Accessor for 'foo' member variable.
sub get_foo {
    my $self = shift;
    return $foo{$$self};
}

😭 Caveats (the important part, don't skip) 😭

  • Inside-out aficionados will have noted that the "cached scalar id" stratagem recommended above isn't compatible with ithreads. yeah. sorry bestie.
  • Overridden methods must not return undef unless the API specifically says that returning undef is permissible. like, actually. don't do it. 🚫

πŸŽ€ CONSTRUCTOR πŸŽ€

new

my $self = $class->SUPER::new;

it's an abstract constructor β€” meaning it must be invoked via a subclass. if you try to instantiate objects of class Clownfish::Obj directly? that causes an error. nope. not allowed. πŸ™ˆ

it also takes no arguments β€” and if any get supplied anyway, an error will be reported. so don't try it. πŸ’€


🌈 ABSTRACT METHODS 🌈

clone

my $result = $obj->clone();

return a clone of the object. a little copy of itself. a mini-me. πŸͺžβœ¨

compare_to

my $int = $obj->compare_to($other);

indicate whether one object is less than, equal to, or greater than another. the classic three-way comparison moment.

  • other β€” another Obj.

Returns: 0 if the objects are equal, a negative number if self is less than other, and a positive number if self is greater than other. 🫢


πŸ’œ METHODS πŸ’œ

to_perl

my $native = $obj->to_perl;

tries to convert the object to its native Perl representation. it's giving translator. it's giving "let me speak your language." 🌐

DESTROY

every single Clownfish class implements a DESTROY method, periodt. so if you override it in a subclass, you must call:

$self->SUPER::DESTROY

...otherwise you will leak memory. 😭 like, don't forget. this one's not optional. it's the whole point.

equals

my $bool = $obj->equals($other);

indicate whether two objects are the same. by default, this compares the memory address β€” so it's an identity check, not a vibe check. πŸ‘€

  • other β€” another Obj.

to_string

my $string = $obj->to_string();

generic stringification, and the format is exactly:

"ClassName@hex_mem_address"

so like, the class name, an @, then the hex memory address. classic. chef's kiss. πŸ‘‘


πŸ’– reminder bestie: $$self is your inside-out ID, SUPER::DESTROY is non-negotiable, and undef is not a valid return unless the API says so. iykyk.

and that's the whole page, no cap. πŸ¦‹ written by the Perl docs crew, perl v5.42.3, dated 2026-09-02, and it's literally Clownfish::Obj(3pm) β€” go forth and subclass responsibly, mwah πŸ’…βœ¨πŸŽ€

πŸ“¦ where this page comes from