Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting exceptions from IO::File?

The IO::File, IO::Socket::INET modules have some advantages over directly using perl's built-in IO functions, such as having explicit syntax to flush a handle.

However, they seem to have some disadvantages over the built-in IO functions. For example, as far as I can tell they can't be combined with the autodie module to raise exceptions on failure, so I'm finding myself having to write overall more boilerplate code to handle failures than I was with built-in functions.

Is there a way to combine the two, or some other modules that have the combined functionality? I've noticed some limited-purpose IO modules, such as File::Slurp, do allow more flexible error handling.

I'm writing module code, and ideally, the solution should work all the way back to perl 5.10.0.

like image 872
John Dough Avatar asked Dec 19 '13 20:12

John Dough


1 Answers

Have you looked at Path::Tiny? The syntax is different but it does throw exceptions.

E.G.

use Path::Tiny;
path('/non/existent/file')->openr;

will die with a Path::Tiny::Exception object (assuming you don't have such a file)

like image 119
Thomas Erskine Avatar answered Oct 11 '22 14:10

Thomas Erskine