summaryrefslogtreecommitdiff
path: root/t/io/inplace.t
blob: a27881895ba384215529a6e75db67a2321e735e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!./perl
use strict;
require './test.pl';

$^I = $^O eq 'VMS' ? '_bak' : '.bak';

plan( tests => 6 );

my @tfiles     = (tempfile(), tempfile(), tempfile());
my @tfiles_bak = map "$_$^I", @tfiles;

END { unlink_all(@tfiles_bak); }

for my $file (@tfiles) {
    runperl( prog => 'print qq(foo\n);', 
             args => ['>', $file] );
}

@ARGV = @tfiles;

while (<>) {
    s/foo/bar/;
}
continue {
    print;
}

is ( runperl( prog => 'print<>;', args => \@tfiles ), 
     "bar\nbar\nbar\n", 
     "file contents properly replaced" );

is ( runperl( prog => 'print<>;', args => \@tfiles_bak ), 
     "foo\nfoo\nfoo\n", 
     "backup file contents stay the same" );

SKIP:
{
    # based on code, dosish and epoc systems can't do no-backup inplace
    # edits
    $^O =~ /^(MSWin32|cygwin|uwin|dos|epoc|os2)$/
	and skip("Can't inplace edit without backups on $^O", 4);
    
    our @ifiles = ( tempfile(), tempfile(), tempfile() );
    
    {
	for my $file (@ifiles) {
	    runperl( prog => 'print qq(bar\n);',
		     args => [ '>', $file ] );
	}
	
	local $^I = '';
    local @ARGV = @ifiles;
	
	while (<>) {
	    print "foo$_";
	}
	
	is(scalar(@ARGV), 0, "consumed ARGV");
	
#	runperl may quote its arguments, so don't expect to be able
#	to reuse things you send it.

	my @my_ifiles = @ifiles;
	is( runperl( prog => 'print<>;', args => \@my_ifiles ),
	    "foobar\nfoobar\nfoobar\n",
	    "normal inplace edit");
    }
    
    # test * equivalency RT #70802
    {
	for my $file (@ifiles) {
	    runperl( prog => 'print qq(bar\n);',
		     args => [ '>', $file ] );
	}
	
	local $^I = '*';
	local @ARGV = @ifiles;
	
	while (<>) {
	    print "foo$_";
	}
	
	is(scalar(@ARGV), 0, "consumed ARGV");
	
	my @my_ifiles = @ifiles;
	is( runperl( prog => 'print<>;', args => \@my_ifiles ),
	    "foobar\nfoobar\nfoobar\n",
	    "normal inplace edit");
    }
    
    END { unlink_all(@ifiles); }
}