Skip Menu |

This queue is for tickets about the File-Slurp CPAN distribution.

Report information
The Basics
Id: 132443
Status: open
Priority: 0/
Queue: File-Slurp

People
Owner: cwhitener [...] gmail.com
Requestors: SVW [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: (no value)



Subject: write_file: Is $! reliable?
I would like to implement my own error handling when writing files. Therefore I have set err_mode to "quiet". Im asking myself if I can rely on $!, if write_file returns undef due to an error. If the answer is yes an updated documentation that could cover the request #129262 too, would be very much appreciated.
I'd prefer to see something akin to: use strict; use warnings; use Syntax::Keyword::Try; use File::Spec qw(write_file); try { write_file('/some/nonexistent/path', ['line1', 'line2']); # it's croak by default. } catch { warn "Oh, crap. we couldn't write there! $@"; } Allow things to die and catch the error. However, I'd strongly suggest Path::Tiny or File::Slurper or Mojo::File at this point. There are a few gotchas with respect to Perl layers that remain that can't easily be fixed here and maintain backwards compatibility. Hope that helps! -- Chase On Tue Apr 28 08:54:06 2020, SVW wrote: Show quoted text
> I would like to implement my own error handling when writing files. > Therefore I have set err_mode to "quiet". Im asking myself if I can > rely on $!, if write_file returns undef due to an error. If the answer > is yes an updated documentation that could cover the request #129262 > too, would be very much appreciated.
Hello Chase, The hints regarding other modules like Path::Tiny, File::Slurper, and Mojo::File are valuable, but the main question has still remained without reply so I have a feeling that you overlooked it. That's why once again: If we want to avoid croak (for whatever reason) and thus call write_file in the quiet mode, can we be sure that the variable $! contains a proper error code / description in case of an I/O error? If yes, an additionla brief remark in the module description would be greatly appreciated! Thanks and Regards, JSF
Quiet mode does nothing. It does not set anything or emit anything. So, one should use a combination of croak and try/catch. $! _might_ be set by a failed open(), sysopen(), read(), rename() etc., but that does not guarantee you that any error causing a failure of write_file() will set any value whatsoever to $!. The value in $! in those cases would be whatever came from the failed call itself and wouldn't have any debug or other info from the File::Slurp module. I explain Exceptions here: https://metacpan.org/pod/Exceptions Thanks, Chase On Wed May 06 10:47:30 2020, JSF wrote: Show quoted text
> Hello Chase, > > The hints regarding other modules like Path::Tiny, File::Slurper, and > Mojo::File are valuable, but the main question has still remained > without reply so I have a feeling that you overlooked it. > That's why once again: > > If we want to avoid croak (for whatever reason) and thus call > write_file in the quiet mode, can we be sure that the variable $! > contains a proper error code / description in case of an I/O error? > > If yes, an additionla brief remark in the module description would be > greatly appreciated! > > Thanks and Regards, > JSF
On Wed May 06 10:47:30 2020, JSF wrote: Show quoted text
> Hello Chase, > > The hints regarding other modules like Path::Tiny, File::Slurper, and > Mojo::File are valuable, but the main question has still remained > without reply so I have a feeling that you overlooked it.
No, he didn't. $! is not reliable, that's not a documented API, and the try/catch (or eval) approach chase gave you *is* the answer to your question. Whatever reason you think you have for avoiding doing it properly is not a good reason, and if you actually care about errors then you should fix your code as chase described. I'd be happy to explain in more detail if you'd provided any detail at all as to why you're trying to do this, but you didn't, so currently all I can really say is "that's wrong, don't, listen to chase."