This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Re: Réf. : Re: PATCH proposal for ext/Safe/safe2.t
[perl5.git] / ext / Safe / safe1.t
CommitLineData
76d62587
PP
1#!./perl -w
2$|=1;
3BEGIN {
4 chdir 't' if -d 't';
20822f61 5 @INC = '../lib';
76d62587
PP
6 require Config; import Config;
7 if ($Config{'extensions'} !~ /\bOpcode\b/ && $Config{'osname'} ne 'VMS') {
8 print "1..0\n";
9 exit 0;
10 }
11}
12
13# Tests Todo:
14# 'main' as root
15
16package test; # test from somewhere other than main
17
18use vars qw($bar);
19
20use Opcode 1.00, qw(opdesc opset opset_to_ops opset_to_hex
21 opmask_add full_opset empty_opset opcodes opmask define_optag);
22
23use Safe 1.00;
24
25my $last_test; # initalised at end
26print "1..$last_test\n";
27
28my $t = 1;
29my $cpt;
30# create and destroy some automatic Safe compartments first
31$cpt = new Safe or die;
32$cpt = new Safe or die;
33$cpt = new Safe or die;
34
35$cpt = new Safe "Root" or die;
36
37foreach(1..3) {
38 $foo = 42;
39
40 $cpt->share(qw($foo));
41
42 print ${$cpt->varglob('foo')} == 42 ? "ok $t\n" : "not ok $t\n"; $t++;
43
44 ${$cpt->varglob('foo')} = 9;
45
46 print $foo == 9 ? "ok $t\n" : "not ok $t\n"; $t++;
47
48 print $cpt->reval('$foo') == 9 ? "ok $t\n" : "not ok $t\n"; $t++;
49 # check 'main' has been changed:
50 print $cpt->reval('$::foo') == 9 ? "ok $t\n" : "not ok $t\n"; $t++;
51 print $cpt->reval('$main::foo') == 9 ? "ok $t\n" : "not ok $t\n"; $t++;
52 # check we can't see our test package:
53 print $cpt->reval('$test::foo') ? "not ok $t\n" : "ok $t\n"; $t++;
54 print $cpt->reval('${"test::foo"}') ? "not ok $t\n" : "ok $t\n"; $t++;
55
56 $cpt->erase; # erase the compartment, e.g., delete all variables
57
58 print $cpt->reval('$foo') ? "not ok $t\n" : "ok $t\n"; $t++;
59
60 # Note that we *must* use $cpt->varglob here because if we used
61 # $Root::foo etc we would still see the original values!
62 # This seems to be because the compiler has created an extra ref.
63
64 print ${$cpt->varglob('foo')} ? "not ok $t\n" : "ok $t\n"; $t++;
65}
66
67print "ok $last_test\n";
68BEGIN { $last_test = 28 }