Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Make Spreadsheet::ParseXLSX be quiet about errors ($SIG{__WARN__} )

by LanX (Saint)
on Aug 18, 2022 at 15:42 UTC ( [id://11146230]=note: print w/replies, xml ) Need Help??


in reply to Make Spreadsheet::ParseXLSX be quiet about errors

> How can I grab all erorrs to some variable without polluting stderr?

eval {BLOCK} is only catching fatal errors, either thrown with die or from inside perl.

But these seem to come from warn

One way is to locally use a %SIG -handler.

DEMO:

> perl -de0 ... DB<173> eval { local $SIG{__WARN__} = sub { die $_[0] }; warn "bla" +} or print "<<<$@>>>" <<<bla at (eval 197)[c:/nonBKU/strawberry-perl-5.32.1.1-64bit-portable +/perl/lib/perl5db.pl:738] line 2. >>> DB<174>

There is also a pragma for this redirection - use warnings FATAL =>"all"; °

But with a handler you can also choose to save the warnings to another variable separate from fatals.

FWIW: Another approach would be to locally redirect STDERR to a var...

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

°) updated see https://perldoc.perl.org/warnings#Fatal-Warnings for more

Replies are listed 'Best First'.
Re^2: Make Spreadsheet::ParseXLSX be quiet about errors ($SIG{__WARN__} )
by leszekdubiel (Scribe) on Aug 18, 2022 at 18:42 UTC
    https://stackoverflow.com/a/71683833
    #!/usr/bin/perl use warnings; use strict; print STDERR "STDERR is on.\n"; my ($stderr_fh, $err_msg_ref) = suppress_std_err(); print "STDERR is now off and error messages are being suppressed and s +aved.\n"; print STDERR "I'm an error message.\n"; restore_std_err($stderr_fh); print STDERR "STDERR is back on\n"; print "Errors reported while STDERR was off: $$err_msg_ref\n"; #Saves STDERR in filehandle then turns it off. #Any error messages that occur while STDERR is off are stored for safe +keeping. sub suppress_std_err { my $suppressed_std_error_messages; open (my $saved_std_err_fh, ">&", STDERR); close STDERR; open (STDERR, ">", \$suppressed_std_error_messages); return ($saved_std_err_fh, \$suppressed_std_error_messages); } #Restores STDERR from saved filehandle. sub restore_std_err { my $old_std_err_fh = shift; close STDERR; open (STDERR, ">&", $old_std_err_fh); }
      Yeah, but why?

      Redirecting STDERR is a tricky thing, there are long perldocs on this issue, and I'm not too confident about portability here.

      And you loose the ability to just selectively silence known warnings, while still being alarmed by unknown issues. So you will need to parse that variable afterwards.

      If you really want to capture all errors indiscriminately, better use warnings FATAL => 'all' inside eval (hence only for that lexical scope)

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

      UPDATE

      well, ok. One advantage of redirecting STDERR is that print STDERR will also be caught.

      But the errors you've shown most certainly come from some Carp routines.

      (FWIW you might alsow want to look into Carp for silencing advice)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11146230]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2024-04-26 05:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found