OBJECTS(1p)
π OBJECTS(1p) β the PDL::Objects glow-up π
okay bestie, gather round, we're talking PDL::Objects β that's the User Contributed Perl Documentation for perl v5.38.2, and yes she's serving Object-Orientation realness. what even is object-orientation and how do we exploit it? let's get into it π¦
β¨ NAME β¨
PDL::Objects β Object-Orientation, what is it and how to exploit it π
πΈ DESCRIPTION πΈ
ngl bestie, this one still needs to be written properly. π
also, fr, is there a good reason we don't recommend storing extra object data in the header hash? asking for the whole class.
𧬠Inheritance (the family tree moment)
there are basically two reasons you'd want to subclass ndarrays, and honestly both are valid:
- you just wanna be able to use your own routines like
$ndarray->something()
without messing up the PDL namespace (a worthy goal, indeed! π)
- you wanna provide special handling of some functions, or just more info about the data the ndarray contains.
for case #1, you can literally just do:
package BAR;
@ISA=qw/PDL/;
sub foo {my($this) = @_; fiddle;}
package main;
$x = PDL::pdl(BAR,5);
$x->foo();
BUT. and this is the plot twist π«’ β because a PDL object is an opaque reference to a C struct, it is not possible to extend the PDL class by, like, extra data via subclassing. it's giving locked door, no key. π
so to circumvent this problem, PerlDL has built-in support to extent the PDL class via the has-a relation for blessed hashes. and the way you get HAS-A to behave like IS-A is iconic: you assign the "PDL" object to the attribute named PDL, and you redefine the method initialize(). slay.
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;
}
all PDL constructors will call initialize() to make sure your extensions get added by all PDL constructors automatically. no cap, automatic. β¨
the "PDL" attribute is what perlDL uses to store the PDL object, and all PDL methods use this attribute automatically if they're called with a blessed hash reference instead of a PDL object (which would be a blessed scalar).
π¨ do remember bestie: if you subclass a class that is subclassed from an ndarray, you need to call SUPER::initialize. periodt.
NEED STUFF ABOUT CODE REFs!! π
π§ Examples
you can find some simple examples of PDL subclassing in the PDL distribution test-case files. look in t/subclass2.t, t/subclass3.t, etc. go peek, they ate π
π Output Auto-Creation and Subclassed Objects
for PDL Functions where the output is created and returned, PDL will either call the subclassed object's "initialize" or "copy" method to create the output object. (see PDL::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() )
PDL will call $a->copy 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. main character energy, honestly. π
the file t/subclass4.t in the PDL Distribution tests for this behavior. see that file for an example.
- for other functions, PDL will call
$class->initializeto create the output object. where$classis the class name of the first argument supplied to the function.
for these more complex cases, it's 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. π
π 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.
okay that's the whole page bestie, you're officially educated πβ¨ this was perl v5.38.2, dated 2024-04-10 β and that's OBJECTS(1p), signing off π π«Ά
π¦ where this page comes from
- ubuntu noble: pdl 1:2.085-1ubuntu1 Β· 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