Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does Perl::Critic dislike using shift to populate subroutine variables?
    primarykey
    data
    text
    <p>Lately, I've decided to start using <a href="http://search.cpan.org/perldoc?Perl::Critic" rel="nofollow noreferrer">Perl::Critic</a> more often on my code. After programming in Perl for close to 7 years now, I've been settled in with most of the Perl best practices for a long while, but I know that there is always room for improvement. One thing that has been bugging me though is the fact that <a href="http://search.cpan.org/perldoc?Perl::Critic" rel="nofollow noreferrer">Perl::Critic</a> doesn't like the way I unpack @_ for subroutines. As an example:</p> <pre><code>sub my_way_to_unpack { my $variable1 = shift @_; my $variable2 = shift @_; my $result = $variable1 + $variable2; return $result; } </code></pre> <p>This is how I've always done it, and, as its been discussed on both PerlMonks and Stack Overflow, its <a href="https://stackoverflow.com/questions/1328411/is-shift-evil-for-processing-perl-subroutine-parameters">not necessarily evil</a> either.</p> <p>Changing the code snippet above to...</p> <pre><code>sub perl_critics_way_to_unpack { my ($variable1, $variable2) = @_; my $result = $variable1 + $variable2; return $result; } </code></pre> <p>...works too, but I find it harder to read. I've also read Damian Conway's book <a href="http://oreilly.com/catalog/9780596001735" rel="nofollow noreferrer">Perl Best Practices</a> and I don't really understand how my preferred approach to unpacking falls under his suggestion to avoid using <code>@_</code> directly, as <a href="http://search.cpan.org/perldoc?Perl::Critic" rel="nofollow noreferrer">Perl::Critic</a> implies. I've always been under the impression that Conway was talking about nastiness such as:</p> <pre><code>sub not_unpacking { my $result = $_[0] + $_[1]; return $result; } </code></pre> <p>The above example is bad and hard to read, and I would never ever consider writing that in a piece of production code.</p> <p>So in short, why does <a href="http://search.cpan.org/perldoc?Perl::Critic" rel="nofollow noreferrer">Perl::Critic</a> consider my preferred way bad? Am I really committing a heinous crime unpacking by using shift?</p> <p>Would this be something that people other than myself think should be brought up with the <a href="http://search.cpan.org/perldoc?Perl::Critic" rel="nofollow noreferrer">Perl::Critic</a> maintainers?</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.
 

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