in reply to Executing "perl -pi -e" in perl script

Also, I'd like to know other ways to accomplish the same task

As you alredy have a Perl script, you could write a routine that does the same as your -e command, plus the file renaming etc. required for the "inplace" edit...

Replies are listed 'Best First'.
Re^2: Executing "perl -pi -e" in perl script
by nbokare (Novice) on Apr 08, 2010 at 11:30 UTC
    I don't want to reinvent the wheel and test it, etc...

      Not much reinventing... You could nicely wrap it up into some sub like inplace_edit() that you then call every time you need "perl -pi -e...":

      #!/usr/bin/perl use strict; use warnings; sub inplace_edit { my ($callback, @files) = @_; return unless ref($callback) eq "CODE"; local $^I = ""; local *ARGV; @ARGV = @files; while (<>) { $callback->(); print; } } inplace_edit( sub { s/(foo|bar)/*\U$1*/; }, "test.dat" ); __END__ $ cat test.cat foo bar baz qux $ ./833501.pl $ cat test.dat *FOO* *BAR* baz qux