3 # Tests for the command-line switches:
4 # -0, -c, -l, -s, -m, -M, -V, -v, -h, -i, -E and all unknown
5 # Some switches have their own tests, see MANIFEST.
10 require Config; import Config;
13 BEGIN { require "./test.pl"; }
18 BEGIN { eval 'use POSIX qw(setlocale LC_ALL)' }
20 # due to a bug in VMS's piping which makes it impossible for runperl()
21 # to emulate echo -n (ie. stdin always winds up with a newline), these
22 # tests almost totally fail.
23 $TODO = "runperl() unable to emulate echo -n due to pipe bug" if $^O eq 'VMS';
27 END { unlink_all @tmpfiles }
32 switches => [ '-0', ],
33 stdin => 'foo\0bar\0baz\0',
34 prog => 'print qq(<$_>) while <>',
36 is( $r, "<foo\0><bar\0><baz\0>", "-0" );
39 switches => [ '-l', '-0', '-p' ],
40 stdin => 'foo\0bar\0baz\0',
43 is( $r, "foo\nbar\nbaz\n", "-0 after a -l" );
46 switches => [ '-0', '-l', '-p' ],
47 stdin => 'foo\0bar\0baz\0',
50 is( $r, "foo\0bar\0baz\0", "-0 before a -l" );
53 switches => [ sprintf("-0%o", ord 'x') ],
54 stdin => 'fooxbarxbazx',
55 prog => 'print qq(<$_>) while <>',
57 is( $r, "<foox><barx><bazx>", "-0 with octal number" );
60 switches => [ '-00', '-p' ],
61 stdin => 'abc\ndef\n\nghi\njkl\nmno\n\npq\n',
62 prog => 's/\n/-/g;$_.=q(/)',
64 is( $r, 'abc-def--/ghi-jkl-mno--/pq-/', '-00 (paragraph mode)' );
67 switches => [ '-0777', '-p' ],
68 stdin => 'abc\ndef\n\nghi\njkl\nmno\n\npq\n',
69 prog => 's/\n/-/g;$_.=q(/)',
71 is( $r, 'abc-def--ghi-jkl-mno--pq-/', '-0777 (slurp mode)' );
74 switches => [ '-066' ],
75 prog => 'BEGIN { print qq{($/)} } print qq{[$/]}',
77 is( $r, "(\066)[\066]", '$/ set at compile-time' );
81 my $filename = tempfile();
83 local $TODO = ''; # this one works on VMS
85 open my $f, ">$filename" or skip( "Can't write temp file $filename: $!" );
87 BEGIN { print "block 1\n"; }
88 CHECK { print "block 2\n"; }
89 INIT { print "block 3\n"; }
91 END { print "block 5\n"; }
93 close $f or die "Could not close: $!";
96 progfile => $filename,
99 # Because of the stderr redirection, we can't tell reliably the order
100 # in which the output is given
102 $r =~ /$filename syntax OK/
103 && $r =~ /\bblock 1\b/
104 && $r =~ /\bblock 2\b/
105 && $r !~ /\bblock 3\b/
106 && $r !~ /\bblock 4\b/
107 && $r !~ /\bblock 5\b/,
113 skip "no POSIX on miniperl", 1, unless $INC{"POSIX.pm"};
114 skip 'No locale testing without d_setlocale', 1 if(!$Config{d_setlocale});
116 my $tempdir = tempfile;
117 mkdir $tempdir, 0700 or die "Can't mkdir '$tempdir': $!";
119 local $ENV{'LC_ALL'} = 'C'; # Keep the test simple: expect English
120 local $ENV{LANGUAGE} = 'C';
121 setlocale(LC_ALL, "C");
123 # Win32 won't let us open the directory, so we never get to die with
124 # EISDIR, which happens after open.
126 import Errno qw(EACCES EISDIR);
128 local $! = $^O eq 'MSWin32' ? &EACCES : &EISDIR; "$!"
131 runperl( switches => [ '-c' ], args => [ $tempdir ], stderr => 1),
132 qr/Can't open perl script.*$tempdir.*\Q$error/s,
133 "RT \#61362: Cannot syntax-check a directory"
135 rmdir $tempdir or die "Can't rmdir '$tempdir': $!";
141 switches => [ sprintf("-l%o", ord 'x') ],
142 prog => 'print for qw/foo bar/'
144 is( $r, 'fooxbarx', '-l with octal number' );
149 switches => [ '-s' ],
150 prog => 'for (qw/abc def ghi/) {print defined $$_ ? $$_ : q(-)}',
151 args => [ '--', '-abc=2', '-def', ],
153 is( $r, '21-', '-s switch parsing' );
155 $filename = tempfile();
157 open my $f, ">$filename" or skip( "Can't write temp file $filename: $!" );
160 BEGIN { print $x,$y; exit }
162 close $f or die "Could not close: $!";
164 progfile => $filename,
165 args => [ '-x=foo -y' ],
167 is( $r, 'foo1', '-s on the shebang line' );
170 # Bug ID 20011106.084
171 $filename = tempfile();
173 open my $f, ">$filename" or skip( "Can't write temp file $filename: $!" );
176 BEGIN { print $x; exit }
178 close $f or die "Could not close: $!";
180 progfile => $filename,
181 args => [ '-x=foo' ],
183 is( $r, 'foo', '-sn on the shebang line' );
186 # Tests for -m and -M
188 my $package = tempfile();
189 $filename = "$package.pm";
191 open my $f, ">$filename" or skip( "Can't write temp file $filename: $!",4 );
192 print $f <<"SWTESTPM";
194 sub import { print map "<\$_>", \@_ }
197 close $f or die "Could not close: $!";
199 switches => [ "-M$package" ],
202 is( $r, "<$package>", '-M' );
204 switches => [ "-M$package=foo" ],
207 is( $r, "<$package><foo>", '-M with import parameter' );
209 switches => [ "-m$package" ],
214 local $TODO = ''; # this one works on VMS
218 switches => [ "-m$package=foo,bar" ],
221 is( $r, "<$package><foo><bar>", '-m with import parameters' );
222 push @tmpfiles, $filename;
225 local $TODO = ''; # these work on VMS
227 is( runperl( switches => [ '-MTie::Hash' ], stderr => 1, prog => 1 ),
228 '', "-MFoo::Bar allowed" );
230 like( runperl( switches => [ "-M:$package" ], stderr => 1,
231 prog => 'die q{oops}' ),
232 qr/Invalid module name [\w:]+ with -M option\b/,
233 "-M:Foo not allowed" );
235 like( runperl( switches => [ '-mA:B:C' ], stderr => 1,
236 prog => 'die q{oops}' ),
237 qr/Invalid module name [\w:]+ with -m option\b/,
238 "-mFoo:Bar not allowed" );
240 like( runperl( switches => [ '-m-A:B:C' ], stderr => 1,
241 prog => 'die q{oops}' ),
242 qr/Invalid module name [\w:]+ with -m option\b/,
243 "-m-Foo:Bar not allowed" );
245 like( runperl( switches => [ '-m-' ], stderr => 1,
246 prog => 'die q{oops}' ),
247 qr/Module name required with -m option\b/,
250 like( runperl( switches => [ '-M-=' ], stderr => 1,
251 prog => 'die q{oops}' ),
252 qr/Module name required with -M option\b/,
254 } # disable TODO on VMS
256 is runperl(stderr => 1, prog => '#!perl -m'),
257 qq 'Too late for "-m" option at -e line 1.\n', '#!perl -m';
258 is runperl(stderr => 1, prog => '#!perl -M'),
259 qq 'Too late for "-M" option at -e line 1.\n', '#!perl -M';
264 local $TODO = ''; # these ones should work on VMS
266 # basic perl -V should generate significant output.
267 # we don't test actual format too much since it could change
268 like( runperl( switches => ['-V'] ), qr/(\n.*){20}/,
269 '-V generates 20+ lines' );
271 like( runperl( switches => ['-V'] ),
272 qr/\ASummary of my perl5 .*configuration:/,
275 # lookup a known config var
276 chomp( $r=runperl( switches => ['-V:osname'] ) );
277 is( $r, "osname='$^O';", 'perl -V:osname');
279 # lookup a nonexistent var
280 chomp( $r=runperl( switches => ['-V:this_var_makes_switches_test_fail'] ) );
281 is( $r, "this_var_makes_switches_test_fail='UNKNOWN';",
282 'perl -V:unknown var');
285 # platforms that don't like this quoting can either skip this test
286 # or fix test.pl _quote_args
287 $r = runperl( switches => ['"-V:i\D+size"'] );
288 # should be unlike( $r, qr/^$|not found|UNKNOWN/ );
289 like( $r, qr/^(?!.*(not found|UNKNOWN))./, 'perl -V:re got a result' );
291 # make sure each line we got matches the re
292 ok( !( grep !/^i\D+size=/, split /^/, $r ), '-V:re correct' );
298 local $TODO = ''; # these ones should work on VMS
299 # there are definitely known build configs where this test will fail
300 # DG/UX comes to mind. Maybe we should remove these special cases?
303 skip "Win32 miniperl produces a default archname in -v", 1
304 if $^O eq 'MSWin32' && is_miniperl;
305 my $v = sprintf "%vd", $^V;
306 my $ver = $Config{PERL_VERSION};
307 my $rel = $Config{PERL_SUBVERSION};
308 like( runperl( switches => ['-v'] ),
309 qr/This is perl 5, version \Q$ver\E, subversion \Q$rel\E \(v\Q$v\E(?:[-*\w]+| \([^)]+\))?\) built for \Q$Config{archname}\E.+Copyright.+Larry Wall.+Artistic License.+GNU General Public License/s,
317 local $TODO = ''; # these ones should work on VMS
319 like( runperl( switches => ['-h'] ),
320 qr/Usage: .+(?i:perl(?:$Config{_exe})?).+switches.+programfile.+arguments/,
325 # Tests for switches which do not exist
327 foreach my $switch (split //, "ABbGgHJjKkLNOoPQqRrYyZz123456789_")
329 local $TODO = ''; # these ones should work on VMS
331 like( runperl( switches => ["-$switch"], stderr => 1,
332 prog => 'die q{oops}' ),
333 qr/\QUnrecognized switch: -$switch (-h will show valid options)./,
334 "-$switch correctly unknown" );
337 like( runperl( stderr => 1, prog => "#!perl -$switch" ),
338 qr/^Unrecognized switch: -$switch \(-h will show valid (?x:
339 )options\) at -e line 1\./,
340 "-$switch unrecognised on #! line" );
343 # Tests for unshebangable switches
344 for (qw( e f x E S V )) {
347 prog => "#!perl -$_",
349 is $r, "Can't emulate -$_ on #! line at -e line 1.\n","-$_ on #! line";
355 local $TODO = ''; # these ones should work on VMS
357 sub do_i_unlink { unlink_all("file", "file.bak") }
359 open(FILE, ">file") or die "$0: Failed to create 'file': $!";
360 print FILE <<__EOF__;
367 END { do_i_unlink() }
369 runperl( switches => ['-pi.bak'], prog => 's/foo/bar/', args => ['file'] );
371 open(FILE, "file") or die "$0: Failed to open 'file': $!";
372 chomp(my @file = <FILE>);
375 open(BAK, "file.bak") or die "$0: Failed to open 'file': $!";
376 chomp(my @bak = <BAK>);
380 "bar yada dada:bada bar bing:king kong bar",
383 "foo yada dada:bada foo bing:king kong foo",
387 switches => ['-i.bak -p'],
394 "-i used with no filenames on the command line, reading from STDIN.\n",
395 "warning when no files given"
398 switches => ['-i.bak -p'],
404 is($out2, "", "no warning when files given");
409 $TODO = ''; # the -E tests work on VMS
412 switches => [ '-E', '"say q(Hello, world!)"']
414 is( $r, "Hello, world!\n", "-E say" );
418 switches => [ '-E', '"no warnings q{experimental::smartmatch}; undef ~~ undef and say q(Hello, world!)"']
420 is( $r, "Hello, world!\n", "-E ~~" );
423 switches => [ '-E', '"no warnings q{experimental::smartmatch}; given(undef) {when(undef) { say q(Hello, world!)"}}']
425 is( $r, "Hello, world!\n", "-E given" );
428 switches => [ '-nE', q("} END { say q/affe/") ],
431 is( $r, "affe\n", '-E works outside of the block created by -n' );
434 switches => [ '-E', q("*{'bar'} = sub{}; print 'Hello, world!',qq|\n|;")]
436 is( $r, "Hello, world!\n", "-E does not enable strictures" );
440 $filename = tempfile();
442 open my $f, ">$filename" or skip( "Can't write temp file $filename: $!" );
447 close $f or die "Could not close: $!";
449 progfile => $filename,
451 like( $r, qr/ok/, 'Spaces on the #! line (#30660)' );