Is the following code safe? Does it run on all perl versions >= 5.8? Is it guaranteed that the shifts are executed before @_ is unpacked?
sub f1 {
shift->update(p1 => shift, p2 => shift, @_);
}
Verbose alternative code:
sub f2 {
my ($self, $p1, $p2, @r) = @_;
$self->update(p1 => $p1, p2 => $p2, @r);
}
It behaves correctly in all existing Perl interpreters.
However, the operand evaluation order of the comma operator is documented for when it's used in scalar context, but not for when it's used in list context as the case is here. Worse, it's undocumented behaviour in an area that's undefined behaviour in some other languages. I don't see the behaviour changing, but it's not perfectly safe either.
Compromise:
sub f3 {
my $self = shift;
my $p1 = shift;
my $p2 = shift;
$self->update(p1 => $p1, p2 => $p2, @_);
}
If you absolutely wanted to go variable-less, you could safely use the following:
sub f4 { $_[0]->update(p1 => $_[1], p2 => $_[2], @_[3..$#_]) }
I don't know why you'd want to use that, though. It's hard to read even without counting the loss of the self-documenting properties of naming the parameters.