This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Add xor tests to test suite
[perl5.git] / t / op / magic.t
CommitLineData
8d063cd8
LW
1#!./perl
2
90ce63d5
RS
3BEGIN {
4 $^W = 1;
5 $| = 1;
6 chdir 't' if -d 't';
7 @INC = '../lib';
774d564b 8 $SIG{__WARN__} = sub { die "Dying on warning: ", @_ };
90ce63d5 9}
8d063cd8 10
90ce63d5
RS
11sub ok {
12 my ($n, $result, $info) = @_;
13 if ($result) {
14 print "ok $n\n";
15 }
16 else {
17 print "not ok $n\n";
18 print "# $info\n" if $info;
19 }
20}
21
3e3baf6d
TB
22$Is_MSWin32 = $^O eq 'MSWin32';
23$Is_VMS = $^O eq 'VMS';
68dc0745
PP
24$PERL = ($Is_MSWin32 ? '.\perl' : './perl');
25
66b1d557 26print "1..30\n";
378cc40b
LW
27
28eval '$ENV{"foo"} = "hi there";'; # check that ENV is inited inside eval
3fe9a6f1 29if ($Is_MSWin32) { ok 1, `cmd /x /c set foo` eq "foo=hi there\n"; }
68dc0745 30else { ok 1, `echo \$foo` eq "hi there\n"; }
8d063cd8 31
bf38876a 32unlink 'ajslkdfpqjsjfk';
8d063cd8 33$! = 0;
90ce63d5
RS
34open(FOO,'ajslkdfpqjsjfk');
35ok 2, $!, $!;
36close FOO; # just mention it, squelch used-only-once
8d063cd8 37
68dc0745
PP
38if ($Is_MSWin32) {
39 ok 3,1;
40 ok 4,1;
41}
42else {
43 # the next tests are embedded inside system simply because sh spits out
44 # a newline onto stderr when a child process kills itself with SIGINT.
45 system './perl', '-e', <<'END';
378cc40b 46
79072805 47 $| = 1; # command buffering
378cc40b 48
79072805
LW
49 $SIG{"INT"} = "ok3"; kill "INT",$$;
50 $SIG{"INT"} = "IGNORE"; kill 2,$$; print "ok 4\n";
51 $SIG{"INT"} = "DEFAULT"; kill 2,$$; print "not ok\n";
52
53 sub ok3 {
54 if (($x = pop(@_)) eq "INT") {
55 print "ok 3\n";
56 }
57 else {
652ed9f8 58 print "not ok 3 ($x @_)\n";
79072805
LW
59 }
60 }
61
62END
68dc0745 63}
a687059c 64
68dc0745
PP
65# can we slice ENV?
66@val1 = @ENV{keys(%ENV)};
a687059c 67@val2 = values(%ENV);
90ce63d5
RS
68ok 5, join(':',@val1) eq join(':',@val2);
69ok 6, @val1 > 1;
70
71# regex vars
72'foobarbaz' =~ /b(a)r/;
73ok 7, $` eq 'foo', $`;
74ok 8, $& eq 'bar', $&;
75ok 9, $' eq 'baz', $';
76ok 10, $+ eq 'a', $+;
77
78# $"
79@a = qw(foo bar baz);
80ok 11, "@a" eq "foo bar baz", "@a";
81{
82 local $" = ',';
83 ok 12, "@a" eq "foo,bar,baz", "@a";
84}
a687059c 85
90ce63d5
RS
86# $;
87%h = ();
88$h{'foo', 'bar'} = 1;
89ok 13, (keys %h)[0] eq "foo\034bar", (keys %h)[0];
90{
91 local $; = 'x';
92 %h = ();
93 $h{'foo', 'bar'} = 1;
94 ok 14, (keys %h)[0] eq 'fooxbar', (keys %h)[0];
95}
ed6116ce 96
90ce63d5 97# $?, $@, $$
5aabfad6 98system qq[$PERL -e "exit(0)"];
90ce63d5 99ok 15, $? == 0, $?;
5aabfad6 100system qq[$PERL -e "exit(1)"];
90ce63d5
RS
101ok 16, $? != 0, $?;
102
103eval { die "foo\n" };
104ok 17, $@ eq "foo\n", $@;
105
106ok 18, $$ > 0, $$;
107
108# $^X and $0
68dc0745
PP
109if ($Is_MSWin32) {
110 for (19 .. 25) { ok $_, 1 }
774d564b
PP
111}
112else {
3e3baf6d 113 if ($^O eq 'qnx') {
68dc0745
PP
114 chomp($wd = `pwd`);
115 }
116 else {
117 $wd = '.';
118 }
119 $script = "$wd/show-shebang";
120 $s1 = $s2 = "\$^X is $wd/perl, \$0 is $script\n";
121 if ($^O eq 'os2') {
122 # Started by ksh, which adds suffixes '.exe' and '.' to perl and script
123 $s2 = "\$^X is $wd/perl.exe, \$0 is $script.\n";
124 }
125 ok 19, open(SCRIPT, ">$script"), $!;
126 ok 20, print(SCRIPT <<EOB . <<'EOF'), $!;
774d564b
PP
127#!$wd/perl
128EOB
90ce63d5
RS
129print "\$^X is $^X, \$0 is $0\n";
130EOF
68dc0745
PP
131 ok 21, close(SCRIPT), $!;
132 ok 22, chmod(0755, $script), $!;
133 $_ = `$script`;
134 s{\bminiperl\b}{perl}; # so that test doesn't fail with miniperl
135 s{is perl}{is $wd/perl}; # for systems where $^X is only a basename
136 ok 23, $_ eq $s2, ":$_:!=:$s2:";
137 $_ = `$wd/perl $script`;
138 ok 24, $_ eq $s1, ":$_:!=:$s1: after `$wd/perl $script`";
139 ok 25, unlink($script), $!;
140}
ed6116ce 141
90ce63d5
RS
142# $], $^O, $^T
143ok 26, $] >= 5.00319, $];
144ok 27, $^O;
145ok 28, $^T > 850000000, $^T;
66b1d557 146
3e3baf6d
TB
147if ($Is_VMS) {
148 ok 29, 1;
149 ok 30, 1;
66b1d557
HM
150}
151else {
3e3baf6d
TB
152 $PATH = $ENV{PATH};
153 $ENV{foo} = "bar";
154 %ENV = ();
155 $ENV{PATH} = $PATH;
156 ok 29, ($Is_MSWin32 ? (`cmd /x /c set foo 2>NUL` eq "")
157 : (`echo \$foo` eq "\n") );
158
159 $ENV{NoNeSuCh} = "foo";
160 $0 = "bar";
161 ok 30, ($Is_MSWin32 ? (`cmd /x /c set NoNeSuCh` eq "NoNeSuCh=foo\n")
162 : (`echo \$NoNeSuCh` eq "foo\n") );
66b1d557 163}
3e3baf6d 164