Perl::...iftRef(3pm)

πŸŽ€ NAME πŸŽ€

okay bestie, welcome to the Perl::...iftRef(3pm) page β€” it’s User Contributed Perl Documentation, and the policy it’s all about is Perl::Critic::Policy::BuiltinFunctions::ProhibitShiftRef πŸ’…

Its whole main-character job: Prohibit \shift in code.


✨ AFFILIATION ✨

This Policy is part of the core Perl::Critic distribution. iconic, no cap 🫢


πŸ’– DESCRIPTION πŸ’–

So the policy says: don’t use \shift, because it is associated with bugs in Perl and its modules. fr fr, that’s the vibe check. 😭

🌸 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. and yes, β€œexciting” is doing a lot of work there πŸ’€

πŸ’€ 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. ngl, that’s rough 😭

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. highkey not the vibe.

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. not the coverage data getting eaten 😭

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. iykyk πŸŒ€

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


πŸŽ›οΈ CONFIGURATION πŸŽ›οΈ

This Policy is not configurable except for the standard options. no extra knobs, bestie β€” just the standard options, periodt ✨


πŸ”— SEE ALSO πŸ”—


πŸ‘‘ AUTHOR πŸ‘‘

Chris Lindee <chris.lindee@cpanel.net>


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.


okay bye bestie β€” this was perl v5.42.0, dated 2024-10-23 πŸ’–βœ¨ slay and stay bug-free πŸ¦‹

πŸ“¦ where this page comes from