No, removing the sub
keyword is definitely not the solution. If you change this:
sub func($@) {
# ...
}
to this:
func($@) {
# ...
}
perlcritic stops complaining about the prototype -- but I think that's just a glitch in perlcritic. Without the sub
keyword, that's no longer a subroutine definition; it's a syntax error, as you'll see if you try to run it or check it with perl -cw
. It's not really perlcritic's job to check whether your code is valid Perl; it apparently assumes that it is, and then warns you about style issues. If you feed it invalid Perl, all bets are off.
The common wisdom these days is that using Perl prototypes is usually not a good idea.
Perlcritic is based on the book "Perl Best Practices", by Damian Conway. The section starting on page 194 is titled "Don’t use subroutine prototypes".
The book is not publicly available, so I can't quote or link to the section here, but chromatic has a blog entry "The Problem with Prototypes" that says, among other things:
The main problem with prototypes is that they behave differently than
most people expect when first encountering them. Prototypes can change
the parsing of subsequent code and they can coerce the types of
arguments. They don't serve as documentation to the number or types of
arguments subroutines expect, nor do they map arguments to named
parameters.
It's easy to assume that Perl's prototypes are similar to C's prototypes, which declare the number and type(s) (and optionally the names) of the arguments that a function expects. In fact, they're quite different. Their primary purpose is to write Perl subroutines that mimic the behavior of built-in functions, for example by not flattening arrays into lists.
See also perldoc perlsub
:
This is all very powerful, of course, and should be used only in
moderation to make the world a better place.