Skip to content

Commit 497a220

Browse files
committed
stop directly mutating $^H in strict::bits()
This improves the code in two ways: 1. Considering strict::bits is used by B::Deparse and vars.pm as unofficial API, the function having this side effect is arguably a latent bug even if it doesn’t break those callers. 2. It is harder to follow the intended logic when the function modifies $^H itself but also returns a value for the caller to apply to $^H. With this change, all branches also become entirely symmetrical.
1 parent 65e079e commit 497a220

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

lib/strict.pm

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package strict;
22

33
$strict::VERSION = "1.13";
44

5-
my ( %bitmask, %explicit_bitmask );
5+
my ( %bitmask, %explicit_bitmask, $explicit_bits );
66

77
BEGIN {
88
# Verify that we're called correctly so that strictures will work.
@@ -12,12 +12,15 @@ BEGIN {
1212
if __FILE__ !~ ( '(?x) \b '.__PACKAGE__.' \.pmc? \z' )
1313
&& __FILE__ =~ ( '(?x) \b (?i:'.__PACKAGE__.') \.pmc? \z' );
1414

15+
# which strictures are actually in force
1516
%bitmask = (
1617
refs => 0x00000002,
1718
subs => 0x00000200,
1819
vars => 0x00000400,
1920
);
2021

22+
# which strictures have at some point been turned on or off explicitly
23+
# and must therefore not be touched by any subsequent `use VERSION` or `no VERSION`
2124
%explicit_bitmask = (
2225
refs => 0x00000020,
2326
subs => 0x00000040,
@@ -38,12 +41,11 @@ BEGIN {
3841
}
3942

4043
sub bits {
41-
my $bits = 0;
44+
my $bits = $explicit_bits = 0;
4245
my @wrong;
4346
foreach my $s (@_) {
4447
if (exists $bitmask{$s}) {
45-
$^H |= $explicit_bitmask{$s};
46-
48+
$explicit_bits |= $explicit_bitmask{$s};
4749
$bits |= $bitmask{$s};
4850
}
4951
else {
@@ -59,14 +61,15 @@ sub bits {
5961

6062
sub import {
6163
shift;
62-
$^H |= @_ ? &bits : all_bits | all_explicit_bits;
64+
$^H |= @_ ? &bits | $explicit_bits : all_bits | all_explicit_bits;
6365
}
6466

6567
sub unimport {
6668
shift;
6769

6870
if (@_) {
6971
$^H &= ~&bits;
72+
$^H |= $explicit_bits;
7073
}
7174
else {
7275
$^H &= ~all_bits;

0 commit comments

Comments
 (0)