PDLA::Objects -- Object-Orientation, what is it and how to exploit it


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?]


There are basically two reasons for subclassing piddles. The first is simply that you want to be able to use your own routines like


but don't want to mess up the PDLA 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;
        sub foo {my($this) = @_; fiddle;}

        package main;
        $x = PDLA::pdl(BAR,5);

However, because a PDLA object is an opaque reference to a C struct, it is not possible to extend the PDLA class by e.g. extra data via subclassing. To circumvent this problem PerlDL has built-in support to extent the PDLA 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 PDLA object to the attribute named PDLA and redefine the method initialize().

    package FOO;

    @FOO::ISA = qw(PDLA);
    sub initialize {
        my $class = shift;
        my $self = {
                creation_time => time(),  # necessary extension :-)
                PDLA => null,             # used to store PDLA object
        bless $self, $class;

All PDLA constructors will call initialize() to make sure that your extensions are added by all PDLA constructors automatically. The PDLA attribute is used by perlDL to store the PDLA object and all PDLA methods use this attribute automatically if they are called with a blessed hash reference instead of a PDLA object (a blessed scalar).

Do remember that if you subclass a class that is subclassed from a piddle, you need to call SUPER::initialize.



You can find some simple examples of PDLA subclassing in the PDLA distribution test-case files. Look in t/subclass2.t, t/subclass3.t, etc.

Output Auto-Creation and Subclassed Objects

For PDLA Functions where the output is created and returned, PDLA will either call the subclassed object's initialize or copy method to create the output object. (See PDLA::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() )

    PDLA will call $a->copy to create the output object.

    In the spirit of the Perl philosophy of making Easy Things Easy, This behavior enables PDLA-subclassed objects to be written without having to overload the many simple PDLA functions in this category.

    The file t/subclass4.t in the PDLA Distribution tests for this behavior. See that file for an example.

  • For other functions, PDLA will call $class->initialize to create the output object. Where $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 copy or a initialize is appropriate. So for these cases, $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.


Copyright (C) Karl Glazebrook (, Tuomas J. Lukka, ( and Christian Soeller ( 2000. Commercial reproduction of this documentation in a different format is forbidden.