nbokare has asked for the wisdom of the Perl Monks concerning the following question:

I know the header sounds funny.

I want to replace all instances of a particular string in a file from a script. To do this I'm using "perl -pi -e" from a perl script which does a lot of things internally.

Now I want to check the return status of the "perl -pi -e" command. To test this i removed the permissions on the destination file and then ran the script. The system command still returns me a status "0" which implies that the command ran successfully. I think the command should have failed and given some other return status. May be I'm missing something here.

Can you let me know how to get the return status of the "perl -pi -e" command?

Also, I'd like to know other ways to accomplish the same task instead of doing a system call for "perl -pi -e".

Thanks, Nikhil

Replies are listed 'Best First'.
Re: Executing "perl -pi -e" in perl script
by Corion (Patriarch) on Apr 08, 2010 at 11:22 UTC

    A quick way to turn a oneliner into the equivalent Perl script is to run:

    perl -MO=Deparse -pi -e ...
Re: Executing "perl -pi -e" in perl script
by Anonymous Monk on Apr 08, 2010 at 11:33 UTC
    Its documented in perlrun under -i[extension]. Here is an example (based on this)
    #!/usr/bin/perl -- use strict; use warnings; use open qw' :std :encoding(UTF-8)'; Main( @ARGV ); exit( 0 ); sub Main { local ( *ARGV, $^I ); @ARGV = @_; $^I = '.orig'; while(<>){ #~ U+2018 (8216), U+2019 (8217) &lsquo; &rsquo; Single quotes +(left and right) #~ U+201C (8220), U+201D (8221) &ldquo; &rdquo; Double quotes +(left and right) #~ U+201B (HTML: &#8219;) – single high-reversed-9, or single reversed + comma, quotation mark (This is sometimes used to show dropped charac +ters at the end of words, such as goin? instead of using goin‘, goin +’, goin`, or goin'[citation needed]) #~ U+201F (HTML: &#8223;) – double high-reversed-9, or double reversed + comma, quotation mark y/\x{2018}\x{2019}\x{201B}\x{201C}\x{201D}\x{201F}/'''"""/; print; } } __END__
Re: Executing "perl -pi -e" in perl script
by almut (Canon) on Apr 08, 2010 at 11:27 UTC
    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...

      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
Re: Executing "perl -pi -e" in perl script
by crashtest (Curate) on Apr 08, 2010 at 17:13 UTC

    The monastary has seen a previous thread on the same topic: -pi -e argv repeater. You'll find some interesting discussion in there.