Skip to content

Commit 6109ba6

Browse files
committed
Undefined subroutine &%s called, close to label '%s'
#17839 requested a compile-time warning for the situation whereby a single colon is accdentally typed as a package separator when calling a function. For example: ``` package BOOP; sub beep; package main; BOOP:beep(); # Meant to be BOOP::beep(); ``` However, because of both Perl's syntax and the potential to populate the stash at runtime, this falls somewhere between very difficult and impossible. As an alternative, some enhanced fatal error wording was requested and this commit attempts to provide that. The above example would previously die with the message: ``` Undefined subroutine &main::beep called at ... line 4. ``` Now it dies with the message: ``` Undefined subroutine &main::beep called, close to label 'BOOP' at ... line 4. ``` For some of the same reasons mentioned, distinguishing this typo from other errors at runtime - such as the target subroutine not being present at all - is also nigh on impossible. The hope is that the error message will give some additional clue when the error is the result of a typo, without distracting the user in all other cases.
1 parent 82669cd commit 6109ba6

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

pod/perldelta.pod

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,18 @@ and L</New Warnings>
260260

261261
=item *
262262

263+
L<Undefined subroutine &%s called, close to label '%s'|perldiag/"Undefined subroutine &%s called, close to label '%s'">
264+
265+
(F) The subroutine indicated hasn't been defined, or if it was, it has
266+
since been undefined.
267+
268+
This error could also indicate a mistyped package separator, when a
269+
single colon was typed instead of two colons. For example, C<Foo:bar()>
270+
would be parsed as the label C<Foo> followed by an unqualified function
271+
name: C<foo: bar()>.
272+
273+
=item *
274+
263275
XXX L<message|perldiag/"message">
264276

265277
=back

pod/perldiag.pod

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6820,6 +6820,16 @@ Perhaps it's in a different package? See L<perlfunc/sort>.
68206820
(F) The subroutine indicated hasn't been defined, or if it was, it has
68216821
since been undefined.
68226822

6823+
=item Undefined subroutine &%s called, close to label '%s'
6824+
6825+
(F) The subroutine indicated hasn't been defined, or if it was, it has
6826+
since been undefined.
6827+
6828+
This error could also indicate a mistyped package separator, when a
6829+
single colon was typed instead of two colons. For example, C<Foo:bar()>
6830+
would be parsed as the label C<Foo> followed by an unqualified function
6831+
name: C<foo: bar()>.
6832+
68236833
=item Undefined subroutine called
68246834

68256835
(F) The anonymous subroutine you're trying to call hasn't been defined,

t/lib/croak/pp_hot

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@ Undefined subroutine &main::foo called at - line 3.
4545
&$foosub;
4646
EXPECT
4747
Undefined subroutine &main::foo called at - line 2.
48+
########
49+
# NAME package separator typo, creating a label by accident
50+
package BEEP;
51+
sub boop;
52+
package main;
53+
BEEP:boop();
54+
EXPECT
55+
Undefined subroutine &main::boop called, close to label 'BEEP' at - line 4.
56+
4857
########
4958
# NAME calling undef scalar
5059
&{+undef};

0 commit comments

Comments
 (0)