I need to edit a file in place within a perl script, so the oft used one liner:

perl -p -e s/<value>/<value>/ig

will not work for this situation. How do I get the same results from within a perl script?

open(CRONTAB, "+<file" || die "...";

while (<CRONTAB>) { 
   if ($_ =~ /value/) {  
      s/^\#+//;  
      print "$_\n";  
   }  
}

My print is showing exactly what I want. It's just the in place edit that I'm looking for.

link|improve this question

1  
You might get better answers on StackOverflow.com If you want it moved, flag it for moderator attention. If you want to maintain ownership, make sure your accounts are associated. – Brad Gilbert Sep 8 '09 at 18:10
feedback

4 Answers

up vote 2 down vote accepted
do {
  local $^I='.bak'; # see perlvar(1)
  local @ARGV=("file");
  while(<>){
    s/<value>/<value>/ig;
    print;
  }
};

Beware though: $^I like perl's -i isn't crashproof.

link|improve this answer
This seems to jive with what I've seen on other sites for a way to in-place edit. I was hoping for something as fast and painfree as the oneliner but this works too. Much thanks. – Shawn Anderson Sep 4 '09 at 22:40
aaarrrghhh. s/jive/jibe/ - "jive" is a style of music or dance. the word you are looking for is "jibe" - "Jibe \Jibe\, v. i. [...] 2. To agree; to harmonize. [Colloq.] --Bartlett.". sorry, just one of the things that bug me like people spelling the two words "a lot" as one word "alot". – Craig Sanders Sep 5 '09 at 2:06
s/// will replace regex. What if I want to delete(not replace with blank line) a line that match the regex. Also How to avoid backups? – balki Jan 27 '11 at 6:14
@balki you should create a new question. – geocar Mar 13 '11 at 1:13
feedback

Your edit appear pretty simple. Have you considered simply using sed with the -i option?

link|improve this answer
I've not considered it. I've never used sed before within a perl script. I'd prefer to do it using perl if possible. I suppose I could backtick the perl -p -e statement in the script too, but it's kind of ugly. Thanks for the thought, though. – Shawn Anderson Sep 4 '09 at 19:38
feedback

Try this, based on the translation of -i in perldoc perlrun:

use strict;
use warnings;
my $filename = "something";
my $extension = '.orig';
while (<>) {
    my $backup;
    if ($extension !~ /\*/) {
        $backup = $filename . $extension;
    }
    else {
        ($backup = $extension) =~ s/\*/$filename/g;
    }
    rename $filename, $backup;
    open my $outfile, '>', $filename;
    select $outfile;
    s/^\#+// if /value/; 
}
continue {
    print;  # this prints to original filename
}
select STDOUT;
link|improve this answer
Thank you for your answer. Most appreciated. – Shawn Anderson Oct 3 '09 at 21:58
1  
Seriously too much work, given that $^I is the precise equivalent for the command line -i. – Randal Schwartz Oct 29 '09 at 1:03
feedback

use File::Inplace

Also, it has commit/rollback after you've got all your changes in place for a file....

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.