Last active
May 6, 2019 15:47
-
-
Save karronoli/1575332 to your computer and use it in GitHub Desktop.
perl compile test(roughly equivalent 'perl -c')
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Hoge; | |
our $a = 1; | |
1; | |
package main; | |
use strict; | |
use warnings; | |
use Test::More; | |
our $a = 1; | |
my $bad_code_string =<<'BAD' | |
use strict; | |
use warnings; | |
BEGIN { | |
print "bad begin Hello\n"; | |
$main::a = 2; | |
$Hoge::a = 2; | |
} | |
sub hoge { | |
pri n t 'Hello'; | |
} | |
n | |
1; | |
BAD | |
; | |
my $good_code_string =<<'GOOD' | |
print "Hello\n"; | |
GOOD | |
; | |
=pod check | |
influence caller namespace on callee BEGIN/CHECK blocks except %main::. | |
all included namespace influence perl internal. perl process stop... | |
my $namespace = join ', ', map {'%'.$_} (grep /::$/, keys %main::); | |
=cut | |
sub check { | |
my $code_str = shift @_ || 'print 1;'; | |
local (*STDOUT, *STDERR); | |
my ($stdout, $stderr) = ('', ''); | |
open *STDOUT, '>', \$stdout; | |
open *STDERR, '>', \$stderr; | |
# simple implement for expect %main:: | |
# local (%main::); | |
# my $ret = eval "sub {$code_str}"; | |
# expect included module. bad failed... | |
# my $namespace = join ', ', map {'%'.$_} (grep /::$/, keys %main::); | |
my $namespace = '%main::'; | |
my $code_ref = eval("local ($namespace);". | |
'(sub {my ($tmp) = @_; eval "sub {$tmp}"})->('. | |
"<<'CODE_STR_SEPARATER' | |
$code_str | |
CODE_STR_SEPARATER | |
);"); | |
my $ret = ($code_ref)? 1: 0; | |
unless ($ret) { | |
# require Data::Dumper; local $Data::Dumper::Deparse = 1; | |
diag(explain((map {"code: $_\n"} split /\n/, $code_str), | |
(map {"stdout: $_\n"} split /\n/, $stdout), | |
(map {"stderr: $_\n"} split /\n/, $stderr), | |
# (map {"deparse: $_\n"} split /\n/, Dumper $code_ref) | |
)); | |
} | |
$ret; | |
} | |
ok (!check($bad_code_string), 'bad code check'); | |
ok (check($good_code_string), 'good code check'); | |
ok ($main::a == 1 && $Hoge::a == 2, 'influence check except %main::') | |
or diag("\$main::a: $main::a, \$Hoge::a: $Hoge::a"); | |
done_testing; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
あとでコアモジュールのSafeを使ってみるかも