This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Integrate perlio:
[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 use Config;
12
13 my $test = 1;
14 sub ok {
15     my($ok, $info, $todo) = @_;
16
17     # You have to do it this way or VMS will get confused.
18     printf "%s $test%s\n", $ok ? "ok" : "not ok",
19                            $todo ? " # TODO $todo" : '';
20
21     unless( $ok ) {
22         printf "# Failed test at line %d\n", (caller)[2];
23         print  "# $info" if defined $info;
24     }
25
26     $test++;
27     return $ok;
28 }
29
30 sub skip {
31     my($reason) = @_;
32
33     printf "ok $test # skipped%s\n", defined $reason ? ": $reason" : '';
34
35     $test++;
36     return 1;
37 }
38
39 print "1..46\n";
40
41 $Is_MSWin32 = $^O eq 'MSWin32';
42 $Is_NetWare = $^O eq 'NetWare';
43 $Is_VMS     = $^O eq 'VMS';
44 $Is_Dos     = $^O eq 'dos';
45 $Is_os2     = $^O eq 'os2';
46 $Is_Cygwin  = $^O eq 'cygwin';
47 $Is_MacOS   = $^O eq 'MacOS';
48 $Is_MPE     = $^O eq 'mpeix';           
49
50 $PERL = ($Is_NetWare            ? 'perl'   :
51          ($Is_MacOS || $Is_VMS) ? $^X      :
52          $Is_MSWin32            ? '.\perl' :
53          './perl');
54
55 eval '$ENV{"FOO"} = "hi there";';       # check that ENV is inited inside eval
56 # cmd.exe will echo 'variable=value' but 4nt will echo just the value
57 # -- Nikola Knezevic
58 if ($Is_MSWin32)  { ok `set FOO` =~ /^(?:FOO=)?hi there$/; }
59 elsif ($Is_MacOS) { ok "1 # skipped", 1; }
60 elsif ($Is_VMS)   { ok `write sys\$output f\$trnlnm("FOO")` eq "hi there\n"; }
61 else              { ok `echo \$FOO` eq "hi there\n"; }
62
63 unlink 'ajslkdfpqjsjfk';
64 $! = 0;
65 open(FOO,'ajslkdfpqjsjfk');
66 ok $!, $!;
67 close FOO; # just mention it, squelch used-only-once
68
69 if ($Is_MSWin32 || $Is_NetWare || $Is_Dos || $Is_MPE || $Is_MacOS) {
70     skip('SIGINT not safe on this platform') for 1..2;
71 }
72 else {
73   # the next tests are done in a subprocess because sh spits out a
74   # newline onto stderr when a child process kills itself with SIGINT.
75   # We use a pipe rather than system() because the VMS command buffer
76   # would overflow with a command that long.
77
78     open( CMDPIPE, "| $PERL");
79
80     print CMDPIPE <<'END';
81
82     $| = 1;             # command buffering
83
84     $SIG{"INT"} = "ok3";     kill "INT",$$; sleep 1;
85     $SIG{"INT"} = "IGNORE";  kill "INT",$$; sleep 1; print "ok 4\n";
86     $SIG{"INT"} = "DEFAULT"; kill "INT",$$; sleep 1; print "not ok 4\n";
87
88     sub ok3 {
89         if (($x = pop(@_)) eq "INT") {
90             print "ok 3\n";
91         }
92         else {
93             print "not ok 3 ($x @_)\n";
94         }
95     }
96
97 END
98
99     close CMDPIPE;
100
101     $test += 2;
102 }
103
104 # can we slice ENV?
105 @val1 = @ENV{keys(%ENV)};
106 @val2 = values(%ENV);
107 ok join(':',@val1) eq join(':',@val2);
108 ok @val1 > 1;
109
110 # regex vars
111 'foobarbaz' =~ /b(a)r/;
112 ok $` eq 'foo', $`;
113 ok $& eq 'bar', $&;
114 ok $' eq 'baz', $';
115 ok $+ eq 'a', $+;
116
117 # $"
118 @a = qw(foo bar baz);
119 ok "@a" eq "foo bar baz", "@a";
120 {
121     local $" = ',';
122     ok "@a" eq "foo,bar,baz", "@a";
123 }
124
125 # $;
126 %h = ();
127 $h{'foo', 'bar'} = 1;
128 ok((keys %h)[0] eq "foo\034bar", (keys %h)[0]);
129 {
130     local $; = 'x';
131     %h = ();
132     $h{'foo', 'bar'} = 1;
133     ok((keys %h)[0] eq 'fooxbar', (keys %h)[0]);
134 }
135
136 # $?, $@, $$
137 if ($Is_MacOS) {
138     skip('$? + system are broken on MacPerl') for 1..2;
139 }
140 else {
141     system qq[$PERL "-I../lib" -e "use vmsish qw(hushed); exit(0)"];
142     ok $? == 0, $?;
143     system qq[$PERL "-I../lib" -e "use vmsish qw(hushed); exit(1)"];
144     ok $? != 0, $?;
145 }
146
147 eval { die "foo\n" };
148 ok $@ eq "foo\n", $@;
149
150 ok $$ > 0, $$;
151 eval { $$++ };
152 ok $@ =~ /^Modification of a read-only value attempted/;
153
154 # $^X and $0
155 {
156     if ($^O eq 'qnx') {
157         chomp($wd = `/usr/bin/fullpath -t`);
158     }
159     elsif($Is_Cygwin || $Config{'d_procselfexe'}) {
160        # Cygwin turns the symlink into the real file
161        chomp($wd = `pwd`);
162        $wd =~ s#/t$##;
163     }
164     elsif($Is_os2) {
165        $wd = Cwd::sys_cwd();
166     }
167     elsif($Is_MacOS) {
168        $wd = ':';
169     }
170     else {
171         $wd = '.';
172     }
173     my $perl = ($Is_MacOS || $Is_VMS) ? $^X : "$wd/perl";
174     my $headmaybe = '';
175     my $tailmaybe = '';
176     $script = "$wd/show-shebang";
177     if ($Is_MSWin32) {
178         chomp($wd = `cd`);
179         $wd =~ s|\\|/|g;
180         $perl = "$wd/perl.exe";
181         $script = "$wd/show-shebang.bat";
182         $headmaybe = <<EOH ;
183 \@rem ='
184 \@echo off
185 $perl -x \%0
186 goto endofperl
187 \@rem ';
188 EOH
189         $tailmaybe = <<EOT ;
190
191 __END__
192 :endofperl
193 EOT
194     }
195     elsif ($Is_os2) {
196       $script = "./show-shebang";
197     }
198     elsif ($Is_MacOS) {
199       $script = ":show-shebang";
200     }
201     elsif ($Is_VMS) {
202       $script = "[]show-shebang";
203     }
204     if ($^O eq 'os390' or $^O eq 'posix-bc' or $^O eq 'vmesa') {  # no shebang
205         $headmaybe = <<EOH ;
206     eval 'exec ./perl -S \$0 \${1+"\$\@"}'
207         if 0;
208 EOH
209     }
210     $s1 = "\$^X is $perl, \$0 is $script\n";
211     ok open(SCRIPT, ">$script"), $!;
212     ok print(SCRIPT $headmaybe . <<EOB . <<'EOF' . $tailmaybe), $!;
213 #!$wd/perl
214 EOB
215 print "\$^X is $^X, \$0 is $0\n";
216 EOF
217     ok close(SCRIPT), $!;
218     ok chmod(0755, $script), $!;
219     $_ = ($Is_MacOS || $Is_VMS) ? `$perl $script` : `$script`;
220     s/\.exe//i if $Is_Dos or $Is_Cygwin or $Is_os2;
221     s{\bminiperl\b}{perl}; # so that test doesn't fail with miniperl
222     s{is perl}{is $perl}; # for systems where $^X is only a basename
223     s{\\}{/}g;
224     ok((($Is_MSWin32 || $Is_os2) ? uc($_) eq uc($s1) : $_ eq $s1), " :$_:!=:$s1:");
225     $_ = `$perl $script`;
226     s/\.exe//i if $Is_Dos or $Is_os2;
227     s{\\}{/}g;
228     ok((($Is_MSWin32 || $Is_os2) ? uc($_) eq uc($s1) : $_ eq $s1), " :$_:!=:$s1: after `$perl $script`");
229     ok unlink($script), $!;
230 }
231
232 # $], $^O, $^T
233 ok $] >= 5.00319, $];
234 ok $^O;
235 ok $^T > 850000000, $^T;
236
237 if ($Is_VMS || $Is_Dos || $Is_MacOS) {
238     skip("%ENV manipulations fail or aren't safe on $^O") for 1..2;
239 }
240 else {
241         $PATH = $ENV{PATH};
242         $PDL = $ENV{PERL_DESTRUCT_LEVEL} || 0;
243         $ENV{foo} = "bar";
244         %ENV = ();
245         $ENV{PATH} = $PATH;
246         $ENV{PERL_DESTRUCT_LEVEL} = $PDL || 0;
247         ok ($Is_MSWin32 ? (`set foo 2>NUL` eq "")
248                                 : (`echo \$foo` eq "\n") );
249
250         $ENV{__NoNeSuCh} = "foo";
251         $0 = "bar";
252 # cmd.exe will echo 'variable=value' but 4nt will echo just the value
253 # -- Nikola Knezevic
254        ok ($Is_MSWin32 ? (`set __NoNeSuCh` =~ /^(?:__NoNeSuCh=)?foo$/)
255                             : (`echo \$__NoNeSuCh` eq "foo\n") );
256 }
257
258 {
259     my $ok = 1;
260     my $warn = '';
261     local $SIG{'__WARN__'} = sub { $ok = 0; $warn = join '', @_; };
262     $! = undef;
263     ok($ok, $warn, $Is_VMS ? "'\$!=undef' does throw a warning" : '');
264 }
265
266 # test case-insignificance of %ENV (these tests must be enabled only
267 # when perl is compiled with -DENV_IS_CASELESS)
268 if ($Is_MSWin32 || $Is_NetWare) {
269     %ENV = ();
270     $ENV{'Foo'} = 'bar';
271     $ENV{'fOo'} = 'baz';
272     ok (scalar(keys(%ENV)) == 1);
273     ok exists($ENV{'FOo'});
274     ok (delete($ENV{'foO'}) eq 'baz');
275     ok (scalar(keys(%ENV)) == 0);
276 }
277 else {
278     skip('no caseless %ENV support') for 1..4;
279 }
280
281 # Make sure Errno hasn't been prematurely autoloaded
282
283 ok !defined %Errno::;
284
285 # Test auto-loading of Errno when %! is used
286
287 ok scalar eval q{
288    my $errs = %!;
289    defined %Errno::;
290 }, $@;
291
292
293 # Make sure that Errno loading doesn't clobber $!
294
295 undef %Errno::;
296 delete $INC{"Errno.pm"};
297
298 open(FOO, "nonesuch"); # Generate ENOENT
299 my %errs = %{"!"}; # Cause Errno.pm to be loaded at run-time
300 ok ${"!"}{ENOENT};
301
302 ok $^S == 0;
303 eval { ok $^S == 1 };
304 ok $^S == 0;
305
306 ok ${^TAINT} == 0;
307 eval { ${^TAINT} = 1 };
308 ok ${^TAINT} == 0;
309
310 # 5.6.1 had a bug: @+ and @- were not properly interpolated
311 # into double-quoted strings
312 # 20020414 mjd-perl-patch+@plover.com
313 "I like pie" =~ /(I) (like) (pie)/;
314 ok "@-" eq  "0 0 2 7";
315 ok "@+" eq "10 1 6 10";
316