This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
935e574990d1556c5cc7cecb87fd1c65e9047ec9
[perl5.git] / t / op / magic.t
1 #!./perl
2
3 BEGIN {
4     $| = 1;
5     chdir 't' if -d 't';
6     @INC = '../lib';
7     $SIG{__WARN__} = sub { die "Dying on warning: ", @_ };
8 }
9
10 use warnings;
11
12 sub ok {
13     my ($n, $result, $info) = @_;
14     if ($result) {
15         print "ok $n\n";
16     }
17     else {
18         print "not ok $n\n";
19         print "# $info\n" if $info;
20     }
21 }
22
23 $Is_MSWin32 = $^O eq 'MSWin32';
24 $Is_NetWare = $^O eq 'NetWare';
25 $Is_VMS     = $^O eq 'VMS';
26 $Is_Dos   = $^O eq 'dos';
27 $Is_os2   = $^O eq 'os2';
28 $Is_Cygwin   = $^O eq 'cygwin';
29 $PERL = ($Is_MSWin32 ? '.\perl' : ($Is_NetWare ? 'perl' : './perl'));
30
31 print "1..41\n";
32
33 eval '$ENV{"FOO"} = "hi there";';       # check that ENV is inited inside eval
34 if ($Is_MSWin32) { ok 1, `cmd /x /c set FOO` eq "FOO=hi there\n"; }
35 else             { ok 1, `echo \$FOO` eq "hi there\n"; }
36
37 unlink 'ajslkdfpqjsjfk';
38 $! = 0;
39 open(FOO,'ajslkdfpqjsjfk');
40 ok 2, $!, $!;
41 close FOO; # just mention it, squelch used-only-once
42
43 if ($Is_MSWin32 || $Is_NetWare || $Is_Dos) {
44     ok "3 # skipped",1;
45     ok "4 # skipped",1;
46 }
47 else {
48   # the next tests are embedded inside system simply because sh spits out
49   # a newline onto stderr when a child process kills itself with SIGINT.
50   system './perl', '-e', <<'END';
51
52     $| = 1;             # command buffering
53
54     $SIG{"INT"} = "ok3";     kill "INT",$$; sleep 1;
55     $SIG{"INT"} = "IGNORE";  kill "INT",$$; sleep 1; print "ok 4\n";
56     $SIG{"INT"} = "DEFAULT"; kill "INT",$$; sleep 1; print "not ok\n";
57
58     sub ok3 {
59         if (($x = pop(@_)) eq "INT") {
60             print "ok 3\n";
61         }
62         else {
63             print "not ok 3 ($x @_)\n";
64         }
65     }
66
67 END
68 }
69
70 # can we slice ENV?
71 @val1 = @ENV{keys(%ENV)};
72 @val2 = values(%ENV);
73 ok 5, join(':',@val1) eq join(':',@val2);
74 ok 6, @val1 > 1;
75
76 # regex vars
77 'foobarbaz' =~ /b(a)r/;
78 ok 7, $` eq 'foo', $`;
79 ok 8, $& eq 'bar', $&;
80 ok 9, $' eq 'baz', $';
81 ok 10, $+ eq 'a', $+;
82
83 # $"
84 @a = qw(foo bar baz);
85 ok 11, "@a" eq "foo bar baz", "@a";
86 {
87     local $" = ',';
88     ok 12, "@a" eq "foo,bar,baz", "@a";
89 }
90
91 # $;
92 %h = ();
93 $h{'foo', 'bar'} = 1;
94 ok 13, (keys %h)[0] eq "foo\034bar", (keys %h)[0];
95 {
96     local $; = 'x';
97     %h = ();
98     $h{'foo', 'bar'} = 1;
99     ok 14, (keys %h)[0] eq 'fooxbar', (keys %h)[0];
100 }
101
102 # $?, $@, $$
103 system qq[$PERL -e "exit(0)"];
104 ok 15, $? == 0, $?;
105 system qq[$PERL -e "exit(1)"];
106 ok 16, $? != 0, $?;
107
108 eval { die "foo\n" };
109 ok 17, $@ eq "foo\n", $@;
110
111 ok 18, $$ > 0, $$;
112
113 # $^X and $0
114 {
115     if ($^O eq 'qnx') {
116         chomp($wd = `/usr/bin/fullpath -t`);
117     }
118     elsif($Is_Cygwin) {
119        # Cygwin turns the symlink into the real file
120        chomp($wd = `pwd`);
121        $wd =~ s#/t$##;
122     }
123     elsif($Is_os2) {
124        $wd = Cwd::sys_cwd();
125     }
126     else {
127         $wd = '.';
128     }
129     my $perl = "$wd/perl";
130     my $headmaybe = '';
131     my $tailmaybe = '';
132     $script = "$wd/show-shebang";
133     if ($Is_MSWin32) {
134         chomp($wd = `cd`);
135         $wd =~ s|\\|/|g;
136         $perl = "$wd/perl.exe";
137         $script = "$wd/show-shebang.bat";
138         $headmaybe = <<EOH ;
139 \@rem ='
140 \@echo off
141 $perl -x \%0
142 goto endofperl
143 \@rem ';
144 EOH
145         $tailmaybe = <<EOT ;
146
147 __END__
148 :endofperl
149 EOT
150     }
151     elsif ($Is_os2) {
152       $script = "./show-shebang";
153     }
154     if ($^O eq 'os390' or $^O eq 'posix-bc' or $^O eq 'vmesa') {  # no shebang
155         $headmaybe = <<EOH ;
156     eval 'exec ./perl -S \$0 \${1+"\$\@"}'
157         if 0;
158 EOH
159     }
160     $s1 = "\$^X is $perl, \$0 is $script\n";
161     ok 19, open(SCRIPT, ">$script"), $!;
162     ok 20, print(SCRIPT $headmaybe . <<EOB . <<'EOF' . $tailmaybe), $!;
163 #!$wd/perl
164 EOB
165 print "\$^X is $^X, \$0 is $0\n";
166 EOF
167     ok 21, close(SCRIPT), $!;
168     ok 22, chmod(0755, $script), $!;
169     $_ = `$script`;
170     s/\.exe//i if $Is_Dos or $Is_Cygwin or $Is_os2;
171     s{\bminiperl\b}{perl}; # so that test doesn't fail with miniperl
172     s{is perl}{is $perl}; # for systems where $^X is only a basename
173     s{\\}{/}g;
174     ok 23, (($Is_MSWin32 || $Is_os2) ? uc($_) eq uc($s1) : $_ eq $s1), " :$_:!=:$s1:";
175     $_ = `$perl $script`;
176     s/\.exe//i if $Is_Dos or $Is_os2;
177     s{\\}{/}g;
178     ok 24, (($Is_MSWin32 || $Is_os2) ? uc($_) eq uc($s1) : $_ eq $s1), " :$_:!=:$s1: after `$perl $script`";
179     ok 25, unlink($script), $!;
180 }
181
182 # $], $^O, $^T
183 ok 26, $] >= 5.00319, $];
184 ok 27, $^O;
185 ok 28, $^T > 850000000, $^T;
186
187 if ($Is_VMS || $Is_Dos) {
188         ok "29 # skipped", 1;
189         ok "30 # skipped", 1;
190 }
191 else {
192         $PATH = $ENV{PATH};
193         $PDL = $ENV{PERL_DESTRUCT_LEVEL} || 0;
194         $ENV{foo} = "bar";
195         %ENV = ();
196         $ENV{PATH} = $PATH;
197         $ENV{PERL_DESTRUCT_LEVEL} = $PDL || 0;
198         ok 29, ($Is_MSWin32 ? (`cmd /x /c set foo 2>NUL` eq "")
199                                 : (`echo \$foo` eq "\n") );
200
201         $ENV{__NoNeSuCh} = "foo";
202         $0 = "bar";
203         ok 30, ($Is_MSWin32 ? (`cmd /x /c set __NoNeSuCh` eq "__NoNeSuCh=foo\n")
204                             : (`echo \$__NoNeSuCh` eq "foo\n") );
205 }
206
207 {
208     local $SIG{'__WARN__'} = sub { print "# @_\nnot " };
209     $! = undef;
210     print "ok 31\n";
211 }
212
213 # test case-insignificance of %ENV (these tests must be enabled only
214 # when perl is compiled with -DENV_IS_CASELESS)
215 if ($Is_MSWin32 || $Is_NetWare) {
216     %ENV = ();
217     $ENV{'Foo'} = 'bar';
218     $ENV{'fOo'} = 'baz';
219     ok 32, (scalar(keys(%ENV)) == 1);
220     ok 33, exists($ENV{'FOo'});
221     ok 34, (delete($ENV{'foO'}) eq 'baz');
222     ok 35, (scalar(keys(%ENV)) == 0);
223 }
224 else {
225     ok "32 # skipped: no caseless %ENV support",1;
226     ok "33 # skipped: no caseless %ENV support",1;
227     ok "34 # skipped: no caseless %ENV support",1;
228     ok "35 # skipped: no caseless %ENV support",1;
229 }
230
231 # Make sure Errno hasn't been prematurely autoloaded
232
233 ok 36, !defined %Errno::;
234
235 # Test auto-loading of Errno when %! is used
236
237 ok 37, scalar eval q{
238    my $errs = %!;
239    defined %Errno::;
240 }, $@;
241
242
243 # Make sure that Errno loading doesn't clobber $!
244
245 undef %Errno::;
246 delete $INC{"Errno.pm"};
247
248 open(FOO, "nonesuch"); # Generate ENOENT
249 my %errs = %{"!"}; # Cause Errno.pm to be loaded at run-time
250 ok 38, ${"!"}{ENOENT};
251
252 ok 39, $^S == 0;
253 eval { ok 40, $^S == 1 };
254 ok 41, $^S == 0;