Can shift and @_ be used in the same statement?
Is the following code safe? Does it work in all versions of perl> = 5.8? Is it guaranteed that the shifts are done before unpacking @_?
sub f1 {
shift->update(p1 => shift, p2 => shift, @_);
}
Detailed alternative code:
sub f2 {
my ($self, $p1, $p2, @r) = @_;
$self->update(p1 => $p1, p2 => $p2, @r);
}
source to share
It works correctly in all existing Perl interpreters.
However, the order of evaluation of the operand for the comma operator is documented when it is used in a scalar context, but not when it is used in the context of a list in this case. Worse, this is undocumented behavior in the undefined behavior area in some other languages. I don't see a change in behavior, but it is not entirely safe.
Compromise:
sub f3 {
my $self = shift;
my $p1 = shift;
my $p2 = shift;
$self->update(p1 => $p1, p2 => $p2, @_);
}
If you absolutely want to go without a variable, you can use the following with confidence:
sub f4 { $_[0]->update(p1 => $_[1], p2 => $_[2], @_[3..$#_]) }
I don't know why you want to use this. Difficult to read even without considering the loss of self-documenting parameter naming properties.
source to share