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 π
<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.
okay bye bestie β this was perl v5.42.0, dated 2024-10-23 πβ¨ slay and stay bug-free π¦
π¦ where this page comes from
- opensuse-leap 16.0: perl-Perl-Critic 1.156.0-bp160.1.1 Β· Artistic-1.0 OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms