Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Not able Create Backup file when using INPLACE_EDIT ( $^I )

by jesuashok (Curate)
on May 02, 2006 at 04:17 UTC ( #546811=perlquestion: print w/ replies, xml ) Need Help??
jesuashok has asked for the wisdom of the Perl Monks concerning the following question:

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/'); while ( <> ) { s/instance/NEW_PATTERN/; print; }

I got the following error.
Error: Can't rename /home/antony/perl/ 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"

Comment on Not able Create Backup file when using INPLACE_EDIT ( $^I )
Select or Download Code
Re: Not able Create Backup file when using INPLACE_EDIT ( $^I )
by Hue-Bond (Priest) on May 02, 2006 at 04:24 UTC
    Can't rename /home/antony/perl/ 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 )
by davido (Archbishop) on May 02, 2006 at 04:25 UTC

    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/' ); while( <> ) {.....

    That will cause a backup file named "" 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 File::Temp.


      Keep in mind that modern versions of Perl added some extra goodies to the -i value (from perlrun):
      If the extension doesn't contain a "*", then it is appende +d to the end of the current filename as a suffix. If the extension + does contain one or more "*" characters, then each "*" is repla +ced with the current filename. In Perl terms, you could think of t +his as: ($backup = $extension) =~ s/\*/$file_name/g; This allows you to add a prefix to the backup file, instea +d of (or in addition to) a suffix: $ perl -pi'orig_*' -e 's/bar/baz/' fileA # backup t +o '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'

      -- Randal L. Schwartz, Perl hacker
      Be sure to read my standard disclaimer if this is a reply.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://546811]
Approved by Errto
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (8)
As of 2014-09-19 13:12 GMT
Find Nodes?
    Voting Booth?

    How do you remember the number of days in each month?

    Results (138 votes), past polls