Bug#744751: perl -i ignores errors (!)

Niko Tyni ntyni at debian.org
Tue Apr 15 19:02:56 UTC 2014


On Mon, Apr 14, 2014 at 12:02:51PM +0100, Ian Jackson wrote:
> Package: perl
> Version: 5.14.2-21+deb7u1
> Severity: grave

> mariner:~/junk> perl -i.bak -pe '' nonexistent
> Can't open nonexistent: No such file or directory.
> mariner:~/junk> echo $?
> 0
> mariner:~/junk>
> 
> I appreciate that fixing this is likely to break things which were
> relying on the fault-oblivious[1] approach but overall I think it's
> necessary.

It's not -i that's ignoring errors here, it's -p. The behaviour
triggered by -i isn't reached at all in your example.

A few reduced versions that behave the same way would be

 perl -pe 1 nonexistent
 perl -ne 1 nonexistent
 perl -e '<>' nonexistent

The first two cases are documented in perlrun(1) for -p and -n:
 If a file named by an argument cannot be opened for some reason, Perl
 warns you about it, and moves on to the next file.

Here's a case where perl -i is really 'ignoring' errors:
 % perl -pi.bak -e1 /etc/passwd
 Can't rename /etc/passwd to /etc/passwd.bak: Permission denied, skipping file.
 % echo $?
 0

or, omitting the -p option as above,
 % perl -i.bak -e '<>' /etc/passwd
 Can't rename /etc/passwd to /etc/passwd.bak: Permission denied, skipping file at -e line 1.
 % echo $
 0

The relevant passage in perlrun(1) for -i:
 If, for a given file, Perl is unable to create the backup file as
 specified in the extension then it will skip that file and continue on
 with the next one (if it exists).

I note that these documentation parts don't cover the exit code,
although the 'warns you' wording for -p and -n IMO implies no change to
the exit code.

I can see that it would be useful if an exit code would be propagated
on errors, somewhat like for instance find(1) does. I suppose it
should replace the implicit exit(0) at the end of the program so that
explicit exit() or die() calls and the like in the program would take
precedence. 

This seems like it might be a feasible change for the future, but it
needs to be discussed and implemented upstream. Backward compatibility
is a high priority for perl, so it might well get rejected too.

Anyway, I think this is an enhancement request of the 'wishlist' category,
not a grave bug. It all seems to be working as designed.

>> Incidentalll, autodie doesn't help:

That's <https://rt.cpan.org/Ticket/Display.html?id=74606>.
-- 
Niko Tyni   ntyni at debian.org




More information about the Perl-maintainers mailing list