Skip to content

Commit 065bd19

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.
1 parent 9f953fc commit 065bd19

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

lib/strict.pm

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package strict;
22

3-
$strict::VERSION = "1.13";
3+
$strict::VERSION = "1.14";
44

55
my ( %bitmask, %explicit_bitmask );
66

@@ -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,12 @@ BEGIN {
3841
}
3942

4043
sub bits {
44+
my $do_explicit = caller eq __PACKAGE__;
4145
my $bits = 0;
4246
my @wrong;
4347
foreach my $s (@_) {
4448
if (exists $bitmask{$s}) {
45-
$^H |= $explicit_bitmask{$s};
46-
49+
$bits |= $explicit_bitmask{$s} if $do_explicit;
4750
$bits |= $bitmask{$s};
4851
}
4952
else {
@@ -66,7 +69,9 @@ sub unimport {
6669
shift;
6770

6871
if (@_) {
69-
$^H &= ~&bits;
72+
my $bits = &bits;
73+
$^H &= ~$bits;
74+
$^H |= all_explicit_bits & $bits;
7075
}
7176
else {
7277
$^H &= ~all_bits;
@@ -75,6 +80,7 @@ sub unimport {
7580
}
7681

7782
1;
83+
7884
__END__
7985
8086
=head1 NAME

0 commit comments

Comments
 (0)