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.
12 BEGIN { require "./test.pl"; }
18 # due to a bug in VMS's piping which makes it impossible for runperl()
19 # to emulate echo -n (ie. stdin always winds up with a newline), these
20 # tests almost totally fail.
21 $TODO = "runperl() unable to emulate echo -n due to pipe bug" if $^O eq 'VMS';
25 END { unlink @tmpfiles }
30 switches => [ '-0', ],
31 stdin => 'foo\0bar\0baz\0',
32 prog => 'print qq(<$_>) while <>',
34 is( $r, "<foo\0><bar\0><baz\0>", "-0" );
37 switches => [ '-l', '-0', '-p' ],
38 stdin => 'foo\0bar\0baz\0',
41 is( $r, "foo\nbar\nbaz\n", "-0 after a -l" );
44 switches => [ '-0', '-l', '-p' ],
45 stdin => 'foo\0bar\0baz\0',
48 is( $r, "foo\0bar\0baz\0", "-0 before a -l" );
51 switches => [ sprintf("-0%o", ord 'x') ],
52 stdin => 'fooxbarxbazx',
53 prog => 'print qq(<$_>) while <>',
55 is( $r, "<foox><barx><bazx>", "-0 with octal number" );
58 switches => [ '-00', '-p' ],
59 stdin => 'abc\ndef\n\nghi\njkl\nmno\n\npq\n',
60 prog => 's/\n/-/g;$_.=q(/)',
62 is( $r, 'abc-def--/ghi-jkl-mno--/pq-/', '-00 (paragraph mode)' );
65 switches => [ '-0777', '-p' ],
66 stdin => 'abc\ndef\n\nghi\njkl\nmno\n\npq\n',
67 prog => 's/\n/-/g;$_.=q(/)',
69 is( $r, 'abc-def--ghi-jkl-mno--pq-/', '-0777 (slurp mode)' );
72 switches => [ '-066' ],
73 prog => 'BEGIN { print qq{($/)} } print qq{[$/]}',
75 is( $r, "(\066)[\066]", '$/ set at compile-time' );
79 my $filename = tempfile();
81 local $TODO = ''; # this one works on VMS
83 open my $f, ">$filename" or skip( "Can't write temp file $filename: $!" );
85 BEGIN { print "block 1\n"; }
86 CHECK { print "block 2\n"; }
87 INIT { print "block 3\n"; }
89 END { print "block 5\n"; }
91 close $f or die "Could not close: $!";
94 progfile => $filename,
97 # Because of the stderr redirection, we can't tell reliably the order
98 # in which the output is given
100 $r =~ /$filename syntax OK/
101 && $r =~ /\bblock 1\b/
102 && $r =~ /\bblock 2\b/
103 && $r !~ /\bblock 3\b/
104 && $r !~ /\bblock 4\b/
105 && $r !~ /\bblock 5\b/,
113 switches => [ sprintf("-l%o", ord 'x') ],
114 prog => 'print for qw/foo bar/'
116 is( $r, 'fooxbarx', '-l with octal number' );
121 switches => [ '-s' ],
122 prog => 'for (qw/abc def ghi/) {print defined $$_ ? $$_ : q(-)}',
123 args => [ '--', '-abc=2', '-def', ],
125 is( $r, '21-', '-s switch parsing' );
127 $filename = tempfile();
129 open my $f, ">$filename" or skip( "Can't write temp file $filename: $!" );
132 BEGIN { print $x,$y; exit }
134 close $f or die "Could not close: $!";
136 progfile => $filename,
137 args => [ '-x=foo -y' ],
139 is( $r, 'foo1', '-s on the shebang line' );
142 # Bug ID 20011106.084
143 $filename = tempfile();
145 open my $f, ">$filename" or skip( "Can't write temp file $filename: $!" );
148 BEGIN { print $x; exit }
150 close $f or die "Could not close: $!";
152 progfile => $filename,
153 args => [ '-x=foo' ],
155 is( $r, 'foo', '-sn on the shebang line' );
158 # Tests for -m and -M
160 my $package = tempfile();
161 $filename = "$package.pm";
163 open my $f, ">$filename" or skip( "Can't write temp file $filename: $!",4 );
164 print $f <<"SWTESTPM";
166 sub import { print map "<\$_>", \@_ }
169 close $f or die "Could not close: $!";
171 switches => [ "-M$package" ],
174 is( $r, "<$package>", '-M' );
176 switches => [ "-M$package=foo" ],
179 is( $r, "<$package><foo>", '-M with import parameter' );
181 switches => [ "-m$package" ],
186 local $TODO = ''; # this one works on VMS
190 switches => [ "-m$package=foo,bar" ],
193 is( $r, "<$package><foo><bar>", '-m with import parameters' );
194 push @tmpfiles, $filename;
196 is( runperl( switches => [ '-MTie::Hash' ], stderr => 1, prog => 1 ),
197 '', "-MFoo::Bar allowed" );
199 like( runperl( switches => [ "-M:$package" ], stderr => 1,
200 prog => 'die "oops"' ),
201 qr/Invalid module name [\w:]+ with -M option\b/,
202 "-M:Foo not allowed" );
204 like( runperl( switches => [ '-mA:B:C' ], stderr => 1,
205 prog => 'die "oops"' ),
206 qr/Invalid module name [\w:]+ with -m option\b/,
207 "-mFoo:Bar not allowed" );
209 like( runperl( switches => [ '-m-A:B:C' ], stderr => 1,
210 prog => 'die "oops"' ),
211 qr/Invalid module name [\w:]+ with -m option\b/,
212 "-m-Foo:Bar not allowed" );
214 like( runperl( switches => [ '-m-' ], stderr => 1,
215 prog => 'die "oops"' ),
216 qr/Module name required with -m option\b/,
219 like( runperl( switches => [ '-M-=' ], stderr => 1,
220 prog => 'die "oops"' ),
221 qr/Module name required with -M option\b/,
228 local $TODO = ''; # these ones should work on VMS
230 # basic perl -V should generate significant output.
231 # we don't test actual format too much since it could change
232 like( runperl( switches => ['-V'] ), qr/(\n.*){20}/,
233 '-V generates 20+ lines' );
235 like( runperl( switches => ['-V'] ),
236 qr/\ASummary of my perl5 .*configuration:/,
239 # lookup a known config var
240 chomp( $r=runperl( switches => ['-V:osname'] ) );
241 is( $r, "osname='$^O';", 'perl -V:osname');
243 # lookup a nonexistent var
244 chomp( $r=runperl( switches => ['-V:this_var_makes_switches_test_fail'] ) );
245 is( $r, "this_var_makes_switches_test_fail='UNKNOWN';",
246 'perl -V:unknown var');
249 # platforms that don't like this quoting can either skip this test
250 # or fix test.pl _quote_args
251 $r = runperl( switches => ['"-V:i\D+size"'] );
252 # should be unlike( $r, qr/^$|not found|UNKNOWN/ );
253 like( $r, qr/^(?!.*(not found|UNKNOWN))./, 'perl -V:re got a result' );
255 # make sure each line we got matches the re
256 ok( !( grep !/^i\D+size=/, split /^/, $r ), '-V:re correct' );
262 local $TODO = ''; # these ones should work on VMS
264 my $v = sprintf "%vd", $^V;
265 like( runperl( switches => ['-v'] ),
266 qr/This is perl, v$v (?:DEVEL\d+ )?built for \Q$Config{archname}\E.+Copyright.+Larry Wall.+Artistic License.+GNU General Public License/s,
274 local $TODO = ''; # these ones should work on VMS
276 like( runperl( switches => ['-h'] ),
277 qr/Usage: .+(?i:perl(?:$Config{_exe})?).+switches.+programfile.+arguments/,
282 # Tests for switches which do not exist
284 foreach my $switch (split //, "ABbGgHJjKkLNOoPQqRrYyZz123456789_")
286 local $TODO = ''; # these ones should work on VMS
288 like( runperl( switches => ["-$switch"], stderr => 1,
289 prog => 'die "oops"' ),
290 qr/\QUnrecognized switch: -$switch (-h will show valid options)./,
291 "-$switch correctly unknown" );
298 local $TODO = ''; # these ones should work on VMS
300 sub do_i_unlink { 1 while unlink("file", "file.bak") }
302 open(FILE, ">file") or die "$0: Failed to create 'file': $!";
303 print FILE <<__EOF__;
310 END { do_i_unlink() }
312 runperl( switches => ['-pi.bak'], prog => 's/foo/bar/', args => ['file'] );
314 open(FILE, "file") or die "$0: Failed to open 'file': $!";
315 chomp(my @file = <FILE>);
318 open(BAK, "file.bak") or die "$0: Failed to open 'file': $!";
319 chomp(my @bak = <BAK>);
323 "bar yada dada:bada bar bing:king kong bar",
326 "foo yada dada:bada foo bing:king kong foo",
333 switches => [ '-E', '"say q(Hello, world!)"']
335 is( $r, "Hello, world!\n", "-E say" );
339 switches => [ '-E', '"undef ~~ undef and say q(Hello, world!)"']
341 is( $r, "Hello, world!\n", "-E ~~" );
344 switches => [ '-E', '"given(undef) {when(undef) { say q(Hello, world!)"}}']
346 is( $r, "Hello, world!\n", "-E given" );
349 switches => [ '-nE', q("} END { say q/affe/") ],
352 is( $r, "affe\n", '-E works outside of the block created by -n' );