PDL::Objects(3pm)
π NAME π
PDL::Objects β object-orientation, what it even is, and how to exploit it π
β¨ DESCRIPTION β¨
okay so bestie, this whole page is the tea on how to subclass PDL objects for fun and profit π no cap, that's the actual goal.
π¦ Why subclass? π¦
There are basically two reasons you'd wanna subclass ndarrays, and honestly both are valid.
Reason one: you just want to be able to call your own lil routines like
$ndarray->something
without spilling your whole vibe into the PDL namespace β and yeah, not messing up the PDL namespace is a worthy goal, indeed! π«Ά
Reason two: you want to give some functions special handling, or carry more info about the data the ndarray actually contains. π
Case one you can absolutely do like this:
package BAR;
our @ISA=qw/PDL/;
sub foo {my($this) = @_; fiddle;}
package main;
$x = BAR->pdl(5);
$x->foo;
BUT π here's the catch, and it's giving plot twist: because a PDL object is an opaque reference to a C struct, it is not instantly possible to extend the PDL class by, say, extra data via subclassing. like, that door just isn't open on its own. π
π§ Perl values into underlying C values π§
so "typemap"s turn Perl values into the C entities the C code needs. PDL's one uses "SvPDLV" to extract a "pdl*" from a Perl value. β¨ And it knows how to deal with all of these:
"simple"blessed scalars that have a C pointer- blessed hash-refs that have a
"PDL"member that's a C pointer as above - blessed hash-refs that have a
"PDL"member that's a Perl code-ref. It will be called, and is expected to return an ndarray that conforms to one of cases 1-2 or 4-5 above. As of 2.094, the original hash-ref will be passed as the first argument. This means you can give a method as shown below, rather than needing to make a closure each time. Example fromt/subclass.t:
package PDL::Derived2;
# This is a array of ones of dim 'Coeff'
# All that is stored initially is "Coeff", the
# PDL array is only realised when a boring PDL
# function is called on it. One can imagine methods
# in PDL::Derived2 doing manipulation on the Coeffs
# rather than actualizing the data.
our @ISA = qw/PDL/;
sub new {
my $class = shift;
bless {
Coeff=>shift,
PDL=>\&cache,
SomethingElse=>42,
}, $class;
}
# Actualize the value (demonstrating cacheing)
# One can imagine expiring the cache if say, Coeffs change
sub cache {
my $self = shift;
return $self->{Cache} if exists $self->{Cache};
$self->{Cache} = PDL->ones(@$self{qw(Coeff Coeff)})+2;
}
"simple"Perl data, either a scalar or an array-reference- blessed hash-refs that are a Math::Complex (or subclass) object β special case of the
"scalar"above
The rest of this document deals with the second case above. π
πΈ Inheritance πΈ
To enable subclassing, make a package and bless a hash-ref into it with a "PDL" member. Make that package inherit from "PDL", and redefine the method "initialize".
package FOO;
our ISA = qw(PDL::Hash);
sub initialize {
my $class = shift;
my $self = $class->SUPER::initialize(@_);
$self->{creation_time} = time(); # necessary extension :-)
$self;
}
All PDL constructors will call initialize() to make sure that your extensions are added by all PDL constructors automatically. like, you don't gotta lift a finger. π
β¨
β οΈ bestie, do remember: if you subclass a class that is subclassed from an ndarray, you need to call"SUPER::initialize". And make sure it is callable as an instance method, e.g. by copying data from$classifref $classis true. fr fr, don't skip this part π
π Examples π
You can find some simple examples of PDL subclassing in the PDL distribution test-case files. Look in t/subclass.t. π
π Output Auto-Creation and Subclassed Objects π
For PDL Functions where the output is created and returned, PDL will call the subclassed object's "initialize" method on either instance or class to create the output object. (See PDL::Indexing for a discussion on Output Auto-Creation.) This behavior is summarized as follows:
PDL will call "initialize" on the first input argument if it is an object, else on "PDL" 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. π¦
π 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. π«Άπ
and that's the whole page bestie, mwah π this was User Contributed Perl Documentation for PDL::Objects(3pm), perl v5.42.3, dated 2026-08-20. stay slaying, and may your typemaps always resolve on the first try β¨π
πΈ
π¦ where this page comes from
- ubuntu stonking: pdl 1:2.106-1build1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms