Subject: | Wishlist: Make strict_ok actually check for strictness rather than regex |
The use strict test works by searching the Perl code for "use strict".
This is seriously limited, shown by the additional searching for "use
Moose" and "use Mouse". I ran into this because I have adopted "use
Modern::Perl" for my code, this also sets strict for me.
I suggest that the current approach of parsing the perl code is flawed.
It works for the most common case. It doesn't catch if strict is only
partially enabled or later disabled. What's worse it doesn't always
catch if it's been commented out "use warnings; # use strict;"
As an alternative approach may I suggest probing the $^H variable?
This is messy due to scope issues but I believe is possible. A one line
proof of concept is: echo 'BEGIN {say $^H & 0x602}' | cat TESTME - | perl
A more complete example follows. I have tried to make it cross
platform. It still has some rough edges, if you are interested I will
probably have them fixed in a few days.
use Test::Builder;
use autodie ':all';
use File::Temp;
sub real_strict_ok {
my ($filename, $msg) = @_;
$msg //= "Strict really set for $filename";
my $Test = Test::Builder->new; # Singleton
my $tmp = File::Temp->new();
# TODO: Handle multiple packages
{
open (my $in_fh, "<", $filename);
local ($/);
my $text = <$in_fh>;
print $tmp $text, ";"; # Add trailing ; in case they left it off
# 0x602 is strict refs, subs, vars. Obtained from strict.pm.
say $tmp 'BEGIN {print(($^H & 0x602) == 0x602, "\n")}';
}
# TODO: Capture STDERR
my $result = [split /\n/, `$^X -c $tmp`]->[-1];
$Test->ok($result, $msg);
}