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