note
sgifford
As others have said, mostly this is a matter of personal style. Sometimes it can make a difference, though, since <code>shift</code> actually modifies <code>@_</code>.
<p>For example, this code could allow a function to act as a regular sub, or as a method (assuming the first argument will never be a reference unless it's called as a method):
<code>
my $self = ref $_[0] ? shift : undef;
my($arg1,$arg2,$arg3)=@_;
</code>
<p>Similarly, if you have a sub that takes a list as its final argument, shifting off the non-list arguments can make it clearer what you're doing, especially if you pass the list on to any other subs:
<code>
sub my_sort
{
my $sortname = shift;
return sort $SORTS{$sortname} @_;
}
</code>
<p>This can also be useful if you decide to [perldoc://goto] another sub, since that sub will see the modified <code>@_</code> as its argument list.
<p>Finally, as a matter of style, I usually get <code>$self</code> or <code>$class</code> with <code>shift</code> in methods and constructors, since it makes it clearer what the explicit arguments are, as compared to the implicit object reference or class name passed in as the first argument.
<p><b>Update: </b> Corrections and clarifications from [jimt].
<!-- Node text goes above. Div tags should contain sig only -->
<div class="pmsig"><div class="pmsig-190016">
<br>
--<br>
<i>[http://www.suspectclass.com/~sgifford/|sgifford's Web page]</i>
</div></div>
575918
575918