NAME
Perl::Critic::Policy::Subroutines::RequireArgUnpacking - Always unpack @_
first.
AFFILIATION
This Policy is part of the core Perl::Critic distribution.
DESCRIPTION
Subroutines that use @_
directly instead of unpacking the arguments to local variables first have two major problems. First, they are very hard to read. If you're going to refer to your variables by number instead of by name, you may as well be writing assembler code! Second, @_
contains aliases to the original variables! If you modify the contents of a @_
entry, then you are modifying the variable outside of your subroutine. For example:
sub print_local_var_plus_one {
my ($var) = @_;
print ++$var;
}
sub print_var_plus_one {
print ++$_[0];
}
my $x = 2;
print_local_var_plus_one($x); # prints "3", $x is still 2
print_var_plus_one($x); # prints "3", $x is now 3 !
print $x; # prints "3"
This is spooky action-at-a-distance and is very hard to debug if it's not intentional and well-documented (like chop
or chomp
).
CONFIGURATION
This policy is lenient for subroutines which have N
or fewer top-level statements, where N
defaults to ZERO. You can override this to set it to a higher number with the short_subroutine_statements
setting. This is very much not recommended but perhaps you REALLY need high performance. To do this, put entries in a .perlcriticrc file like this:
[Subroutines::RequireArgUnpacking]
short_subroutine_statements = 2
CAVEATS
PPI doesn't currently detect anonymous subroutines, so we don't check those. This should just work when PPI gains that feature.
We don't check for @ARG
, the alias for @_
from English.pm. That's deprecated anyway.
CREDITS
Initial development of this policy was supported by a grant from the Perl Foundation.
AUTHOR
Chris Dolan <cdolan@cpan.org>
COPYRIGHT
Copyright (c) 2007-2008 Chris Dolan. Many rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. The full text of this license can be found in the LICENSE file included with this module