fixes for bugs in change#4586 and OS/2 pod tweak, from Ilya
[p5sagit/p5-mst-13.2.git] / t / op / magic.t
CommitLineData
8d063cd8 1#!./perl
2
90ce63d5 3BEGIN {
4 $^W = 1;
5 $| = 1;
6 chdir 't' if -d 't';
93430cb4 7 unshift @INC, '../lib';
774d564b 8 $SIG{__WARN__} = sub { die "Dying on warning: ", @_ };
90ce63d5 9}
8d063cd8 10
90ce63d5 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 22$Is_MSWin32 = $^O eq 'MSWin32';
23$Is_VMS = $^O eq 'VMS';
39e571d4 24$Is_Dos = $^O eq 'dos';
ed344e4f 25$Is_os2 = $^O eq 'os2';
1cab015a 26$Is_Cygwin = $^O =~ /cygwin/;
68dc0745 27$PERL = ($Is_MSWin32 ? '.\perl' : './perl');
28
78987ded 29print "1..35\n";
378cc40b 30
39e571d4 31eval '$ENV{"FOO"} = "hi there";'; # check that ENV is inited inside eval
32if ($Is_MSWin32) { ok 1, `cmd /x /c set FOO` eq "FOO=hi there\n"; }
33else { ok 1, `echo \$FOO` eq "hi there\n"; }
8d063cd8 34
bf38876a 35unlink 'ajslkdfpqjsjfk';
8d063cd8 36$! = 0;
90ce63d5 37open(FOO,'ajslkdfpqjsjfk');
38ok 2, $!, $!;
39close FOO; # just mention it, squelch used-only-once
8d063cd8 40
39e571d4 41if ($Is_MSWin32 || $Is_Dos) {
902173a3 42 ok "3 # skipped",1;
43 ok "4 # skipped",1;
68dc0745 44}
45else {
46 # the next tests are embedded inside system simply because sh spits out
47 # a newline onto stderr when a child process kills itself with SIGINT.
48 system './perl', '-e', <<'END';
378cc40b 49
79072805 50 $| = 1; # command buffering
378cc40b 51
b715f106 52 $SIG{"INT"} = "ok3"; kill "INT",$$; sleep 1;
53 $SIG{"INT"} = "IGNORE"; kill "INT",$$; sleep 1; print "ok 4\n";
54 $SIG{"INT"} = "DEFAULT"; kill "INT",$$; sleep 1; print "not ok\n";
79072805 55
56 sub ok3 {
57 if (($x = pop(@_)) eq "INT") {
58 print "ok 3\n";
59 }
60 else {
652ed9f8 61 print "not ok 3 ($x @_)\n";
79072805 62 }
63 }
64
65END
68dc0745 66}
a687059c 67
68dc0745 68# can we slice ENV?
69@val1 = @ENV{keys(%ENV)};
a687059c 70@val2 = values(%ENV);
90ce63d5 71ok 5, join(':',@val1) eq join(':',@val2);
72ok 6, @val1 > 1;
73
74# regex vars
75'foobarbaz' =~ /b(a)r/;
76ok 7, $` eq 'foo', $`;
77ok 8, $& eq 'bar', $&;
78ok 9, $' eq 'baz', $';
79ok 10, $+ eq 'a', $+;
80
81# $"
82@a = qw(foo bar baz);
83ok 11, "@a" eq "foo bar baz", "@a";
84{
85 local $" = ',';
86 ok 12, "@a" eq "foo,bar,baz", "@a";
87}
a687059c 88
90ce63d5 89# $;
90%h = ();
91$h{'foo', 'bar'} = 1;
92ok 13, (keys %h)[0] eq "foo\034bar", (keys %h)[0];
93{
94 local $; = 'x';
95 %h = ();
96 $h{'foo', 'bar'} = 1;
97 ok 14, (keys %h)[0] eq 'fooxbar', (keys %h)[0];
98}
ed6116ce 99
90ce63d5 100# $?, $@, $$
5aabfad6 101system qq[$PERL -e "exit(0)"];
90ce63d5 102ok 15, $? == 0, $?;
5aabfad6 103system qq[$PERL -e "exit(1)"];
90ce63d5 104ok 16, $? != 0, $?;
105
106eval { die "foo\n" };
107ok 17, $@ eq "foo\n", $@;
108
109ok 18, $$ > 0, $$;
110
111# $^X and $0
ed37317b 112{
3e3baf6d 113 if ($^O eq 'qnx') {
7fbf1995 114 chomp($wd = `/usr/bin/fullpath -t`);
68dc0745 115 }
1cab015a 116 elsif($Is_Cygwin) {
117 # Cygwin turns the symlink into the real file
118 chomp($wd = `pwd`);
119 $wd =~ s#/t$##;
120 }
ed344e4f 121 elsif($Is_os2) {
122 $wd = Cwd::sys_cwd();
123 }
68dc0745 124 else {
125 $wd = '.';
126 }
ed37317b 127 my $perl = "$wd/perl";
128 my $headmaybe = '';
129 my $tailmaybe = '';
68dc0745 130 $script = "$wd/show-shebang";
ed37317b 131 if ($Is_MSWin32) {
132 chomp($wd = `cd`);
8ac9c18d 133 $wd =~ s|\\|/|g;
134 $perl = "$wd/perl.exe";
135 $script = "$wd/show-shebang.bat";
ed37317b 136 $headmaybe = <<EOH ;
137\@rem ='
138\@echo off
139$perl -x \%0
140goto endofperl
141\@rem ';
142EOH
143 $tailmaybe = <<EOT ;
144
145__END__
146:endofperl
147EOT
148 }
ed344e4f 149 elsif ($Is_os2) {
150 $script = "./show-shebang";
151 }
a1a0e61e 152 if ($^O eq 'os390' or $^O eq 'posix-bc' or $^O eq 'vmesa') { # no shebang
9d116dd7 153 $headmaybe = <<EOH ;
154 eval 'exec ./perl -S \$0 \${1+"\$\@"}'
155 if 0;
156EOH
157 }
2eecd615 158 $s1 = "\$^X is $perl, \$0 is $script\n";
68dc0745 159 ok 19, open(SCRIPT, ">$script"), $!;
ed37317b 160 ok 20, print(SCRIPT $headmaybe . <<EOB . <<'EOF' . $tailmaybe), $!;
774d564b 161#!$wd/perl
162EOB
90ce63d5 163print "\$^X is $^X, \$0 is $0\n";
164EOF
68dc0745 165 ok 21, close(SCRIPT), $!;
166 ok 22, chmod(0755, $script), $!;
167 $_ = `$script`;
ed344e4f 168 s/\.exe//i if $Is_Dos or $Is_Cygwin or $Is_os2;
68dc0745 169 s{\bminiperl\b}{perl}; # so that test doesn't fail with miniperl
ed37317b 170 s{is perl}{is $perl}; # for systems where $^X is only a basename
a6c40364 171 s{\\}{/}g;
ed344e4f 172 ok 23, (($Is_MSWin32 || $Is_os2) ? uc($_) eq uc($s1) : $_ eq $s1), " :$_:!=:$s1:";
ed37317b 173 $_ = `$perl $script`;
ed344e4f 174 s/\.exe//i if $Is_Dos or $Is_os2;
a6c40364 175 s{\\}{/}g;
ed344e4f 176 ok 24, (($Is_MSWin32 || $Is_os2) ? uc($_) eq uc($s1) : $_ eq $s1), " :$_:!=:$s1: after `$perl $script`";
68dc0745 177 ok 25, unlink($script), $!;
178}
ed6116ce 179
90ce63d5 180# $], $^O, $^T
181ok 26, $] >= 5.00319, $];
182ok 27, $^O;
183ok 28, $^T > 850000000, $^T;
66b1d557 184
39e571d4 185if ($Is_VMS || $Is_Dos) {
902173a3 186 ok "29 # skipped", 1;
187 ok "30 # skipped", 1;
66b1d557 188}
189else {
3e3baf6d 190 $PATH = $ENV{PATH};
191 $ENV{foo} = "bar";
192 %ENV = ();
193 $ENV{PATH} = $PATH;
194 ok 29, ($Is_MSWin32 ? (`cmd /x /c set foo 2>NUL` eq "")
195 : (`echo \$foo` eq "\n") );
196
197 $ENV{NoNeSuCh} = "foo";
198 $0 = "bar";
199 ok 30, ($Is_MSWin32 ? (`cmd /x /c set NoNeSuCh` eq "NoNeSuCh=foo\n")
200 : (`echo \$NoNeSuCh` eq "foo\n") );
66b1d557 201}
3e3baf6d 202
78987ded 203{
2eecd615 204 local $SIG{'__WARN__'} = sub { print "# @_\nnot " };
78987ded 205 $! = undef;
206 print "ok 31\n";
207}
208
902173a3 209# test case-insignificance of %ENV (these tests must be enabled only
210# when perl is compiled with -DENV_IS_CASELESS)
211if ($Is_MSWin32) {
212 %ENV = ();
213 $ENV{'Foo'} = 'bar';
214 $ENV{'fOo'} = 'baz';
78987ded 215 ok 32, (scalar(keys(%ENV)) == 1);
216 ok 33, exists($ENV{'FOo'});
217 ok 34, (delete($ENV{'foO'}) eq 'baz');
218 ok 35, (scalar(keys(%ENV)) == 0);
902173a3 219}
220else {
ed344e4f 221 ok "32 # skipped: no caseless %ENV support",1;
222 ok "33 # skipped: no caseless %ENV support",1;
223 ok "34 # skipped: no caseless %ENV support",1;
224 ok "35 # skipped: no caseless %ENV support",1;
902173a3 225}