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 use Errno qw(EACCES EISDIR);
19 BEGIN { eval 'use POSIX qw(setlocale LC_ALL)' }
21 # due to a bug in VMS's piping which makes it impossible for runperl()
22 # to emulate echo -n (ie. stdin always winds up with a newline), these
23 # tests almost totally fail.
24 $TODO = "runperl() unable to emulate echo -n due to pipe bug" if $^O eq 'VMS';
28 END { unlink_all @tmpfiles }
33 switches => [ '-0', ],
34 stdin => 'foo\0bar\0baz\0',
35 prog => 'print qq(<$_>) while <>',
37 is( $r, "<foo\0><bar\0><baz\0>", "-0" );
40 switches => [ '-l', '-0', '-p' ],
41 stdin => 'foo\0bar\0baz\0',
44 is( $r, "foo\nbar\nbaz\n", "-0 after a -l" );
47 switches => [ '-0', '-l', '-p' ],
48 stdin => 'foo\0bar\0baz\0',
51 is( $r, "foo\0bar\0baz\0", "-0 before a -l" );
54 switches => [ sprintf("-0%o", ord 'x') ],
55 stdin => 'fooxbarxbazx',
56 prog => 'print qq(<$_>) while <>',
58 is( $r, "<foox><barx><bazx>", "-0 with octal number" );
61 switches => [ '-00', '-p' ],
62 stdin => 'abc\ndef\n\nghi\njkl\nmno\n\npq\n',
63 prog => 's/\n/-/g;$_.=q(/)',
65 is( $r, 'abc-def--/ghi-jkl-mno--/pq-/', '-00 (paragraph mode)' );
68 switches => [ '-0777', '-p' ],
69 stdin => 'abc\ndef\n\nghi\njkl\nmno\n\npq\n',
70 prog => 's/\n/-/g;$_.=q(/)',
72 is( $r, 'abc-def--ghi-jkl-mno--pq-/', '-0777 (slurp mode)' );
75 switches => [ '-066' ],
76 prog => 'BEGIN { print qq{($/)} } print qq{[$/]}',
78 is( $r, "(\066)[\066]", '$/ set at compile-time' );
82 my $filename = tempfile();
84 local $TODO = ''; # this one works on VMS
86 open my $f, ">$filename" or skip( "Can't write temp file $filename: $!" );
88 BEGIN { print "block 1\n"; }
89 CHECK { print "block 2\n"; }
90 INIT { print "block 3\n"; }
92 END { print "block 5\n"; }
94 close $f or die "Could not close: $!";
97 progfile => $filename,
100 # Because of the stderr redirection, we can't tell reliably the order
101 # in which the output is given
103 $r =~ /$filename syntax OK/
104 && $r =~ /\bblock 1\b/
105 && $r =~ /\bblock 2\b/
106 && $r !~ /\bblock 3\b/
107 && $r !~ /\bblock 4\b/
108 && $r !~ /\bblock 5\b/,
114 skip "no POSIX on miniperl", 1, unless $INC{"POSIX.pm"};
115 skip 'No locale testing without d_setlocale', 1 if(!$Config{d_setlocale});
117 my $tempdir = tempfile;
118 mkdir $tempdir, 0700 or die "Can't mkdir '$tempdir': $!";
120 local $ENV{'LC_ALL'} = 'C'; # Keep the test simple: expect English
121 local $ENV{LANGUAGE} = 'C';
122 setlocale(LC_ALL, "C");
124 # Win32 won't let us open the directory, so we never get to die with
125 # EISDIR, which happens after open.
126 my $error = do { local $! = $^O eq 'MSWin32' ? EACCES : EISDIR; "$!" };
128 runperl( switches => [ '-c' ], args => [ $tempdir ], stderr => 1),
129 qr/Can't open perl script.*$tempdir.*\Q$error/s,
130 "RT \#61362: Cannot syntax-check a directory"
132 rmdir $tempdir or die "Can't rmdir '$tempdir': $!";
138 switches => [ sprintf("-l%o", ord 'x') ],
139 prog => 'print for qw/foo bar/'
141 is( $r, 'fooxbarx', '-l with octal number' );
146 switches => [ '-s' ],
147 prog => 'for (qw/abc def ghi/) {print defined $$_ ? $$_ : q(-)}',
148 args => [ '--', '-abc=2', '-def', ],
150 is( $r, '21-', '-s switch parsing' );
152 $filename = tempfile();
154 open my $f, ">$filename" or skip( "Can't write temp file $filename: $!" );
157 BEGIN { print $x,$y; exit }
159 close $f or die "Could not close: $!";
161 progfile => $filename,
162 args => [ '-x=foo -y' ],
164 is( $r, 'foo1', '-s on the shebang line' );
167 # Bug ID 20011106.084
168 $filename = tempfile();
170 open my $f, ">$filename" or skip( "Can't write temp file $filename: $!" );
173 BEGIN { print $x; exit }
175 close $f or die "Could not close: $!";
177 progfile => $filename,
178 args => [ '-x=foo' ],
180 is( $r, 'foo', '-sn on the shebang line' );
183 # Tests for -m and -M
185 my $package = tempfile();
186 $filename = "$package.pm";
188 open my $f, ">$filename" or skip( "Can't write temp file $filename: $!",4 );
189 print $f <<"SWTESTPM";
191 sub import { print map "<\$_>", \@_ }
194 close $f or die "Could not close: $!";
196 switches => [ "-M$package" ],
199 is( $r, "<$package>", '-M' );
201 switches => [ "-M$package=foo" ],
204 is( $r, "<$package><foo>", '-M with import parameter' );
206 switches => [ "-m$package" ],
211 local $TODO = ''; # this one works on VMS
215 switches => [ "-m$package=foo,bar" ],
218 is( $r, "<$package><foo><bar>", '-m with import parameters' );
219 push @tmpfiles, $filename;
222 local $TODO = ''; # these work on VMS
224 is( runperl( switches => [ '-MTie::Hash' ], stderr => 1, prog => 1 ),
225 '', "-MFoo::Bar allowed" );
227 like( runperl( switches => [ "-M:$package" ], stderr => 1,
228 prog => 'die q{oops}' ),
229 qr/Invalid module name [\w:]+ with -M option\b/,
230 "-M:Foo not allowed" );
232 like( runperl( switches => [ '-mA:B:C' ], stderr => 1,
233 prog => 'die q{oops}' ),
234 qr/Invalid module name [\w:]+ with -m option\b/,
235 "-mFoo:Bar not allowed" );
237 like( runperl( switches => [ '-m-A:B:C' ], stderr => 1,
238 prog => 'die q{oops}' ),
239 qr/Invalid module name [\w:]+ with -m option\b/,
240 "-m-Foo:Bar not allowed" );
242 like( runperl( switches => [ '-m-' ], stderr => 1,
243 prog => 'die q{oops}' ),
244 qr/Module name required with -m option\b/,
247 like( runperl( switches => [ '-M-=' ], stderr => 1,
248 prog => 'die q{oops}' ),
249 qr/Module name required with -M option\b/,
251 } # disable TODO on VMS
253 is runperl(stderr => 1, prog => '#!perl -m'),
254 qq 'Too late for "-m" option at -e line 1.\n', '#!perl -m';
255 is runperl(stderr => 1, prog => '#!perl -M'),
256 qq 'Too late for "-M" option at -e line 1.\n', '#!perl -M';
261 local $TODO = ''; # these ones should work on VMS
263 # basic perl -V should generate significant output.
264 # we don't test actual format too much since it could change
265 like( runperl( switches => ['-V'] ), qr/(\n.*){20}/,
266 '-V generates 20+ lines' );
268 like( runperl( switches => ['-V'] ),
269 qr/\ASummary of my perl5 .*configuration:/,
272 # lookup a known config var
273 chomp( $r=runperl( switches => ['-V:osname'] ) );
274 is( $r, "osname='$^O';", 'perl -V:osname');
276 # lookup a nonexistent var
277 chomp( $r=runperl( switches => ['-V:this_var_makes_switches_test_fail'] ) );
278 is( $r, "this_var_makes_switches_test_fail='UNKNOWN';",
279 'perl -V:unknown var');
282 # platforms that don't like this quoting can either skip this test
283 # or fix test.pl _quote_args
284 $r = runperl( switches => ['"-V:i\D+size"'] );
285 # should be unlike( $r, qr/^$|not found|UNKNOWN/ );
286 like( $r, qr/^(?!.*(not found|UNKNOWN))./, 'perl -V:re got a result' );
288 # make sure each line we got matches the re
289 ok( !( grep !/^i\D+size=/, split /^/, $r ), '-V:re correct' );
295 local $TODO = ''; # these ones should work on VMS
296 # there are definitely known build configs where this test will fail
297 # DG/UX comes to mind. Maybe we should remove these special cases?
298 my $v = sprintf "%vd", $^V;
299 my $ver = $Config{PERL_VERSION};
300 my $rel = $Config{PERL_SUBVERSION};
301 like( runperl( switches => ['-v'] ),
302 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,
310 local $TODO = ''; # these ones should work on VMS
312 like( runperl( switches => ['-h'] ),
313 qr/Usage: .+(?i:perl(?:$Config{_exe})?).+switches.+programfile.+arguments/,
318 # Tests for switches which do not exist
320 foreach my $switch (split //, "ABbGgHJjKkLNOoPQqRrYyZz123456789_")
322 local $TODO = ''; # these ones should work on VMS
324 like( runperl( switches => ["-$switch"], stderr => 1,
325 prog => 'die q{oops}' ),
326 qr/\QUnrecognized switch: -$switch (-h will show valid options)./,
327 "-$switch correctly unknown" );
330 like( runperl( stderr => 1, prog => "#!perl -$switch" ),
331 qr/^Unrecognized switch: -$switch \(-h will show valid (?x:
332 )options\) at -e line 1\./,
333 "-$switch unrecognised on #! line" );
336 # Tests for unshebangable switches
337 for (qw( e f x E S V )) {
340 prog => "#!perl -$_",
342 is $r, "Can't emulate -$_ on #! line at -e line 1.\n","-$_ on #! line";
348 local $TODO = ''; # these ones should work on VMS
350 sub do_i_unlink { unlink_all("file", "file.bak") }
352 open(FILE, ">file") or die "$0: Failed to create 'file': $!";
353 print FILE <<__EOF__;
360 END { do_i_unlink() }
362 runperl( switches => ['-pi.bak'], prog => 's/foo/bar/', args => ['file'] );
364 open(FILE, "file") or die "$0: Failed to open 'file': $!";
365 chomp(my @file = <FILE>);
368 open(BAK, "file.bak") or die "$0: Failed to open 'file': $!";
369 chomp(my @bak = <BAK>);
373 "bar yada dada:bada bar bing:king kong bar",
376 "foo yada dada:bada foo bing:king kong foo",
380 switches => ['-i.bak -p'],
387 "-i used with no filenames on the command line, reading from STDIN.\n",
388 "warning when no files given"
391 switches => ['-i.bak -p'],
397 is($out2, "", "no warning when files given");
402 $TODO = ''; # the -E tests work on VMS
405 switches => [ '-E', '"say q(Hello, world!)"']
407 is( $r, "Hello, world!\n", "-E say" );
411 switches => [ '-E', '"no warnings q{experimental::smartmatch}; undef ~~ undef and say q(Hello, world!)"']
413 is( $r, "Hello, world!\n", "-E ~~" );
416 switches => [ '-E', '"no warnings q{experimental::smartmatch}; given(undef) {when(undef) { say q(Hello, world!)"}}']
418 is( $r, "Hello, world!\n", "-E given" );
421 switches => [ '-nE', q("} END { say q/affe/") ],
424 is( $r, "affe\n", '-E works outside of the block created by -n' );
427 switches => [ '-E', q("*{'bar'} = sub{}; print 'Hello, world!',qq|\n|;")]
429 is( $r, "Hello, world!\n", "-E does not enable strictures" );
433 $filename = tempfile();
435 open my $f, ">$filename" or skip( "Can't write temp file $filename: $!" );
440 close $f or die "Could not close: $!";
442 progfile => $filename,
444 like( $r, qr/ok/, 'Spaces on the #! line (#30660)' );