| Clownfish::Obj - phpMan
Clownfish::Obj(3) User Contributed Perl Documentation Clownfish::Obj(3)
NAME
Clownfish::Obj - Base class for all objects.
SYNOPSIS
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.
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.
Subclassing
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:
· Inside-out aficionados will have noted that the "cached scalar id" stratagem
recommended above isn't compatible with ithreads.
· Overridden methods must not return undef unless the API specifies that returning undef
is permissible. (Failure to adhere to this rule currently results in a segfault
rather than an exception.)
CONSTRUCTOR
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.
DESTRUCTOR
DESTROY
All Clownfish classes implement a DESTROY method; if you override it in a subclass, you
must call "$self->SUPER::DESTROY" to avoid leaking memory.
ABSTRACT METHODS
to_i64()
Convert the object to a 64-bit integer.
to_f64()
Convert the object to a double precision floating point number.
METHODS
to_string()
Generic stringification: "ClassName@hex_mem_address".
equals(other)
Indicate whether two objects are the same. By default, compares the memory address.
· other - Another Obj.
perl v5.20.2 2015-12-01 Clownfish::Obj(3)
|