Skip to content

Commit

Permalink
error messages - do not repeat class names twice in our error messages
Browse files Browse the repository at this point in the history
Showing the classname twice in the error message seems friendly when the
class name is short, but when it is long enough that the line wraps the
duplication just increases cognitive load understand the error message.
This is especially the case when the value is a total error and contains
gibberish or a long binary string or such things, something that is all
to common with this type of error. Even with the recently merged eliding
of names the duplication means that an error message can be 2k long in
the worst case, mostly because of the unnecessary duplication.

As a compromise we can simply replace the second invocation of the class
name by saying "it" instead, and that is what this patch does. The error
message is still friendly, but not repetitive. We could also use "the
package" if people preferred.

Thus:
$ perl -le'("x" x 50)->new()'
Can't locate object method "new" via package "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
 (perhaps you forgot to load "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"?) at -e line 1.

Turns into:
$ ./perl -le'("x" x 50)->new()'
Can't locate object method "new" via package "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
 (perhaps you forgot to load it?) at -e line 1.
  • Loading branch information
demerphq committed Aug 27, 2022
1 parent 39629dc commit e693667
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 14 deletions.
9 changes: 4 additions & 5 deletions gv.c
Original file line number Diff line number Diff line change
Expand Up @@ -917,12 +917,11 @@ S_gv_fetchmeth_internal(pTHX_ HV* stash, SV* meth, const char* name, STRLEN len,
Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
"While trying to resolve method call %.*s->%.*s()"
" can not locate package %" SVf_QUOTEDPREFIX " yet it is mentioned in @%.*s::ISA"
" (perhaps you forgot to load %" SVf_QUOTEDPREFIX "?)",
" (perhaps you forgot to load it?)",
(int) hvnamelen, hvname,
(int) len, name,
SVfARG(linear_sv),
(int) hvnamelen, hvname,
SVfARG(linear_sv));
(int) hvnamelen, hvname);
}
}
continue;
Expand Down Expand Up @@ -1263,9 +1262,9 @@ Perl_gv_fetchmethod_pvn_flags(pTHX_ HV *stash, const char *name, const STRLEN le
Perl_croak(aTHX_
"Can't locate object method %" UTF8f_QUOTEDPREFIX ""
" via package %" SVf_QUOTEDPREFIX ""
" (perhaps you forgot to load %" SVf_QUOTEDPREFIX "?)",
" (perhaps you forgot to load it?)",
UTF8fARG(is_utf8, name_end - name, name),
SVfARG(packnamesv), SVfARG(packnamesv));
SVfARG(packnamesv));
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions pod/perldiag.pod
Original file line number Diff line number Diff line change
Expand Up @@ -1169,8 +1169,7 @@ unable to locate this library. See L<DynaLoader>.
functioning as a class, but that package doesn't define that particular
method, nor does any of its base classes. See L<perlobj>.

=item Can't locate object method "%s" via package "%s" (perhaps you forgot
to load "%s"?)
=item Can't locate object method "%s" via package "%s" (perhaps you forgot to load it?)

(F) You called a method on a class that did not exist, and the method
could not be found in UNIVERSAL. This often means that a method
Expand Down Expand Up @@ -7820,7 +7819,7 @@ can be determined from the template alone. This is not possible if
it contains any of the codes @, /, U, u, w or a *-length. Redesign
the template.

=item While trying to resolve method call %s->%s() can not locate package "%s" yet it is mentioned in @%s::ISA (perhaps you forgot to load "%s"?)
=item While trying to resolve method call %s->%s() can not locate package "%s" yet it is mentioned in @%s::ISA (perhaps you forgot to load it?)

(W syntax) It is possible that the C<@ISA> contains a misspelled or never loaded
package name, which can result in perl choosing an unexpected parent
Expand Down
4 changes: 2 additions & 2 deletions pp_sys.c
Original file line number Diff line number Diff line change
Expand Up @@ -951,8 +951,8 @@ PP(pp_tie)
: newSVpvs_flags("main", SVs_TEMP);
DIE(aTHX_ "Can't locate object method %" PVf_QUOTEDPREFIX
" via package %" SVf_QUOTEDPREFIX
" (perhaps you forgot to load %" SVf_QUOTEDPREFIX "?)",
methname, SVfARG(stashname), SVfARG(stashname));
" (perhaps you forgot to load it?)",
methname, SVfARG(stashname));
}
}
else if (!(gv = gv_fetchmethod(stash, methname))) {
Expand Down
4 changes: 2 additions & 2 deletions t/lib/warnings/gv
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Undefined subroutine &main::joe called at - line 3.
use warnings 'syntax' ;
@ISA = qw(Fred); __PACKAGE__->joe()
EXPECT
While trying to resolve method call main->joe() can not locate package "Fred" yet it is mentioned in @main::ISA (perhaps you forgot to load "Fred"?) at - line 3.
While trying to resolve method call main->joe() can not locate package "Fred" yet it is mentioned in @main::ISA (perhaps you forgot to load it?) at - line 3.
Can't locate object method "joe" via package "main" at - line 3.
########
# gv.c
Expand Down Expand Up @@ -57,7 +57,7 @@ $a = bless [], 'C';
$a->foo();
__END__
EXPECT
While trying to resolve method call C->foo() can not locate package "A" yet it is mentioned in @C::ISA (perhaps you forgot to load "A"?) at - line 18.
While trying to resolve method call C->foo() can not locate package "A" yet it is mentioned in @C::ISA (perhaps you forgot to load it?) at - line 18.
I'm in B's foo
########
# gv.c
Expand Down
2 changes: 1 addition & 1 deletion t/op/tie.t
Original file line number Diff line number Diff line change
Expand Up @@ -930,7 +930,7 @@ sub IO::File::TIEARRAY {
}
fileno FOO; tie @a, "FOO"
EXPECT
Can't locate object method "TIEARRAY" via package "FOO" (perhaps you forgot to load "FOO"?) at - line 5.
Can't locate object method "TIEARRAY" via package "FOO" (perhaps you forgot to load it?) at - line 5.
########
# tie into empty package name
tie $foo, "";
Expand Down
2 changes: 1 addition & 1 deletion t/run/fresh_perl.t
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ $array[128]=1
########
$x=0x0eabcd; print $x->ref;
EXPECT
Can't locate object method "ref" via package "961485" (perhaps you forgot to load "961485"?) at - line 1.
Can't locate object method "ref" via package "961485" (perhaps you forgot to load it?) at - line 1.
########
chop ($str .= <DATA>);
########
Expand Down

0 comments on commit e693667

Please sign in to comment.