From 6c692eb49735ae6901652a974eb76e2a0c36dfae Mon Sep 17 00:00:00 2001 From: Ed J Date: Fri, 10 Jan 2025 23:18:47 +0000 Subject: [PATCH] overloaded operators now throw exception if given undef argument - #519 --- Changes | 1 + lib/PDL/PP.pm | 24 +++++++++++++++++------- t/ops.t | 5 +++++ 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/Changes b/Changes index 4d2ef6383..0c9b0b3ca 100644 --- a/Changes +++ b/Changes @@ -14,6 +14,7 @@ - InplaceDoc now automatically generated like BadDoc, Signature doc includes types - Overload key added to PP (#519) - "usage" section of xform docs now automatically generated (#519) +- overloaded operators now throw exception if given undef argument (#519) - thanks @cgtinney for report 2.098 2025-01-03 - fix Windows build problems diff --git a/lib/PDL/PP.pm b/lib/PDL/PP.pm index 188b94eac..20e6ba8d7 100644 --- a/lib/PDL/PP.pm +++ b/lib/PDL/PP.pm @@ -1441,25 +1441,35 @@ $PDL::PP::deftbl = my $fullname = $::PDLOBJ."::$name"; if ($one_arg) { $ret .= pp_line_numbers(__LINE__, < sub { $fullname(\$_[0]) }; +use overload '$op' => sub { + Carp::confess("$fullname: overloaded '$op' given undef") + if grep !defined, \$_[0]; + $fullname(\$_[0]); +}; EOF } else { $ret .= pp_line_numbers(__LINE__, < \$overload_sub = sub(;\@) { - return $fullname($bitwise_passon) unless ref \$_[1] - && (ref \$_[1] ne '$::PDLOBJ') - && defined(\$foo = overload::Method(\$_[1], '$op')) - && \$foo != \$overload_sub; # recursion guard - goto &\$foo; + Carp::confess("$fullname: overloaded '$op' given undef") + if grep !defined, \@_[0,1]; + return $fullname($bitwise_passon) unless ref \$_[1] + && (ref \$_[1] ne '$::PDLOBJ') + && defined(\$foo = overload::Method(\$_[1], '$op')) + && \$foo != \$overload_sub; # recursion guard + goto &\$foo; }; } EOF } $ret .= pp_line_numbers(__LINE__, <