in reply to Re: Executing "perl -pi -e" in perl script
in thread Executing "perl -pi -e" in perl script

I don't want to reinvent the wheel and test it, etc...
  • Comment on Re^2: Executing "perl -pi -e" in perl script

Replies are listed 'Best First'.
Re^3: Executing "perl -pi -e" in perl script
by almut (Canon) on Apr 08, 2010 at 15:32 UTC

    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