3 # Tests for the command-line switches:
4 # -0, -c, -l, -s, -m, -M, -V, -v, -h, -z, -i
5 # Some switches have their own tests, see MANIFEST.
16 # due to a bug in VMS's piping which makes it impossible for runperl()
17 # to emulate echo -n (ie. stdin always winds up with a newline), these
18 # tests almost totally fail.
19 $TODO = "runperl() unable to emulate echo -n due to pipe bug" if $^O eq 'VMS';
23 END { unlink @tmpfiles }
28 switches => [ '-0', ],
29 stdin => 'foo\0bar\0baz\0',
30 prog => 'print qq(<$_>) while <>',
32 is( $r, "<foo\0><bar\0><baz\0>", "-0" );
35 switches => [ '-l', '-0', '-p' ],
36 stdin => 'foo\0bar\0baz\0',
39 is( $r, "foo\nbar\nbaz\n", "-0 after a -l" );
42 switches => [ '-0', '-l', '-p' ],
43 stdin => 'foo\0bar\0baz\0',
46 is( $r, "foo\0bar\0baz\0", "-0 before a -l" );
49 switches => [ sprintf("-0%o", ord 'x') ],
50 stdin => 'fooxbarxbazx',
51 prog => 'print qq(<$_>) while <>',
53 is( $r, "<foox><barx><bazx>", "-0 with octal number" );
56 switches => [ '-00', '-p' ],
57 stdin => 'abc\ndef\n\nghi\njkl\nmno\n\npq\n',
58 prog => 's/\n/-/g;$_.=q(/)',
60 is( $r, 'abc-def--/ghi-jkl-mno--/pq-/', '-00 (paragraph mode)' );
63 switches => [ '-0777', '-p' ],
64 stdin => 'abc\ndef\n\nghi\njkl\nmno\n\npq\n',
65 prog => 's/\n/-/g;$_.=q(/)',
67 is( $r, 'abc-def--ghi-jkl-mno--pq-/', '-0777 (slurp mode)' );
70 switches => [ '-066' ],
71 prog => 'BEGIN { print qq{($/)} } print qq{[$/]}',
73 is( $r, "(\066)[\066]", '$/ set at compile-time' );
77 my $filename = 'swctest.tmp';
79 local $TODO = ''; # this one works on VMS
81 open my $f, ">$filename" or skip( "Can't write temp file $filename: $!" );
83 BEGIN { print "block 1\n"; }
84 CHECK { print "block 2\n"; }
85 INIT { print "block 3\n"; }
87 END { print "block 5\n"; }
89 close $f or die "Could not close: $!";
92 progfile => $filename,
95 # Because of the stderr redirection, we can't tell reliably the order
96 # in which the output is given
98 $r =~ /$filename syntax OK/
99 && $r =~ /\bblock 1\b/
100 && $r =~ /\bblock 2\b/
101 && $r !~ /\bblock 3\b/
102 && $r !~ /\bblock 4\b/
103 && $r !~ /\bblock 5\b/,
106 push @tmpfiles, $filename;
112 switches => [ sprintf("-l%o", ord 'x') ],
113 prog => 'print for qw/foo bar/'
115 is( $r, 'fooxbarx', '-l with octal number' );
120 switches => [ '-s' ],
121 prog => 'for (qw/abc def ghi/) {print defined $$_ ? $$_ : q(-)}',
122 args => [ '--', '-abc=2', '-def', ],
124 is( $r, '21-', '-s switch parsing' );
126 # Bug ID 20011106.084
127 $filename = 'swstest.tmp';
129 open my $f, ">$filename" or skip( "Can't write temp file $filename: $!" );
132 BEGIN { print $x; exit }
134 close $f or die "Could not close: $!";
136 progfile => $filename,
137 args => [ '-x=foo' ],
139 is( $r, 'foo', '-s on the shebang line' );
140 push @tmpfiles, $filename;
143 # Tests for -m and -M
145 $filename = 'swtest.pm';
147 open my $f, ">$filename" or skip( "Can't write temp file $filename: $!",4 );
148 print $f <<'SWTESTPM';
150 sub import { print map "<$_>", @_ }
153 close $f or die "Could not close: $!";
155 switches => [ '-Mswtest' ],
158 is( $r, '<swtest>', '-M' );
160 switches => [ '-Mswtest=foo' ],
163 is( $r, '<swtest><foo>', '-M with import parameter' );
165 switches => [ '-mswtest' ],
170 local $TODO = ''; # this one works on VMS
174 switches => [ '-mswtest=foo,bar' ],
177 is( $r, '<swtest><foo><bar>', '-m with import parameters' );
178 push @tmpfiles, $filename;
184 local $TODO = ''; # these ones should work on VMS
186 # basic perl -V should generate significant output.
187 # we don't test actual format too much since it could change
188 like( runperl( switches => ['-V'] ), qr/(\n.*){20}/,
189 '-V generates 20+ lines' );
191 like( runperl( switches => ['-V'] ),
192 qr/\ASummary of my perl5 .*configuration:/,
195 # lookup a known config var
196 chomp( $r=runperl( switches => ['-V:osname'] ) );
197 is( $r, "osname='$^O';", 'perl -V:osname');
199 # lookup a nonexistent var
200 chomp( $r=runperl( switches => ['-V:this_var_makes_switches_test_fail'] ) );
201 is( $r, "this_var_makes_switches_test_fail='UNKNOWN';",
202 'perl -V:unknown var');
205 # platforms that don't like this quoting can either skip this test
206 # or fix test.pl _quote_args
207 $r = runperl( switches => ['"-V:i\D+size"'] );
208 # should be unlike( $r, qr/^$|not found|UNKNOWN/ );
209 like( $r, qr/^(?!.*(not found|UNKNOWN))./, 'perl -V:re got a result' );
211 # make sure each line we got matches the re
212 ok( !( grep !/^i\D+size=/, split /^/, $r ), '-V:re correct' );
218 local $TODO = ''; # these ones should work on VMS
220 my $v = sprintf "%vd", $^V;
222 like( runperl( switches => ['-v'] ),
223 qr/This is perl, v$v built for $Config{archname}.+Copyright.+Larry Wall.+Artistic License.+GNU General Public License/s,
231 local $TODO = ''; # these ones should work on VMS
233 like( runperl( switches => ['-h'] ),
234 qr/Usage: .+(?i:perl(\.exe)?).+switches.+programfile.+arguments/,
239 # Tests for -z (which does not exist)
242 local $TODO = ''; # these ones should work on VMS
244 like( runperl( switches => ['-z'], stderr => 1 ),
245 qr/\QUnrecognized switch: -z (-h will show valid options)./,
246 '-z correctly unknown' );
253 local $TODO = ''; # these ones should work on VMS
255 sub do_i_unlink { 1 while unlink("file", "file.bak") }
257 open(FILE, ">file") or die "$0: Failed to create 'file': $!";
258 print FILE <<__EOF__;
265 END { do_i_unlink() }
267 runperl( switches => ['-pi.bak'], prog => 's/foo/bar/', args => ['file'] );
269 open(FILE, "file") or die "$0: Failed to open 'file': $!";
270 chomp(my @file = <FILE>);
273 open(BAK, "file.bak") or die "$0: Failed to open 'file': $!";
274 chomp(my @bak = <BAK>);
278 "bar yada dada:bada bar bing:king kong bar",
281 "foo yada dada:bada foo bing:king kong foo",