Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Make Spreadsheet::ParseXLSX be quiet about errors

by kcott (Archbishop)
on Aug 19, 2022 at 08:15 UTC ( [id://11146242]=note: print w/replies, xml ) Need Help??


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

G'day leszekdubiel,

"open() - Duping filehandles" explains how you can do this. Here's some example code (stderr_test.pl):

#!/usr/bin/env perl use strict; use warnings; use autodie; use Spreadsheet::ParseXLSX; my $original_stderr; eval { open $original_stderr, '>&', \*STDERR; open STDERR, '>&', \*STDOUT; Spreadsheet::ParseXLSX->new->parse('invalid.xlsx'); 1; } or do { my $err = $@; print $err; }; open STDERR, '>&', $original_stderr; warn "Now back to normal STDERR\n";

I set up a clean directory to show how this works.

ken@titan ~/tmp/pm_11146229 $ ls -l total 1 -rw-r--r-- 1 ken None 0 Aug 19 17:09 invalid.xlsx -rwxr-xr-x 1 ken None 380 Aug 19 18:10 stderr_test.pl ken@titan ~/tmp/pm_11146229 $ ./stderr_test.pl > stdout.out 2> stderr.out ken@titan ~/tmp/pm_11146229 $ ls -l total 6 -rw-r--r-- 1 ken None 0 Aug 19 17:09 invalid.xlsx -rw-r--r-- 1 ken None 26 Aug 19 18:11 stderr.out -rwxr-xr-x 1 ken None 380 Aug 19 18:10 stderr_test.pl -rw-r--r-- 1 ken None 1105 Aug 19 18:11 stdout.out ken@titan ~/tmp/pm_11146229 $ cat stdout.out format error: file is too short at /home/ken/perl5/perlbrew/perls/perl-5.36.0/lib/site_perl/5.36.0/Ar +chive/Zip/Archive.pm line 1031. Archive::Zip::Archive::_findEndOfCentralDirectory(Archive::Zip +::Archive=HASH(0x8000b79b0), IO::File=GLOB(0x800099368)) called at /h +ome/ken/perl5/perlbrew/perls/perl-5.36.0/lib/site_perl/5.36.0/Archive +/Zip/Archive.pm line 761 Archive::Zip::Archive::readFromFileHandle(Archive::Zip::Archiv +e=HASH(0x8000b79b0), IO::File=GLOB(0x800099368), "invalid.xlsx") call +ed at /home/ken/perl5/perlbrew/perls/perl-5.36.0/lib/site_perl/5.36.0 +/Archive/Zip/Archive.pm line 729 Archive::Zip::Archive::read(Archive::Zip::Archive=HASH(0x8000b +79b0), "invalid.xlsx") called at /home/ken/perl5/perlbrew/perls/perl- +5.36.0/lib/site_perl/5.36.0/Spreadsheet/ParseXLSX.pm line 63 Spreadsheet::ParseXLSX::parse(Spreadsheet::ParseXLSX=HASH(0x80 +17a9b88), "invalid.xlsx") called at ./stderr_test.pl line 14 eval {...} called at ./stderr_test.pl line 17 Can't open file 'invalid.xlsx' as a zip file at /home/ken/perl5/perlbr +ew/perls/perl-5.36.0/lib/site_perl/5.36.0/Spreadsheet/ParseXLSX.pm li +ne 63. ken@titan ~/tmp/pm_11146229 $ cat stderr.out Now back to normal STDERR ken@titan ~/tmp/pm_11146229 $

— Ken

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-25 20:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found