1Clownfish::Obj(3) User Contributed Perl Documentation Clownfish::Obj(3)
2
3
4
6 Clownfish::Obj - Base class for all objects.
7
9 package MyObj;
10 use base qw( Clownfish::Obj );
11
12 # Inside-out member var.
13 my %foo;
14
15 sub new {
16 my ( $class, %args ) = @_;
17 my $foo = delete $args{foo};
18 my $self = $class->SUPER::new(%args);
19 $foo{$$self} = $foo;
20 return $self;
21 }
22
23 sub get_foo {
24 my $self = shift;
25 return $foo{$$self};
26 }
27
28 sub DESTROY {
29 my $self = shift;
30 delete $foo{$$self};
31 $self->SUPER::DESTROY;
32 }
33
35 Clownfish::Obj is the base class of the Clownfish object hierarchy.
36
37 From the standpoint of a Perl programmer, all classes are implemented
38 as blessed scalar references, with the scalar storing a pointer to a C
39 struct.
40
41 Subclassing
42 The recommended way to subclass Clownfish::Obj and its descendants is
43 to use the inside-out design pattern. (See Class::InsideOut for an
44 introduction to inside-out techniques.)
45
46 Since the blessed scalar stores a C pointer value which is unique per-
47 object, $$self can be used as an inside-out ID.
48
49 # Accessor for 'foo' member variable.
50 sub get_foo {
51 my $self = shift;
52 return $foo{$$self};
53 }
54
55 Caveats:
56
57 • Inside-out aficionados will have noted that the "cached scalar id"
58 stratagem recommended above isn't compatible with ithreads.
59
60 • Overridden methods must not return undef unless the API specifies
61 that returning undef is permissible.
62
64 new
65 my $self = $class->SUPER::new;
66
67 Abstract constructor -- must be invoked via a subclass. Attempting to
68 instantiate objects of class "Clownfish::Obj" directly causes an error.
69
70 Takes no arguments; if any are supplied, an error will be reported.
71
73 clone
74 my $result = $obj->clone();
75
76 Return a clone of the object.
77
78 compare_to
79 my $int = $obj->compare_to($other);
80
81 Indicate whether one object is less than, equal to, or greater than
82 another.
83
84 • other - Another Obj.
85
86 Returns: 0 if the objects are equal, a negative number if "self" is
87 less than "other", and a positive number if "self" is greater than
88 "other".
89
91 to_perl
92 my $native = $obj->to_perl;
93
94 Tries to convert the object to its native Perl representation.
95
96 equals
97 my $bool = $obj->equals($other);
98
99 Indicate whether two objects are the same. By default, compares the
100 memory address.
101
102 • other - Another Obj.
103
104 DESTROY
105 All Clownfish classes implement a DESTROY method; if you override it in
106 a subclass, you must call "$self->SUPER::DESTROY" to avoid leaking
107 memory.
108
109 to_string
110 my $string = $obj->to_string();
111
112 Generic stringification: “ClassName@hex_mem_address”.
113
114
115
116perl v5.36.0 2023-01-20 Clownfish::Obj(3)