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.
Beware though:
$^I
like perl's-i
isn't crashproof.Your edit appear pretty simple. Have you considered simply using sed with the -i option?
Try this, based on the translation of
-i
in perldoc perlrun:use File::Inplace
Also, it has commit/rollback after you've got all your changes in place for a file....