Not able Create Backup file when using INPLACE_EDIT ( $^I )
jesuashok
created: 2006-05-02 00:17:39
Hi all great people,

Today I read about using $^I from the following node in perlmonks, Use $^I (was Re: updating a file)
After that I have written a samle code and checked for the same.

#!/bin/perl

local $^I = "sub/.bak";
local @ARGV = ('/home/antony/perl/l.pl');

while ( <> ) {
        s/instance/NEW_PATTERN/;
        print;
}

I got the following error.
Error:
Can't rename /home/antony/perl/l.pl to /home/antony/perl/l.plsub/.bak: No such file or directory, skipping file at l line 6.
1) what's wrong with the above code? Is the backup should reside in the same directory?

"Keep pouring your ideas"
Re: Not able Create Backup file when using INPLACE_EDIT ( $^I )
created: 2006-05-02 00:24:39
Can't rename /home/antony/perl/l.pl to /home/antony/perl/l.plsub/.bak: No such file or directory

If you pay attention to the error message, you'll see that the value of $^I is being appended to the file currently being edited. Yes, you can have a subdir where to save the backups (in this case, it would be named l.plsub but then you would need one directory per backup. Not much different from having the backup in the same directory as the file itself.

--
David Serrano

Re: Not able Create Backup file when using INPLACE_EDIT ( $^I )
created: 2006-05-02 00:25:22

The problem is that you're trying to create a backup file named ".bak" in a nonexistant directory named "l.plsub".

$^I doesn't really give you a lot of control over the path into which the temp file is going. Do you see what the error message says? It's not encrypted. The backup file is being named "l.plsub/.bak". That means that the $^I flag is just appending its contents to the current <> filename/path. Probably the easiest solution within your existing framework is to simply use $^I as it's intended to be used, like this:

local $^I = ".bak";
local @ARGV = ( '/home/antony/perl/l.pl' );
while( <> ) {.....

That will cause a backup file named "l.pl.bak" to be created, which is, at least, a valid filename under many OS's, and in an existant directory.

If you want finer control over your tempfiles, you might want to use [cpan://File::Temp].


Dave

Re^2: Not able Create Backup file when using INPLACE_EDIT ( $^I )
created: 2006-05-02 01:07:02
Keep in mind that modern versions of Perl added some extra goodies to the -i value (from [doc://perlrun]):
            If the extension doesn't contain a "*", then it is appended to the
            end of the current filename as a suffix.  If the extension does
            contain one or more "*" characters, then each "*" is replaced with
            the current filename.  In Perl terms, you could think of this as:

                ($backup = $extension) =~ s/\*/$file_name/g;

            This allows you to add a prefix to the backup file, instead of (or
            in addition to) a suffix:

                $ perl -pi'orig_*' -e 's/bar/baz/' fileA    # backup to 'orig_fileA'

            Or even to place backup copies of the original files into another
            directory (provided the directory already exists):

                $ perl -pi'old/*.orig' -e 's/bar/baz/' fileA # backup to 'old/fileA.orig'

-- [http://www.stonehenge.com/merlyn/|Randal L. Schwartz, Perl hacker]
Be sure to read [id://205373|my standard disclaimer] if this is a reply.

perlmonks.org content © perlmonks.org and davido, Hue-Bond, jesuashok, merlyn

prlmnks.org © 2006 edmund von der burg (eccles & toad)

v 0.03