.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "OBJECTS 1p" .TH OBJECTS 1p 2024-04-25 "perl v5.38.2" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH NAME PDL::Objects \-\- Object\-Orientation, what is it and how to exploit it .SH DESCRIPTION .IX Header "DESCRIPTION" This still needs to be written properly. [Also, is there a good reason we don't recommend storing extra object data in the header hash?] .SS Inheritance .IX Subsection "Inheritance" There are basically two reasons for subclassing ndarrays. The first is simply that you want to be able to use your own routines like .PP .Vb 1 \& $ndarray\->something() .Ve .PP 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 ndarray contains. In the first case, you can do with .PP .Vb 3 \& package BAR; \& @ISA=qw/PDL/; \& sub foo {my($this) = @_; fiddle;} \& \& package main; \& $x = PDL::pdl(BAR,5); \& $x\->foo(); .Ve .PP 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 \fIhas-a\fR relation for blessed hashes. You can get the \fIHAS-A\fR behave like \&\fIIS-A\fR simply in that you assign the \f(CW\*(C`PDL\*(C'\fR object to the attribute named PDL and redefine the method \fBinitialize()\fR. .PP .Vb 1 \& 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; \& } .Ve .PP All PDL constructors will call \fBinitialize()\fR to make sure that your extensions are added by \fIall\fR PDL constructors automatically. The \&\f(CW\*(C`PDL\*(C'\fR attribute is used by perlDL to store the PDL object and all PDL methods use this attribute automatically if they are called with a blessed hash reference instead of a PDL object (a blessed scalar). .PP Do remember that if you subclass a class that is subclassed from an ndarray, you need to call SUPER::initialize. .PP NEED STUFF ABOUT CODE REFs!! .SS Examples .IX Subsection "Examples" You can find some simple examples of PDL subclassing in the PDL distribution test-case files. Look in \f(CW\*(C`t/subclass2.t\*(C'\fR, \f(CW\*(C`t/subclass3.t\*(C'\fR, etc. .SS "Output Auto-Creation and Subclassed Objects" .IX Subsection "Output Auto-Creation and Subclassed Objects" For PDL Functions where the output is created and returned, PDL will either call the subclassed object's \f(CW\*(C`initialize\*(C'\fR or \f(CW\*(C`copy\*(C'\fR method to create the output object. (See PDL::Indexing for a discussion on Output Auto-Creation.) This behavior is summarized as follows: .IP \(bu 1 For \fISimple\fR functions, defined as having a signature of .Sp .Vb 1 \& func( a(), [o]b() ) .Ve .Sp PDL will call \f(CW$a\fR\->copy to create the output object. .Sp In the spirit of the Perl philosophy of making \fIEasy Things Easy\fR, This behavior enables PDL-subclassed objects to be written without having to overload the many simple PDL functions in this category. .Sp The file t/subclass4.t in the PDL Distribution tests for this behavior. See that file for an example. .IP \(bu 1 For other functions, PDL will call \f(CW$class\fR\->initialize to create the output object. Where \f(CW$class\fR is the class name of the first argument supplied to the function. .Sp For these more complex cases, it is difficult to second-guess the subclassed object's designer to know if a \f(CW\*(C`copy\*(C'\fR or a \f(CW\*(C`initialize\*(C'\fR is appropriate. So for these cases, \&\f(CW$class\fR\->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. .SH AUTHOR .IX Header "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. All rights reserved. There is no warranty. You are allowed to copy this on the same terms as Perl itself.