Skip to content

Commit ed958fa

Browse files
JRaspassFather Chrysostomos
authored andcommitted
Optimise strict.pm for the common case
1 parent 3d6de2c commit ed958fa

File tree

2 files changed

+53
-20
lines changed

2 files changed

+53
-20
lines changed

lib/strict.pm

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,46 @@ unless ( __FILE__ =~ /(^|[\/\\])\Q${\__PACKAGE__}\E\.pmc?$/ ) {
99
die("Incorrect use of pragma '${\__PACKAGE__}' at $f line $l.\n");
1010
}
1111

12-
my %bitmask = (
13-
refs => 0x00000002,
14-
subs => 0x00000200,
15-
vars => 0x00000400
16-
);
17-
my %explicit_bitmask = (
18-
refs => 0x00000020,
19-
subs => 0x00000040,
20-
vars => 0x00000080
21-
);
12+
my ( %bitmask, %explicit_bitmask );
13+
14+
BEGIN {
15+
%bitmask = (
16+
refs => 0x00000002,
17+
subs => 0x00000200,
18+
vars => 0x00000400,
19+
);
20+
21+
%explicit_bitmask = (
22+
refs => 0x00000020,
23+
subs => 0x00000040,
24+
vars => 0x00000080,
25+
);
26+
27+
my $bits = 0;
28+
$bits |= $_ for values %bitmask;
29+
30+
my $inline_all_bits = $bits;
31+
*all_bits = sub () { $inline_all_bits };
32+
33+
$bits = 0;
34+
$bits |= $_ for values %explicit_bitmask;
35+
36+
my $inline_all_explicit_bits = $bits;
37+
*all_explicit_bits = sub () { $inline_all_explicit_bits };
38+
}
2239

2340
sub bits {
2441
my $bits = 0;
2542
my @wrong;
2643
foreach my $s (@_) {
27-
if (exists $bitmask{$s}) {
28-
$^H |= $explicit_bitmask{$s};
29-
}
30-
else { push @wrong, $s };
31-
$bits |= $bitmask{$s} || 0;
44+
if (exists $bitmask{$s}) {
45+
$^H |= $explicit_bitmask{$s};
46+
47+
$bits |= $bitmask{$s};
48+
}
49+
else {
50+
push @wrong, $s;
51+
}
3252
}
3353
if (@wrong) {
3454
require Carp;
@@ -37,16 +57,21 @@ sub bits {
3757
$bits;
3858
}
3959

40-
my @default_bits = qw(refs subs vars);
41-
4260
sub import {
4361
shift;
44-
$^H |= bits(@_ ? @_ : @default_bits);
62+
$^H |= @_ ? &bits : all_bits | all_explicit_bits;
4563
}
4664

4765
sub unimport {
4866
shift;
49-
$^H &= ~ bits(@_ ? @_ : @default_bits);
67+
68+
if (@_) {
69+
$^H &= ~&bits;
70+
}
71+
else {
72+
$^H &= ~all_bits;
73+
$^H |= all_explicit_bits;
74+
}
5075
}
5176

5277
1;

lib/strict.t

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
chdir 't' if -d 't';
44
@INC = '../lib';
55

6-
our $local_tests = 4;
6+
our $local_tests = 6;
77
require "../t/lib/common.pl";
88

99
eval qq(use strict 'garbage');
@@ -17,3 +17,11 @@ like($@, qr/^Unknown 'strict' tag\(s\) 'foo bar'/);
1717

1818
eval qq(no strict qw(foo bar));
1919
like($@, qr/^Unknown 'strict' tag\(s\) 'foo bar'/);
20+
21+
eval 'use v5.12; use v5.10; ${"c"}';
22+
is($@, '', 'use v5.10 disables implicit strict refs');
23+
24+
eval 'use strict; use v5.10; ${"c"}';
25+
like($@,
26+
qr/^Can't use string \("c"\) as a SCALAR ref while "strict refs" in use/,
27+
"use v5.10 doesn't disable explicit strict ref");

0 commit comments

Comments
 (0)