OBJECTS(1p)

πŸ“¦
version 2.085 Β· 3 versions
, in ubuntu noble

πŸ‘‘ 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:

  1. you just wanna be able to use your own routines like
$ndarray->something()

without messing up the PDL namespace (a worthy goal, indeed! πŸ‘‘)

  1. 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->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'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