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.
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 = 'swctest.tmp';
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/,
108 push @tmpfiles, $filename;
114 switches => [ sprintf("-l%o", ord 'x') ],
115 prog => 'print for qw/foo bar/'
117 is( $r, 'fooxbarx', '-l with octal number' );
122 switches => [ '-s' ],
123 prog => 'for (qw/abc def ghi/) {print defined $$_ ? $$_ : q(-)}',
124 args => [ '--', '-abc=2', '-def', ],
126 is( $r, '21-', '-s switch parsing' );
128 # Bug ID 20011106.084
129 $filename = 'swstest.tmp';
131 open my $f, ">$filename" or skip( "Can't write temp file $filename: $!" );
134 BEGIN { print $x; exit }
136 close $f or die "Could not close: $!";
138 progfile => $filename,
139 args => [ '-x=foo' ],
141 is( $r, 'foo', '-s on the shebang line' );
142 push @tmpfiles, $filename;
145 # Tests for -m and -M
147 $filename = 'swtest.pm';
149 open my $f, ">$filename" or skip( "Can't write temp file $filename: $!",4 );
150 print $f <<'SWTESTPM';
152 sub import { print map "<$_>", @_ }
155 close $f or die "Could not close: $!";
157 switches => [ '-Mswtest' ],
160 is( $r, '<swtest>', '-M' );
162 switches => [ '-Mswtest=foo' ],
165 is( $r, '<swtest><foo>', '-M with import parameter' );
167 switches => [ '-mswtest' ],
172 local $TODO = ''; # this one works on VMS
176 switches => [ '-mswtest=foo,bar' ],
179 is( $r, '<swtest><foo><bar>', '-m with import parameters' );
180 push @tmpfiles, $filename;
186 local $TODO = ''; # these ones should work on VMS
188 # basic perl -V should generate significant output.
189 # we don't test actual format too much since it could change
190 like( runperl( switches => ['-V'] ), qr/(\n.*){20}/,
191 '-V generates 20+ lines' );
193 like( runperl( switches => ['-V'] ),
194 qr/\ASummary of my perl5 .*configuration:/,
197 # lookup a known config var
198 chomp( $r=runperl( switches => ['-V:osname'] ) );
199 is( $r, "osname='$^O';", 'perl -V:osname');
201 # lookup a nonexistent var
202 chomp( $r=runperl( switches => ['-V:this_var_makes_switches_test_fail'] ) );
203 is( $r, "this_var_makes_switches_test_fail='UNKNOWN';",
204 'perl -V:unknown var');
207 # platforms that don't like this quoting can either skip this test
208 # or fix test.pl _quote_args
209 $r = runperl( switches => ['"-V:i\D+size"'] );
210 # should be unlike( $r, qr/^$|not found|UNKNOWN/ );
211 like( $r, qr/^(?!.*(not found|UNKNOWN))./, 'perl -V:re got a result' );
213 # make sure each line we got matches the re
214 ok( !( grep !/^i\D+size=/, split /^/, $r ), '-V:re correct' );
220 local $TODO = ''; # these ones should work on VMS
222 my $v = sprintf "%vd", $^V;
223 like( runperl( switches => ['-v'] ),
224 qr/This is perl, v$v built for $Config{archname}.+Copyright.+Larry Wall.+Artistic License.+GNU General Public License/s,
232 local $TODO = ''; # these ones should work on VMS
234 like( runperl( switches => ['-h'] ),
235 qr/Usage: .+(?i:perl(?:$Config{_exe})?).+switches.+programfile.+arguments/,
240 # Tests for -z (which does not exist)
243 local $TODO = ''; # these ones should work on VMS
245 like( runperl( switches => ['-z'], stderr => 1 ),
246 qr/\QUnrecognized switch: -z (-h will show valid options)./,
247 '-z correctly unknown' );
254 local $TODO = ''; # these ones should work on VMS
256 sub do_i_unlink { 1 while unlink("file", "file.bak") }
258 open(FILE, ">file") or die "$0: Failed to create 'file': $!";
259 print FILE <<__EOF__;
266 END { do_i_unlink() }
268 runperl( switches => ['-pi.bak'], prog => 's/foo/bar/', args => ['file'] );
270 open(FILE, "file") or die "$0: Failed to open 'file': $!";
271 chomp(my @file = <FILE>);
274 open(BAK, "file.bak") or die "$0: Failed to open 'file': $!";
275 chomp(my @bak = <BAK>);
279 "bar yada dada:bada bar bing:king kong bar",
282 "foo yada dada:bada foo bing:king kong foo",