Skip to content

Commit

Permalink
if $pdl->{PDL} is code ref, pass $pdl as arg
Browse files Browse the repository at this point in the history
  • Loading branch information
mohawk2 committed Oct 24, 2024
1 parent ad716c5 commit 79522f3
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 20 deletions.
29 changes: 12 additions & 17 deletions Basic/Core/pdlcore.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,27 +69,22 @@ pdl* pdl_SvPDLV ( SV* sv ) {
and allow normal PDL functions to still work so long
as the {PDL} code returns a standard ndarray on
demand - KGB */

if (SvROK(*svp) && SvTYPE(SvRV(*svp)) == SVt_PVCV) {
dSP;
ENTER ;
SAVETMPS ;
PUSHMARK(SP) ;

int count = perl_call_sv(*svp, G_SCALAR|G_NOARGS);

SPAGAIN ;

ENTER;
SAVETMPS;
PUSHMARK(SP);
XPUSHs(sv);
PUTBACK;
int count = perl_call_sv(*svp, G_SCALAR);
SPAGAIN;
if (count != 1)
croak("Execution of PDL structure failed to return one value\n") ;

croak("Execution of PDL structure failed to return one value\n");
sv=newSVsv(POPs);

PUTBACK ;
FREETMPS ;
LEAVE ;
}
else {
PUTBACK;
FREETMPS;
LEAVE;
} else {
sv = *svp;
}

Expand Down
8 changes: 6 additions & 2 deletions Basic/Pod/Objects.pod
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ blessed hash-refs that have a C<PDL> member that's a C pointer as above

blessed hash-refs that have a C<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. Example from F<t/subclass.t>:
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 from F<t/subclass.t>:

package PDL::Derived2;
# This is a array of ones of dim 'Coeff'
Expand All @@ -65,7 +69,7 @@ to one of cases 1-2 or 4-5 above. Example from F<t/subclass.t>:
my $class = shift;
bless {
Coeff=>shift,
PDL=>sub { return $_[0]->cache },
PDL=>\&cache,
SomethingElse=>42,
}, $class;
}
Expand Down
1 change: 1 addition & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- IO::FlexRaw::mapflex stop allocating memory (#501) - thanks @vitstradal for report
- add Primitive::approx_artol
- get_dataref now works even with DONTTOUCHDATA which is really about allocation
- if $pdl->{PDL} is code ref, $pdl now passed as arg

2.093 2024-09-29
- PDL.set_datatype now doesn't physicalise input, PDL.convert_type does
Expand Down
2 changes: 1 addition & 1 deletion t/subclass.t
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ package PDL::Derived2;
our @ISA = qw/PDL/;
sub new {
my $class = shift;
bless {Coeff=>shift, PDL=>sub { return $_[0]->cache }, SomethingElse=>42}, $class;
bless {Coeff=>shift, PDL=>\&cache, SomethingElse=>42}, $class;
}
# Actualize the value (demonstrating cacheing)
# One can imagine expiring the cache if say, Coeffs change
Expand Down

0 comments on commit 79522f3

Please sign in to comment.