This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Add delay to signal handling in t/op/magic.t. (Solaris with pthreads
[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
b715f106
MB
49 $SIG{"INT"} = "ok3"; kill "INT",$$; sleep 1;
50 $SIG{"INT"} = "IGNORE"; kill "INT",$$; sleep 1; print "ok 4\n";
51 $SIG{"INT"} = "DEFAULT"; kill "INT",$$; sleep 1; print "not ok\n";
79072805
LW
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
ed37317b 109{
3e3baf6d 110 if ($^O eq 'qnx') {
68dc0745
PP
111 chomp($wd = `pwd`);
112 }
113 else {
114 $wd = '.';
115 }
ed37317b
TB
116 my $perl = "$wd/perl";
117 my $headmaybe = '';
118 my $tailmaybe = '';
68dc0745 119 $script = "$wd/show-shebang";
ed37317b
TB
120 if ($Is_MSWin32) {
121 chomp($wd = `cd`);
122 $perl = "$wd\\perl.exe";
123 $script = "$wd\\show-shebang.bat";
124 $headmaybe = <<EOH ;
125\@rem ='
126\@echo off
127$perl -x \%0
128goto endofperl
129\@rem ';
130EOH
131 $tailmaybe = <<EOT ;
132
133__END__
134:endofperl
135EOT
136 }
137 $s1 = $s2 = "\$^X is $perl, \$0 is $script\n";
68dc0745
PP
138 if ($^O eq 'os2') {
139 # Started by ksh, which adds suffixes '.exe' and '.' to perl and script
140 $s2 = "\$^X is $wd/perl.exe, \$0 is $script.\n";
141 }
142 ok 19, open(SCRIPT, ">$script"), $!;
ed37317b 143 ok 20, print(SCRIPT $headmaybe . <<EOB . <<'EOF' . $tailmaybe), $!;
774d564b
PP
144#!$wd/perl
145EOB
90ce63d5
RS
146print "\$^X is $^X, \$0 is $0\n";
147EOF
68dc0745
PP
148 ok 21, close(SCRIPT), $!;
149 ok 22, chmod(0755, $script), $!;
150 $_ = `$script`;
151 s{\bminiperl\b}{perl}; # so that test doesn't fail with miniperl
ed37317b
TB
152 s{is perl}{is $perl}; # for systems where $^X is only a basename
153 ok 23, ($Is_MSWin32 ? uc($_) eq uc($s2) : $_ eq $s2), ":$_:!=:$s2:";
154 $_ = `$perl $script`;
155 ok 24, ($Is_MSWin32 ? uc($_) eq uc($s1) : $_ eq $s1), ":$_:!=:$s1: after `$perl $script`";
68dc0745
PP
156 ok 25, unlink($script), $!;
157}
ed6116ce 158
90ce63d5
RS
159# $], $^O, $^T
160ok 26, $] >= 5.00319, $];
161ok 27, $^O;
162ok 28, $^T > 850000000, $^T;
66b1d557 163
3e3baf6d
TB
164if ($Is_VMS) {
165 ok 29, 1;
166 ok 30, 1;
66b1d557
HM
167}
168else {
3e3baf6d
TB
169 $PATH = $ENV{PATH};
170 $ENV{foo} = "bar";
171 %ENV = ();
172 $ENV{PATH} = $PATH;
173 ok 29, ($Is_MSWin32 ? (`cmd /x /c set foo 2>NUL` eq "")
174 : (`echo \$foo` eq "\n") );
175
176 $ENV{NoNeSuCh} = "foo";
177 $0 = "bar";
178 ok 30, ($Is_MSWin32 ? (`cmd /x /c set NoNeSuCh` eq "NoNeSuCh=foo\n")
179 : (`echo \$NoNeSuCh` eq "foo\n") );
66b1d557 180}
3e3baf6d 181