Clownfish::Obj - Base class for all objects.
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; }
Clownfish::Obj is the base class of the Clownfish object hierarchy.
From the standpoint of a Perl programmer, all classes are implemented as blessed scalar references, with the scalar storing a pointer to a C struct.
The recommended way to subclass Clownfish::Obj and its descendants is to use the inside-out design pattern. (See Class::InsideOut for an introduction to inside-out techniques.)
Since the blessed scalar stores a C pointer value which is unique per-object,
$$self
can be used as an inside-out ID.
# Accessor for 'foo' member variable. sub get_foo { my $self = shift; return $foo{$$self}; }
Caveats:
my $self = $class->SUPER::new;
Abstract constructor -- must be invoked via a subclass. Attempting to instantiate objects of class "Clownfish::Obj" directly causes an error.
Takes no arguments; if any are supplied, an error will be reported.
my $result = $obj->clone();
Return a clone of the object.
my $int = $obj->compare_to($other);
Indicate whether one object is less than, equal to, or greater than another.
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
.
my $native = $obj->to_perl;
Tries to convert the object to its native Perl representation.
my $bool = $obj->equals($other);
Indicate whether two objects are the same. By default, compares the memory address.
All Clownfish classes implement a DESTROY method; if you override it in a subclass,
you must call $self->SUPER::DESTROY
to avoid leaking memory.
my $string = $obj->to_string();
Generic stringification: “ClassName@hex_mem_address”.
Copyright © 2010-2015 The Apache Software Foundation, Licensed under the
Apache License, Version 2.0.
Apache Lucy, Lucy, Apache, the Apache feather logo, and the Apache Lucy project logo are trademarks of The
Apache Software Foundation. All other marks mentioned may be trademarks or registered trademarks of their
respective owners.