1

I'm getting this error when I run Perlcritic:

Subroutine prototypes used at line xx, column x. See page 194 of PBP. (Severity: 5)

The subroutine is:

sub zFormatDate() {
  my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = shift;
  return sprintf("%04d%02d%02d%02d%02d%02d",
    $year + 1900, $mon+1, $mday, $hour, $min, $sec);
}

If I remove the keyword 'sub' from my function it disappears.

Is this OK, or should I be looking at a different solution?

3
  • 10
    Context, please. Include some code so that we can see what's going on.
    – user554546
    Dec 20, 2011 at 9:42
  • 3
    You might want to start reading a Perl tutorial or introductory book first. Dec 20, 2011 at 11:25
  • Note, that this warning is also raised by perlcritic when it encouters the (relative to En-Motion's post) new feature of 'signatures'. With perl 5.36 signatures are "productive" perl (not experimental anymore). I guess the perlcritic message should nowadays be regarded as "false positive", see phoenixtrap.com/2021/01/27/…
    – Sarge1060
    Sep 20, 2022 at 18:43

6 Answers 6

20

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.

18

As the message suggests, you have used subroutine prototypes. You most probably do not need them.

Currently your subroutine definition may be similar to:

sub foo()

Change it to:

sub foo

Note the removal of ( and ) and anything in between.

10

Perl::Critic considers subroutine prototypes to be bad. It's not about the "sub" keyword but the function arguments definition. Removing "sub" will trick Perl::Critic into not reporting the error but your code won't run anyway

You probably want to use subroutine prototypes in one of the two scenarios below:

  1. you want to perform some "magic" with your function paramaters, for example:

    sub fun1(\@) { my ($ar_ref) = @_ }

so a call like

fun1(@args)  

won't slurp the @args into @_ but will pass @args as an array ref into @_[0]

  1. you want to clearly state the function signature

    sub fun2($$\%) { }

fun2 args are a scalar, another scalar and a hash ref

Perl Best Practice book gives usage scenarios for both cases in wich you are getting screwed by your own code pretty easy. The fix given by PBP is: Don't use prototypes.

If you still want to use them you can tell Perl::Critic not to report the usage of prototypes:

## no critic
# Perl::Critic will ignore any problems it sees with your code
sub func_With_prototypes ($$$)
{
return undef
}
## use critic
# Perl::Critic will report any problems it sees within your code
3
  • This is the subroutine: sub zFormatDate() { my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = shift; return sprintf("%04d%02d%02d%02d%02d%02d", $year + 1900, $mon+1, $mday, $hour, $min, $sec); } and called using: $lSortDate = zFormatDate($lSortDate); there are other subroutines in the code which don't raise errors so I'll need to investigate further with this subroutine
    – En-Motion
    Dec 20, 2011 at 10:25
  • 1
    remove the (), they're misleading about the prototype of your function anyway. it should be sub zFormatDate { ... }
    – user237419
    Dec 20, 2011 at 10:47
  • sub zFormatDate () {...} is a prototype too, it says zFormatDate has zero arguments because of the "()" so removing the () will stop perlcritic complaining about it
    – user237419
    Dec 20, 2011 at 10:52
6

Newer Perls have a signatures feature, which while still flagged experimental, is pretty popular that is flagged by this rule. Signatures was introduced after this original post and may have been what En-Motion wanted.

Because Catalyst uses Prototypes extensively, Prototypes cannot be deprecated. Catalyst hacks the prototypes to provide a dynamic routing system.

If you use the signatures feature (or Catalyst) the best solution is to disable the rule via ~/.perlcriticrc.

Add the following to ~/.perlcriticrc

[-Subroutines::ProhibitSubroutinePrototypes]

While signatures look like prototypes to PerlCritic they are an essential element of Modern Perl Programming.

consider:

sub add2nums { my $num1 = shift; my $num2 = shift; return $num1 + $num2 ; }

with signature:

use feature 'signatures';

sub add2nums ( $num1, $num2 ) { return $num1 + $num2 ; }

1

I guess

sub zFormatDate() {
  my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = shift;
  return sprintf("%04d%02d%02d%02d%02d%02d",
    $year + 1900, $mon+1, $mday, $hour, $min, $sec);
}

was intended to read

sub zFormatDate($$$$$$$$$) {
  my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = @_;
  return sprintf("%04d%02d%02d%02d%02d%02d",
    $year + 1900, $mon+1, $mday, $hour, $min, $sec);
}

That is: Provide a prototype for 9 parameters, and set the my variables correctly.

1

On a more general note, I think the lecture of this article is quite illuminating: https://www.effectiveperlprogramming.com/2015/04/use-v5-20-subroutine-signatures/

tldr;

use v5.20; # or newer, currently it is v5.34
use feature qw(signatures);
no warnings qw(experimental::signatures);

sub my_func($param, $another_param, $an_optional_param=1) {
   say "$param is so $another_param"
       if $an_optional_param";
}

my_func('Chocolate', 'sweet');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.