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


In reply to Re: Make Spreadsheet::ParseXLSX be quiet about errors by kcott
in thread Make Spreadsheet::ParseXLSX be quiet about errors by leszekdubiel

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.