Skip to content

Compile-time syntax warning on lone colon without space #17839

Closed
@epa

Description

@epa

A double colon is a package separator. A single colon marks a label. If you mistype :: as : you get a rather mystifying error message:

MyPackage:foo(55)

Undefined subroutine OtherPackage::foo called

This with 5.26.3. The explanation is that MyPackage is being treated as a label, followed by a statement which calls foo(55) in the current package. But this is far from obvious. Could this "paper cut" in Perl's diagnostics be improved? Could it be reported at compile time?

Conventionally when programming Perl a label is followed by at least some white space (usually a newline character but sometimes a space). Conventionally the package separator :: is used without spaces either side. So how about a warning when a lone : appears and has a word character immediately to its left and immediately to its right? Those like me who like extra strictness can fatalize it with use warnings FATAL => 'syntax'.

The usual remarks about warnings apply. I think the mistake is easy enough to make, hard enough to work out (even for experienced programmers), and the false positive rate low enough that it falls on the right side of the line for a warning. If you are really concerned to minimize false positives, the warning could be given only when the word to the left of the token is itself a package name.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions