develooper Front page | perl.perl5.porters | Postings from March 2012

Re: Error handling in one-liners

Thread Previous | Thread Next
From:
Christoph Bußenius
Date:
March 26, 2012 16:01
Subject:
Re: Error handling in one-liners
Message ID:
4F70C387.6030103@cpan.org
On 03/26/2012 09:35 AM, Rafael Garcia-Suarez wrote:
> This has been discussed before on this list; in my opinion the core
> problem is the silent failure of the closes done by perl at the end of
> the process.

Or no closing at all, in case of stdout.  What would speak against dying 
on implicit close errors?

The point is, I don't really want to tell people, "if you want your 
one-liners to be safe, then either use awk/sed or use <insert long error 
handling code that doubles the size of the script>".  Shouldn't make 
perl things at least about as easy as awk/sed?

Also, why does the following script replace the file with an empty one 
even though it does print an error message if the disk is full?

perl -i -plwe 's/foo/bar/; close ARGVOUT or die $! if eof ARGV' file

Output is: No space left on device at -e line 1, <> line 1041.

Similar results with this:

perl -i -plwe '$|++; s/foo/bar/' file

Also the first version gives an error if the disk is not full:
print() on closed filehandle ARGVOUT, <> line 98.
-p destination: Bad file descriptor

What would be the most elegant and safe version of this one-liner?  Do 
you really need to resort to File::Slurp as Uri suggests?

Cheers,
Christoph

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About