user3768541
user3768541

Reputation: 119

Perl on windows wont let me replace contents inside a file

So I have a script that renames some contents inside a file

local @ARGV = 'myfile';
local $^I = '';
while (<>) {
  s/oldtext/newttext/g;
 print;
}

Whenever i run it locally on my macbook it works, but when i run it locally on windows using cmd it gives me the following error: Can't do inplace edit without backup at ..

Anyone know how to fix this?

Upvotes: 0

Views: 392

Answers (1)

Miller
Miller

Reputation: 35198

Windows requires that you specify a backup extension.

Just specify a value for $^I and then optionally delete the backup after processing the file.

local @ARGV = 'myfile';
local $^I = '.bak';
while (<>) {
    s/oldtext/newttext/g;
    print;
}
unlink "myfile$^I"; # Optionally delete backup

Upvotes: 4

Related Questions