Getting exceptions from IO::File?
Asked Answered
G

1

7

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.

Gurge answered 19/12, 2013 at 20:51 Comment(1)
Note that file handles are/contain IO::File objects anyway (given a sufficiently modern perl, and maybe an use IO::File to load the methods) – so you can use the builtins like open without giving up the OOP features.Prescott
J
2

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)

Jetty answered 17/1, 2014 at 14:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.