Skip Menu |

Preferred bug tracker

Please visit the preferred bug tracker to report your issue.

This queue is for tickets about the Perl-Critic CPAN distribution.

Report information
The Basics
Id: 42790
Status: rejected
Priority: 0/
Queue: Perl-Critic

People
Owner: Nobody in particular
Requestors: hinrik.sig [...] gmail.com
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: (no value)



Subject: Policy suggestion: Require croak/die when getting wrong number of args
I don't know how to write Perl::Critic policies, so I'm submitting this suggestion here. Many methods and subroutines take key/value pairs as arguments. It feels natural, then, to write something like this: sub new { my ($package, %args) = @_; # check if some mandatory args are missing, etc return bless \%args, $package; } However, if the user of this code supplies an odd number of arguments to new(), it will only generate a warning at run-time. A better practice would be to croak() or die() when receiving an odd number of arguments, like so: sub new { my ($package) = shift; croak "$package requires an even number of arguments" if @_ & 1; my %args = @_; # check if some mandatory args are missing, etc return bless \%args, $package; } I would propose checking if there is a hash on the left-hand side when assigning from @_ in a subroutine where the number of elements in @_ has not been checked beforehand.
Show quoted text
> I would propose checking if there is a hash on the left-hand side when > assigning from @_ in a subroutine where the number of elements in @_ has > not been checked beforehand.
I think this particular idiom for checking subroutine args will be difficult for people to accept. However, argument validation is a good thing, and I'd rather steer people towards using Params::Validate or some other framework. So I'm going to reject this ticket, but add a TODO item to enforce use of Params::Validate. If you really want to enforce that idiom in your own code, then I encourage you to write your own policy. See http://search.cpan.org/~elliotjs/Perl-Critic-1.096/lib/Perl/Critic/DEVELOPER.pod for a how-to guide on writing policies. Cheers. -Jeff