man PDL::Objects () - PDL::Objects -- Object-Orientation, what is it and how to exploit it
NAME
PDL::Objects -- Object-Orientation, what is it and how to exploit it
DESCRIPTION
This still needs to be written properly.
Inheritance
There are basically two reasons for subclassing piddles. The first is simply that you want to be able to use your own routines like
$piddle->something()
but don't want to mess up the PDL namespace (a worthy goal, indeed!). The other is that you wish to provide special handling of some functions or more information about the data the piddle contains. In the first case, you can do with
package BAR; @ISA=qw/PDL/; sub foo {my($this) = @_; fiddle;}
package main; $a = PDL::pdl(BAR,5); $a->foo();
However, because a PDL object is an opaque reference to a C struct, it is not possible to extend the PDL class by e.g. extra data via subclassing. To circumvent this problem PerlDL has built-in support to extent the PDL class via the has-a relation for blessed hashes. You can get the HAS-A behave like IS-A simply in that you assign the CWPDL object to the attribute named PDL and redefine the method initialize().
package FOO;
@FOO::ISA = qw(PDL); sub initialize { my $class = shift; my $self = { creation_time => time(), # necessary extension :-) PDL => null, # used to store PDL object }; bless $self, $class; }
All PDL constructors will call initialize() to make sure that your extentions are added by all PDL constructors automaticly. The CWPDL attribute is used by perlDL to store the PDL object and all PDL methods use this attribute automaticly if they are called with a blessed hash reference instead of a PDL object (a blessed scalar).
Do remember that if you subclass a class that is subclassed from a piddle, you need to call SUPER::initialize.
NEED STUFF ABOUT CODE REFs!!
Examples
You can find some simple examples of PDL subclassing in the PDL distribution test-case files. Look in CWt/subclass2.t, CWt/subclass3.t, etc.
Output Auto-Creation and Subclassed Objects
For PDL Functions where the output is created and returned, PDL will either call the subclassed object's CWinitialize or CWcopy method to create the output object. (See PDL::Indexing for a discussion on Output Auto-Creation.) This behavior is summarized as follows:
- •
-
For Simple functions, defined as having a signature of
func( a(), [o]b() )
PDL will call CW$a->copy to create the output object. In the spirit of the perl philosophy of making Easy Things Easy, This behavior enables PDL-subclassed objects to be written without having to overload the many simple PDL functions in this category. The file t/subclass4.t in the PDL Distribution tests for this behavior. See that file for an example. - •
- For other functions, PDL will call CW$class->initialize to create the output object. Where CW$class is the class name of the first argument supplied to the function. For these more complex cases, it is difficult to second-guess the subclassed-object's designer to know if a CWcopy or a CWinitialize is appropriate. So for these cases, CW$class->initialize is called by default. If this is not appropriate for you, overload the function in your subclass and do whatever is appropriate is the overloaded function's code.
AUTHOR
Copyright (C) Karl Glazebrook (kgb@aaoepp.aao.gov.au), Tuomas J. Lukka, (lukka@husc.harvard.edu) and Christian Soeller (c.soeller@auckland.ac.nz) 2000. Commercial reproduction of this documentation in a different format is forbidden.