Skip to content

Commit

Permalink
Return nothing, instead of undef, when can() is called with missing m…
Browse files Browse the repository at this point in the history
…ethods
  • Loading branch information
Ovid committed Jul 15, 2024
1 parent 4795c9c commit f2b6d8f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
10 changes: 9 additions & 1 deletion t/uni/universal.t
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ BEGIN {
use utf8;
use open qw( :utf8 :std );

plan tests => 103;
plan tests => 105;

$a = {};
bless $a, "Bòb";
Expand Down Expand Up @@ -195,3 +195,11 @@ ok( scalar $child->can(qw(bar baz)), 'can(@methods) in scalar content should ret

ok( !Child->can(qw(foo baz no_such_method)), 'can(@methods) with non-existent methods should already return nothing' );
ok( !scalar Child->can(qw(foo baz no_such_method)), 'can(@methods) with non-existent methods should return false in scalar context' );

#
# Check that can() returns an empty list when called with missing methods
#
@methods = Child->can(qw(no_such_method));
ok( !@methods, 'can(@methods) with non-existent methods should already return nothing even when assigned to an array' );
@methods = $child->can(qw(no_such_method));
ok( !@methods, 'can(@methods) with non-existent methods should return even when assigned to an array' );
6 changes: 3 additions & 3 deletions universal.c
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ XS(XS_UNIVERSAL_can)
precedence here over the numeric form, as (!1)->foo treats the
invocant as the empty string, though it is a dualvar. */
if (!SvOK(sv) || (SvPOK(sv) && !SvCUR(sv)))
XSRETURN_UNDEF;
XSRETURN_EMPTY;

if (SvROK(sv)) {
sv = MUTABLE_SV(SvRV(sv));
Expand All @@ -512,15 +512,15 @@ XS(XS_UNIVERSAL_can)
for (i = 1; i < items; i++) {
GV * const gv = gv_fetchmethod_sv_flags(pkg, ST(i), 0);
if (!gv || !isGV(gv) || !GvCV(gv))
XSRETURN_UNDEF;
XSRETURN_EMPTY;
else {
ST(i-1) = sv_2mortal(newRV(MUTABLE_SV(GvCV(gv))));
}
}
XSRETURN(items - 1);
}

XSRETURN_UNDEF;
XSRETURN_EMPTY;
}

XS(XS_UNIVERSAL_DOES); /* prototype to pass -Wmissing-prototypes */
Expand Down

0 comments on commit f2b6d8f

Please sign in to comment.