Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify strict.pm #22848

Open
wants to merge 4 commits into
base: blead
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 19 additions & 29 deletions lib/strict.pm
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,24 @@ BEGIN {
if __FILE__ !~ ( '(?x) \b '.__PACKAGE__.' \.pmc? \z' )
&& __FILE__ =~ ( '(?x) \b (?i:'.__PACKAGE__.') \.pmc? \z' );

# which strictures are actually in force
%bitmask = (
refs => 0x00000002,
subs => 0x00000200,
vars => 0x00000400,
);

# which strictures have at some point been turned on or off explicitly
# and must therefore not be touched by any subsequent `use VERSION` or `no VERSION`
%explicit_bitmask = (
refs => 0x00000020,
subs => 0x00000040,
vars => 0x00000080,
);

# when a stricture is turned on explicitly, set both bits at once
$bitmask{$_} |= $explicit_bitmask{$_} for keys %bitmask;

my $bits = 0;
$bits |= $_ for values %bitmask;

Expand All @@ -37,41 +43,25 @@ BEGIN {
*all_explicit_bits = sub () { $inline_all_explicit_bits };
}

sub bits {
my $bits = 0;
my @wrong;
foreach my $s (@_) {
if (exists $bitmask{$s}) {
$^H |= $explicit_bitmask{$s};

$bits |= $bitmask{$s};
}
else {
push @wrong, $s;
}
}
if (@wrong) {
require Carp;
Carp::croak("Unknown 'strict' tag(s) '@wrong'");
}
$bits;
sub complain {
my @wrong = grep !$bitmask{$_}, @_;
die sprintf "Unknown 'strict' tag(s) '%s' at %s line %d.\n", "@wrong", +(caller)[1,2];
}

sub import {
shift;
$^H |= @_ ? &bits : all_bits | all_explicit_bits;
@_ <= 1 ? $^H |= all_bits : do {
shift;
for my $s (@_) { $^H |= ( $bitmask{$s} or goto &complain ) }
$^H;
};
}

sub unimport {
shift;

if (@_) {
$^H &= ~&bits;
}
else {
$^H &= ~all_bits;
$^H |= all_explicit_bits;
}
@_ <= 1 ? ( $^H &= ~all_bits ) |= all_explicit_bits : do {
shift;
for my $s (@_) { ( $^H &= ~( $bitmask{$s} or goto &complain ) ) |= $explicit_bitmask{$s} }
$^H;
};
}

1;
Expand Down
Loading