This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Make utf8::encode respect magic
[perl5.git] / t / io / inplace.t
CommitLineData
fe14fcc3 1#!./perl
99ec4a7e
CB
2use strict;
3require './test.pl';
8d063cd8 4
103a9d15 5$^I = $^O eq 'VMS' ? '_bak' : '.bak';
fe14fcc3 6
13290fcd 7plan( tests => 6 );
8d063cd8 8
62a28c97
NC
9my @tfiles = (tempfile(), tempfile(), tempfile());
10my @tfiles_bak = map "$_$^I", @tfiles;
99ec4a7e 11
62a28c97 12END { unlink_all(@tfiles_bak); }
99ec4a7e
CB
13
14for my $file (@tfiles) {
15 runperl( prog => 'print qq(foo\n);',
16 args => ['>', $file] );
68dc0745 17}
99ec4a7e
CB
18
19@ARGV = @tfiles;
20
8d063cd8
LW
21while (<>) {
22 s/foo/bar/;
23}
24continue {
25 print;
26}
27
99ec4a7e
CB
28is ( runperl( prog => 'print<>;', args => \@tfiles ),
29 "bar\nbar\nbar\n",
30 "file contents properly replaced" );
31
32is ( runperl( prog => 'print<>;', args => \@tfiles_bak ),
33 "foo\nfoo\nfoo\n",
34 "backup file contents stay the same" );
8d063cd8 35
13290fcd
TC
36SKIP:
37{
38 # based on code, dosish and epoc systems can't do no-backup inplace
39 # edits
138b66d5 40 $^O =~ /^(MSWin32|cygwin|uwin|dos|epoc|os2)$/
13290fcd
TC
41 and skip("Can't inplace edit without backups on $^O", 4);
42
43 our @ifiles = ( tempfile(), tempfile(), tempfile() );
44
45 {
46 for my $file (@ifiles) {
47 runperl( prog => 'print qq(bar\n);',
48 args => [ '>', $file ] );
49 }
50
51 local $^I = '';
52 local @ARGV = @ifiles;
53
54 while (<>) {
55 print "foo$_";
56 }
57
58 is(scalar(@ARGV), 0, "consumed ARGV");
59
15fe4dea
CB
60# runperl may quote its arguments, so don't expect to be able
61# to reuse things you send it.
62
63 my @my_ifiles = @ifiles;
64 is( runperl( prog => 'print<>;', args => \@my_ifiles ),
13290fcd
TC
65 "foobar\nfoobar\nfoobar\n",
66 "normal inplace edit");
67 }
68
93f09d7b 69 # test * equivalence RT #70802
13290fcd
TC
70 {
71 for my $file (@ifiles) {
72 runperl( prog => 'print qq(bar\n);',
73 args => [ '>', $file ] );
74 }
75
76 local $^I = '*';
77 local @ARGV = @ifiles;
78
79 while (<>) {
80 print "foo$_";
81 }
82
83 is(scalar(@ARGV), 0, "consumed ARGV");
84
15fe4dea
CB
85 my @my_ifiles = @ifiles;
86 is( runperl( prog => 'print<>;', args => \@my_ifiles ),
13290fcd
TC
87 "foobar\nfoobar\nfoobar\n",
88 "normal inplace edit");
89 }
90
91 END { unlink_all(@ifiles); }
92}