logo
Free, unlimited AI code reviews that run on commit
git-lrc git-lrc GitHub Install Now We'd appreciate a star git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt

Perl::Critic::Policy::BuiltinFunctions::ProhibitShiftRef - Prohibit "\shift" in code

Affiliation

       This Policy is part of the core Perl::Critic distribution.

Author

       Chris Lindee <chris.lindee@cpanel.net>

Configuration

       This Policy is not configurable except for the standard options.

Description

       Prohibit the use of "\shift", as it is associated with bugs in Perl and its modules.

   Background
       Often, "\shift" is used to create references that act much like an alias.  By creating an "alias" that is
       named, the code becomes more readable.  For example,

           sub routine {
               my $longstring = \shift;
               print $$longstring;
           }

       is more readable than

           sub routine {
               print $_[0];    # longstring
           }

       Unfortunately, this added readability brings with it new and exciting issues, detailed in the next
       section.

   Problemswith"\shift"
       By avoiding "\shift", several issues in Perl can be averted, including:

       Memory leak since Perl 5.22
           Issue  #126676 was introduced in Perl 5.21.4 and is triggered when "\shift" is used.  The bug has not
           been resolved as of Perl 5.28.

           In short, the bug causes the ref counter for the aliased variable to be incremented when running  the
           subroutine,  but  it  is  not  subsequently decremented after the subroutine returns.  In addition to
           leaking memory, this issue can also delay the cleanup of objects until Global Destruction, which  can
           cause further issues.

           For more information, see <https://rt.perl.org/Public/Bug/Display.html?id=126676>.

       Devel::Cover crashes
           A  separate,  longstanding  issue in Devel::Cover (since at least 1.21), causes test code to segfault
           occasionally.  This prevents the coverage data from being written out, resulting in bad metrics.

           The bug itself isn't actually caused by "\shift", instead it shows up in code like the following:

               sub myopen {
                   open ${ \$_[0] }, ">test";
               }

           However, this code would rarely be seen in production.  It would more likely manifest with  "\shift",
           as it does below:

               sub myopen {
                   my $fh = \shift;
                   open $$fh, ">test";
               }

           So while "\shift" isn't the cause, it's often associated with the problem.

           For more information, see <https://github.com/pjcj/Devel--Cover/issues/125>.

Name

       Perl::Critic::Policy::BuiltinFunctions::ProhibitShiftRef - Prohibit "\shift" in code

See Also

       <https://rt.perl.org/Public/Bug/Display.html?id=126676>

       <https://github.com/pjcj/Devel--Cover/issues/125>

See Also