Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The use of <code>shift</code> to unpack arguments is not evil. It's a common convention and may be the fastest way to process arguments (depending on how many there are and how they're passed). Here's one example of a somewhat common scenario where that's the case: a simple accessor.</p> <pre><code>use Benchmark qw(cmpthese); sub Foo::x_shift { shift-&gt;{'a'} } sub Foo::x_ref { $_[0]-&gt;{'a'} } sub Foo::x_copy { my $s = $_[0]; $s-&gt;{'a'} } our $o = bless {a =&gt; 123}, 'Foo'; cmpthese(-2, { x_shift =&gt; sub { $o-&gt;x_shift }, x_ref =&gt; sub { $o-&gt;x_ref }, x_copy =&gt; sub { $o-&gt;x_copy }, }); </code></pre> <p>The results on perl 5.8.8 on my machine:</p> <pre><code> Rate x_copy x_ref x_shift x_copy 772761/s -- -12% -19% x_ref 877709/s 14% -- -8% x_shift 949792/s 23% 8% -- </code></pre> <p>Not dramatic, but there it is. Always test your scenario on your version of perl on your target hardware to find out for sure.</p> <p><code>shift</code> is also useful in cases where you want to shift off the invocant and then call a <code>SUPER::</code> method, passing the remaining <code>@_</code> as-is.</p> <pre><code>sub my_method { my $self = shift; ... return $self-&gt;SUPER::my_method(@_); } </code></pre> <p>If I had a very long series of <code>my $foo = shift;</code> operations at the top of a function, however, I might consider using a mass copy from <code>@_</code> instead. But in general, if you have a function or method that takes more than a handful of arguments, using named parameters (i.e., catching all of <code>@_</code> in a <code>%args</code> hash or expecting a single hash reference argument) is a much better approach.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload