Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

share|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
add comment

4 Answers

up vote 4 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.

share|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". –  cas 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
add comment

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

share|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
add comment

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;
share|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
add comment

use File::Inplace

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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