source: trunk/essentials/dev-lang/perl/t/op/magic.t

Last change on this file was 3181, checked in by bird, 18 years ago

perl 5.8.8

File size: 11.3 KB
Line 
1#!./perl
2
3BEGIN {
4 $| = 1;
5 chdir 't' if -d 't';
6 @INC = '../lib';
7 $SIG{__WARN__} = sub { die "Dying on warning: ", @_ };
8}
9
10use warnings;
11use Config;
12
13my $test = 1;
14sub 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\n" if defined $info;
24 }
25
26 $test++;
27 return $ok;
28}
29
30sub skip {
31 my($reason) = @_;
32
33 printf "ok $test # skipped%s\n", defined $reason ? ": $reason" : '';
34
35 $test++;
36 return 1;
37}
38
39print "1..58\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$Is_miniperl = $ENV{PERL_CORE_MINITEST};
50$Is_BeOS = $^O eq 'beos';
51
52$PERL = $ENV{PERL}
53 || ($Is_NetWare ? 'perl' :
54 ($Is_MacOS || $Is_VMS) ? $^X :
55 $Is_MSWin32 ? '.\perl' :
56 './perl');
57
58eval '$ENV{"FOO"} = "hi there";'; # check that ENV is inited inside eval
59# cmd.exe will echo 'variable=value' but 4nt will echo just the value
60# -- Nikola Knezevic
61if ($Is_MSWin32) { ok `set FOO` =~ /^(?:FOO=)?hi there$/; }
62elsif ($Is_MacOS) { ok "1 # skipped", 1; }
63elsif ($Is_VMS) { ok `write sys\$output f\$trnlnm("FOO")` eq "hi there\n"; }
64else { ok `echo \$FOO` eq "hi there\n"; }
65
66unlink 'ajslkdfpqjsjfk';
67$! = 0;
68open(FOO,'ajslkdfpqjsjfk');
69ok $!, $!;
70close FOO; # just mention it, squelch used-only-once
71
72if ($Is_MSWin32 || $Is_NetWare || $Is_Dos || $Is_MPE || $Is_MacOS) {
73 skip('SIGINT not safe on this platform') for 1..4;
74}
75else {
76 # the next tests are done in a subprocess because sh spits out a
77 # newline onto stderr when a child process kills itself with SIGINT.
78 # We use a pipe rather than system() because the VMS command buffer
79 # would overflow with a command that long.
80
81 open( CMDPIPE, "| $PERL");
82
83 print CMDPIPE <<'END';
84
85 $| = 1; # command buffering
86
87 $SIG{"INT"} = "ok3"; kill "INT",$$; sleep 1;
88 $SIG{"INT"} = "IGNORE"; kill "INT",$$; sleep 1; print "ok 4\n";
89 $SIG{"INT"} = "DEFAULT"; kill "INT",$$; sleep 1; print "not ok 4\n";
90
91 sub ok3 {
92 if (($x = pop(@_)) eq "INT") {
93 print "ok 3\n";
94 }
95 else {
96 print "not ok 3 ($x @_)\n";
97 }
98 }
99
100END
101
102 close CMDPIPE;
103
104 open( CMDPIPE, "| $PERL");
105 print CMDPIPE <<'END';
106
107 { package X;
108 sub DESTROY {
109 kill "INT",$$;
110 }
111 }
112 sub x {
113 my $x=bless [], 'X';
114 return sub { $x };
115 }
116 $| = 1; # command buffering
117 $SIG{"INT"} = "ok5";
118 {
119 local $SIG{"INT"}=x();
120 print ""; # Needed to expose failure in 5.8.0 (why?)
121 }
122 sleep 1;
123 delete $SIG{"INT"};
124 kill "INT",$$; sleep 1;
125 sub ok5 {
126 print "ok 5\n";
127 }
128END
129 close CMDPIPE;
130 $? >>= 8 if $^O eq 'VMS'; # POSIX status hiding in 2nd byte
131 my $todo = ($^O eq 'os2' ? ' # TODO: EMX v0.9d_fix4 bug: wrong nibble? ' : '');
132 print $? & 0xFF ? "ok 6$todo\n" : "not ok 6$todo\n";
133
134 $test += 4;
135}
136
137# can we slice ENV?
138@val1 = @ENV{keys(%ENV)};
139@val2 = values(%ENV);
140ok join(':',@val1) eq join(':',@val2);
141ok @val1 > 1;
142
143# regex vars
144'foobarbaz' =~ /b(a)r/;
145ok $` eq 'foo', $`;
146ok $& eq 'bar', $&;
147ok $' eq 'baz', $';
148ok $+ eq 'a', $+;
149
150# $"
151@a = qw(foo bar baz);
152ok "@a" eq "foo bar baz", "@a";
153{
154 local $" = ',';
155 ok "@a" eq "foo,bar,baz", "@a";
156}
157
158# $;
159%h = ();
160$h{'foo', 'bar'} = 1;
161ok((keys %h)[0] eq "foo\034bar", (keys %h)[0]);
162{
163 local $; = 'x';
164 %h = ();
165 $h{'foo', 'bar'} = 1;
166 ok((keys %h)[0] eq 'fooxbar', (keys %h)[0]);
167}
168
169# $?, $@, $$
170if ($Is_MacOS) {
171 skip('$? + system are broken on MacPerl') for 1..2;
172}
173else {
174 system qq[$PERL "-I../lib" -e "use vmsish qw(hushed); exit(0)"];
175 ok $? == 0, $?;
176 system qq[$PERL "-I../lib" -e "use vmsish qw(hushed); exit(1)"];
177 ok $? != 0, $?;
178}
179
180eval { die "foo\n" };
181ok $@ eq "foo\n", $@;
182
183ok $$ > 0, $$;
184eval { $$++ };
185ok $@ =~ /^Modification of a read-only value attempted/;
186
187# $^X and $0
188{
189 if ($^O eq 'qnx') {
190 chomp($wd = `/usr/bin/fullpath -t`);
191 }
192 elsif($Is_Cygwin || $Config{'d_procselfexe'}) {
193 # Cygwin turns the symlink into the real file
194 chomp($wd = `pwd`);
195 $wd =~ s#/t$##;
196 }
197 elsif($Is_os2) {
198 $wd = Cwd::sys_cwd();
199 }
200 elsif($Is_MacOS) {
201 $wd = ':';
202 }
203 else {
204 $wd = '.';
205 }
206 my $perl = ($Is_MacOS || $Is_VMS) ? $^X : "$wd/perl";
207 my $headmaybe = '';
208 my $tailmaybe = '';
209 $script = "$wd/show-shebang";
210 if ($Is_MSWin32) {
211 chomp($wd = `cd`);
212 $wd =~ s|\\|/|g;
213 $perl = "$wd/perl.exe";
214 $script = "$wd/show-shebang.bat";
215 $headmaybe = <<EOH ;
216\@rem ='
217\@echo off
218$perl -x \%0
219goto endofperl
220\@rem ';
221EOH
222 $tailmaybe = <<EOT ;
223
224__END__
225:endofperl
226EOT
227 }
228 elsif ($Is_os2) {
229 $script = "./show-shebang";
230 }
231 elsif ($Is_MacOS) {
232 $script = ":show-shebang";
233 }
234 elsif ($Is_VMS) {
235 $script = "[]show-shebang";
236 }
237 if ($^O eq 'os390' or $^O eq 'posix-bc' or $^O eq 'vmesa') { # no shebang
238 $headmaybe = <<EOH ;
239 eval 'exec ./perl -S \$0 \${1+"\$\@"}'
240 if 0;
241EOH
242 }
243 $s1 = "\$^X is $perl, \$0 is $script\n";
244 ok open(SCRIPT, ">$script"), $!;
245 ok print(SCRIPT $headmaybe . <<EOB . <<'EOF' . $tailmaybe), $!;
246#!$wd/perl
247EOB
248print "\$^X is $^X, \$0 is $0\n";
249EOF
250 ok close(SCRIPT), $!;
251 ok chmod(0755, $script), $!;
252 $_ = ($Is_MacOS || $Is_VMS) ? `$perl $script` : `$script`;
253 s/\.exe//i if $Is_Dos or $Is_Cygwin or $Is_os2;
254 s{./$script}{$script} if $Is_BeOS; # revert BeOS execvp() side-effect
255 s{\bminiperl\b}{perl}; # so that test doesn't fail with miniperl
256 s{is perl}{is $perl}; # for systems where $^X is only a basename
257 s{\\}{/}g;
258 ok((($Is_MSWin32 || $Is_os2) ? uc($_) eq uc($s1) : $_ eq $s1), " :$_:!=:$s1:");
259 $_ = `$perl $script`;
260 s/\.exe//i if $Is_Dos or $Is_os2;
261 s{./$perl}{$perl} if $Is_BeOS; # revert BeOS execvp() side-effect
262 s{\\}{/}g;
263 ok((($Is_MSWin32 || $Is_os2) ? uc($_) eq uc($s1) : $_ eq $s1), " :$_:!=:$s1: after `$perl $script`");
264 ok unlink($script), $!;
265}
266
267# $], $^O, $^T
268ok $] >= 5.00319, $];
269ok $^O;
270ok $^T > 850000000, $^T;
271
272# Test change 25062 is working
273my $orig_osname = $^O;
274{
275local $^I = '.bak';
276ok($^O eq $orig_osname, 'Assigning $^I does not clobber $^O');
277}
278$^O = $orig_osname;
279
280if ($Is_VMS || $Is_Dos || $Is_MacOS) {
281 skip("%ENV manipulations fail or aren't safe on $^O") for 1..4;
282}
283else {
284 if ($ENV{PERL_VALGRIND}) {
285 skip("clearing \%ENV is not safe when running under valgrind");
286 } else {
287 $PATH = $ENV{PATH};
288 $PDL = $ENV{PERL_DESTRUCT_LEVEL} || 0;
289 $ENV{foo} = "bar";
290 %ENV = ();
291 $ENV{PATH} = $PATH;
292 $ENV{PERL_DESTRUCT_LEVEL} = $PDL || 0;
293 ok ($Is_MSWin32 ? (`set foo 2>NUL` eq "")
294 : (`echo \$foo` eq "\n") );
295 }
296
297 $ENV{__NoNeSuCh} = "foo";
298 $0 = "bar";
299# cmd.exe will echo 'variable=value' but 4nt will echo just the value
300# -- Nikola Knezevic
301 ok ($Is_MSWin32 ? (`set __NoNeSuCh` =~ /^(?:__NoNeSuCh=)?foo$/)
302 : (`echo \$__NoNeSuCh` eq "foo\n") );
303 if ($^O =~ /^(linux|freebsd)$/ &&
304 open CMDLINE, "/proc/$$/cmdline") {
305 chomp(my $line = scalar <CMDLINE>);
306 my $me = (split /\0/, $line)[0];
307 ok($me eq $0, 'altering $0 is effective (testing with /proc/)');
308 close CMDLINE;
309 # perlbug #22811
310 my $mydollarzero = sub {
311 my($arg) = shift;
312 $0 = $arg if defined $arg;
313 # In FreeBSD the ps -o command= will cause
314 # an empty header line, grab only the last line.
315 my $ps = (`ps -o command= -p $$`)[-1];
316 return if $?;
317 chomp $ps;
318 printf "# 0[%s]ps[%s]\n", $0, $ps;
319 $ps;
320 };
321 my $ps = $mydollarzero->("x");
322 ok(!$ps # we allow that something goes wrong with the ps command
323 # In Linux 2.4 we would get an exact match ($ps eq 'x') but
324 # in Linux 2.2 there seems to be something funny going on:
325 # it seems as if the original length of the argv[] would
326 # be stored in the proc struct and then used by ps(1),
327 # no matter what characters we use to pad the argv[].
328 # (And if we use \0:s, they are shown as spaces.) Sigh.
329 || $ps =~ /^x\s*$/
330 # FreeBSD cannot get rid of both the leading "perl :"
331 # and the trailing " (perl)": some FreeBSD versions
332 # can get rid of the first one.
333 || ($^O eq 'freebsd' && $ps =~ m/^(?:perl: )?x(?: \(perl\))?$/),
334 'altering $0 is effective (testing with `ps`)');
335 } else {
336 skip("\$0 check only on Linux and FreeBSD") for 0, 1;
337 }
338}
339
340{
341 my $ok = 1;
342 my $warn = '';
343 local $SIG{'__WARN__'} = sub { $ok = 0; $warn = join '', @_; };
344 $! = undef;
345 ok($ok, $warn, $Is_VMS ? "'\$!=undef' does throw a warning" : '');
346}
347
348# test case-insignificance of %ENV (these tests must be enabled only
349# when perl is compiled with -DENV_IS_CASELESS)
350if ($Is_MSWin32 || $Is_NetWare) {
351 %ENV = ();
352 $ENV{'Foo'} = 'bar';
353 $ENV{'fOo'} = 'baz';
354 ok (scalar(keys(%ENV)) == 1);
355 ok exists($ENV{'FOo'});
356 ok (delete($ENV{'foO'}) eq 'baz');
357 ok (scalar(keys(%ENV)) == 0);
358}
359else {
360 skip('no caseless %ENV support') for 1..4;
361}
362
363if ($Is_miniperl) {
364 skip ("miniperl can't rely on loading %Errno") for 1..2;
365} else {
366 no warnings 'void';
367
368# Make sure Errno hasn't been prematurely autoloaded
369
370 ok !keys %Errno::;
371
372# Test auto-loading of Errno when %! is used
373
374 ok scalar eval q{
375 %!;
376 defined %Errno::;
377 }, $@;
378}
379
380if ($Is_miniperl) {
381 skip ("miniperl can't rely on loading %Errno");
382} else {
383 # Make sure that Errno loading doesn't clobber $!
384
385 undef %Errno::;
386 delete $INC{"Errno.pm"};
387
388 open(FOO, "nonesuch"); # Generate ENOENT
389 my %errs = %{"!"}; # Cause Errno.pm to be loaded at run-time
390 ok ${"!"}{ENOENT};
391}
392
393ok $^S == 0 && defined $^S;
394eval { ok $^S == 1 };
395eval " BEGIN { ok ! defined \$^S } ";
396ok $^S == 0 && defined $^S;
397
398ok ${^TAINT} == 0;
399eval { ${^TAINT} = 1 };
400ok ${^TAINT} == 0;
401
402# 5.6.1 had a bug: @+ and @- were not properly interpolated
403# into double-quoted strings
404# 20020414 mjd-perl-patch+@plover.com
405"I like pie" =~ /(I) (like) (pie)/;
406ok "@-" eq "0 0 2 7";
407ok "@+" eq "10 1 6 10";
408
409# Tests for the magic get of $\
410{
411 my $ok = 0;
412 # [perl #19330]
413 {
414 local $\ = undef;
415 $\++; $\++;
416 $ok = $\ eq 2;
417 }
418 ok $ok;
419 $ok = 0;
420 {
421 local $\ = "a\0b";
422 $ok = "a$\b" eq "aa\0bb";
423 }
424 ok $ok;
425}
426
427# Test for bug [perl #27839]
428{
429 my $x;
430 sub f {
431 "abc" =~ /(.)./;
432 $x = "@+";
433 return @+;
434 };
435 my @y = f();
436 ok( $x eq "@y", "return a magic array ($x) vs (@y)" );
437}
438
439# Test for bug [perl #36434]
440if (!$Is_VMS) {
441 local @ISA;
442 local %ENV;
443 eval { push @ISA, __PACKAGE__ };
444 ok( $@ eq '', 'Push a constant on a magic array');
445 $@ and print "# $@";
446 eval { %ENV = (PATH => __PACKAGE__) };
447 ok( $@ eq '', 'Assign a constant to a magic hash');
448 $@ and print "# $@";
449 eval { my %h = qw(A B); %ENV = (PATH => (keys %h)[0]) };
450 ok( $@ eq '', 'Assign a shared key to a magic hash');
451 $@ and print "# $@";
452}
453else {
454# Can not do this test on VMS, EPOC, and SYMBIAN according to comments
455# in mg.c/Perl_magic_clear_all_env()
456#
457 skip('Can\'t make assignment to \%ENV on this system') for 1..3;
458}
Note: See TracBrowser for help on using the repository browser.