I'm frequently using shift
to unpack function parameters:
sub my_sub {
my $self = shift;
my $params = shift;
....
}
However, many on my colleagues are preaching that shift
is actually evil. Could you explain why I should prefer
sub my_sub {
my ($self, $params) = @_;
....
}
to shift
?
I don't think
shift
is evil. The use ofshift
shows your willingness to actually name variables - instead of using$_[0]
.Personally, I use
shift
when there's only one parameter to a function. If I have more than one parameter, I'll use the list context.There is an optimization for list assignment.
The only reference I could find, is this one.
This is an example of the affected performance regression.