Why does my Perl in place of the script exit with zero exit code, even if it doesn't work?

I have a one-line Perl search and replace that looks something like this:

perl -p -i -e 's/foo/bar/' non-existent-file.txt

Since the file does not exist (which is not intentional, but it is part of an automatic build script, so I want to protect against this), Perl completes this error:

Can't open non-existent-file.txt: No such file or directory.

However, the exit code is still zero:

echo $?
0

Am I doing something wrong? Should I modify my script or the way I invoke Perl? I naively believed that since Perl could not find the file, it would exit with non-zero code.

+4
source share
2 answers

You can force an error by dying,

perl -p -i -e 'BEGIN{ -f $ARGV[0] or die"no file" } s/foo/bar/' non-existent-file.txt
+4
source

. perl -pe'...' file1 file2, file2, file1 .

, .

$ perl -i -pe'
   BEGIN {
      $SIG{__WARN__} = sub {
         ++$error;
         print STDERR $_[0];
      };
    }

    END { $? ||= 1 if $error; }

    s/foo/bar/g;
' file1 file2

, file2 , file1 , .

+4

Source: https://habr.com/ru/post/1530193/


All Articles