The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

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

AFFILIATION

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

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.

Problems with \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.

CONFIGURATION

This Policy is not configurable except for the standard options.

SEE ALSO

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

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

AUTHOR

Chris Lindee <chris.lindee@cpanel.net>

COPYRIGHT

Copyright (c) 2018 cPanel, L.L.C.

All rights reserved.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.