1OBJECTS(1) User Contributed Perl Documentation OBJECTS(1)
2
3
4
6 PDL::Objects -- Object-Orientation, what is it and how to exploit it
7
9 This still needs to be written properly. [Also, is there a good reason
10 we don't recommend storing extra object data in the header hash?]
11
12 Inheritance
13 There are basically two reasons for subclassing ndarrays. The first is
14 simply that you want to be able to use your own routines like
15
16 $ndarray->something()
17
18 but don't want to mess up the PDL namespace (a worthy goal, indeed!).
19 The other is that you wish to provide special handling of some
20 functions or more information about the data the ndarray contains. In
21 the first case, you can do with
22
23 package BAR;
24 @ISA=qw/PDL/;
25 sub foo {my($this) = @_; fiddle;}
26
27 package main;
28 $x = PDL::pdl(BAR,5);
29 $x->foo();
30
31 However, because a PDL object is an opaque reference to a C struct, it
32 is not possible to extend the PDL class by e.g. extra data via
33 subclassing. To circumvent this problem PerlDL has built-in support to
34 extent the PDL class via the has-a relation for blessed hashes. You
35 can get the HAS-A behave like IS-A simply in that you assign the "PDL"
36 object to the attribute named PDL and redefine the method initialize().
37
38 package FOO;
39
40 @FOO::ISA = qw(PDL);
41 sub initialize {
42 my $class = shift;
43 my $self = {
44 creation_time => time(), # necessary extension :-)
45 PDL => null, # used to store PDL object
46 };
47 bless $self, $class;
48 }
49
50 All PDL constructors will call initialize() to make sure that your
51 extensions are added by all PDL constructors automatically. The "PDL"
52 attribute is used by perlDL to store the PDL object and all PDL methods
53 use this attribute automatically if they are called with a blessed hash
54 reference instead of a PDL object (a blessed scalar).
55
56 Do remember that if you subclass a class that is subclassed from an
57 ndarray, you need to call SUPER::initialize.
58
59 NEED STUFF ABOUT CODE REFs!!
60
61 Examples
62 You can find some simple examples of PDL subclassing in the PDL
63 distribution test-case files. Look in "t/subclass2.t", "t/subclass3.t",
64 etc.
65
66 Output Auto-Creation and Subclassed Objects
67 For PDL Functions where the output is created and returned, PDL will
68 either call the subclassed object's "initialize" or "copy" method to
69 create the output object. (See PDL::Indexing for a discussion on Output
70 Auto-Creation.) This behavior is summarized as follows:
71
72 •
73 For Simple functions, defined as having a signature of
74
75 func( a(), [o]b() )
76
77 PDL will call $a->copy to create the output object.
78
79 In the spirit of the Perl philosophy of making Easy Things Easy, This
80 behavior enables PDL-subclassed objects to be written without having
81 to overload the many simple PDL functions in this category.
82
83 The file t/subclass4.t in the PDL Distribution tests for this
84 behavior. See that file for an example.
85
86 •
87 For other functions, PDL will call $class->initialize to create the
88 output object. Where $class is the class name of the first argument
89 supplied to the function.
90
91 For these more complex cases, it is difficult to second-guess the
92 subclassed object's designer to know if a "copy" or a "initialize" is
93 appropriate. So for these cases, $class->initialize is called by
94 default. If this is not appropriate for you, overload the function in
95 your subclass and do whatever is appropriate is the overloaded
96 function's code.
97
99 Copyright (C) Karl Glazebrook (kgb@aaoepp.aao.gov.au), Tuomas J. Lukka,
100 (lukka@husc.harvard.edu) and Christian Soeller
101 (c.soeller@auckland.ac.nz) 2000. All rights reserved. There is no
102 warranty. You are allowed to copy this on the same terms as Perl
103 itself.
104
105
106
107perl v5.36.0 2022-07-22 OBJECTS(1)