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