Skip to content

Commit 7156e69

Browse files
author
Father Chrysostomos
committed
[perl #105922] Allow any string before ->meth
The rules for filtering out what do not look like package names are not logical and disallow valid things like "::main", while allowing q"_#@*$!@*^(". This commit simply lets any non-empty string be used as a package name. If it is a typo, you’ll get an error anyway. This allows autobox-style calls like "3foo"->CORE::uc, or even "3foo"->uc if you set up @isa first. I made an exception for the empty string because it messes up caches somehow and causes subsequent method calls all to be called on the main package. I haven’t looked into that yet. I don’t know whether it’s worth it. The changes to the tests in cpan/Test-Simple have been submit- ted upstream.
1 parent daef195 commit 7156e69

File tree

4 files changed

+26
-18
lines changed

4 files changed

+26
-18
lines changed

cpan/Test-Simple/t/fail-more.t

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -248,22 +248,22 @@ ERR
248248

249249
#line 248
250250
isa_ok(42, "Wibble", "My Wibble");
251-
out_ok( <<OUT, <<ERR );
251+
out_like( <<OUT, <<ERR );
252252
not ok - My Wibble isa Wibble
253253
OUT
254254
# Failed test 'My Wibble isa Wibble'
255255
# at $0 line 248.
256-
# My Wibble isn't a class or reference
256+
# My Wibble isn't a .*
257257
ERR
258258

259259
#line 248
260260
isa_ok(42, "Wibble");
261-
out_ok( <<OUT, <<ERR );
262-
not ok - The thing isa Wibble
261+
out_like( <<OUT, <<ERR );
262+
not ok - The (thing|class) isa Wibble
263263
OUT
264-
# Failed test 'The thing isa Wibble'
264+
# Failed test 'The (thing|class) isa Wibble'
265265
# at $0 line 248.
266-
# The thing isn't a class or reference
266+
# The (thing|class) isn't a .*
267267
ERR
268268

269269
#line 258

pp_hot.c

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2956,12 +2956,14 @@ S_method_common(pTHX_ SV* meth, U32* hashp)
29562956
PERL_ARGS_ASSERT_METHOD_COMMON;
29572957

29582958
if (!sv)
2959+
undefined:
29592960
Perl_croak(aTHX_ "Can't call method \"%"SVf"\" on an undefined value",
29602961
SVfARG(meth));
29612962

29622963
SvGETMAGIC(sv);
29632964
if (SvROK(sv))
29642965
ob = MUTABLE_SV(SvRV(sv));
2966+
else if (!SvOK(sv)) goto undefined;
29652967
else {
29662968
GV* iogv;
29672969
STRLEN packlen;
@@ -2990,17 +2992,11 @@ S_method_common(pTHX_ SV* meth, U32* hashp)
29902992
!(ob=MUTABLE_SV(GvIO(iogv))))
29912993
{
29922994
/* this isn't the name of a filehandle either */
2993-
if (!packname ||
2994-
((UTF8_IS_START(*packname) && DO_UTF8(sv))
2995-
? !isIDFIRST_utf8((U8*)packname)
2996-
: !isIDFIRST_L1((U8)*packname)
2997-
))
2995+
if (!packname || !packlen)
29982996
{
2999-
/* diag_listed_as: Can't call method "%s" without a package or object reference */
3000-
Perl_croak(aTHX_ "Can't call method \"%"SVf"\" %s",
3001-
SVfARG(meth),
3002-
SvOK(sv) ? "without a package or object reference"
3003-
: "on an undefined value");
2997+
Perl_croak(aTHX_ "Can't call method \"%"SVf"\" "
2998+
"without a package or object reference",
2999+
SVfARG(meth));
30043000
}
30053001
/* assume it's a package name */
30063002
stash = gv_stashpvn(packname, packlen, packname_is_utf8 ? SVf_UTF8 : 0);

t/op/method.t

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ BEGIN {
1313
use strict;
1414
no warnings 'once';
1515

16-
plan(tests => 111);
16+
plan(tests => 116);
1717

1818
@A::ISA = 'B';
1919
@B::ISA = 'C';
@@ -477,3 +477,15 @@ package egakacp {
477477
@SUPER::ISA = "SUPPER";
478478
sub SUPPER::foo { "supper" }
479479
is "SUPER"->foo, 'supper', 'SUPER->method';
480+
481+
sub flomp { "flimp" }
482+
sub main::::flomp { "flump" }
483+
is "::"->flomp, 'flump', 'method call on ::';
484+
is "::main"->flomp, 'flimp', 'method call on ::main';
485+
eval { ""->flomp };
486+
like $@,
487+
qr/^Can't call method "flomp" without a package or object reference/,
488+
'method call on empty string';
489+
is "3foo"->CORE::uc, '3FOO', '"3foo"->CORE::uc';
490+
{ no strict; @{"3foo::ISA"} = "CORE"; }
491+
is "3foo"->uc, '3FOO', '"3foo"->uc (autobox style!)';

t/run/fresh_perl.t

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ $array[128]=1
8181
########
8282
$x=0x0eabcd; print $x->ref;
8383
EXPECT
84-
Can't call method "ref" without a package or object reference at - line 1.
84+
Can't locate object method "ref" via package "961485" (perhaps you forgot to load "961485"?) at - line 1.
8585
########
8686
chop ($str .= <DATA>);
8787
########

0 commit comments

Comments
 (0)