Clownfish::Obj(3pm)
π 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
undefunless the API specifically says that returningundefis 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:$$selfis your inside-out ID,SUPER::DESTROYis non-negotiable, andundefis 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
- fedora 43: perl-Clownfish 0.8.0-1.fc43 Β· Apache-2.0
- fedora 44: perl-Clownfish 0.8.0-1.fc44 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms