Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Catch warning from external module

by Athanasius (Archbishop)
on Jul 18, 2014 at 16:00 UTC ( [id://1094224]=note: print w/replies, xml ) Need Help??


in reply to Catch warning from external module

Hello p4luch, and welcome to the Monastery!

Since warn prints to STDERR, you may find it sufficient to simply catch any output to STDERR and use that. For this purpose, the module Capture::Tiny provides a subroutine called capture_stderr:

# Main script (.pl file) use strict; use warnings; use Capture::Tiny 'capture_stderr'; use MyMod; my $warnings1 = capture_stderr { MyMod::run_warnings(); }; print "\nCaptured the following warnings:\n$warnings1" if $warnings1; my $warnings2 = capture_stderr { MyMod::run_no_warnings(); }; print "\nCaptured the following warnings:\n$warnings2" if $warnings2; ###################################################################### +######### # Test module (.pm file) package MyMod; use strict; use warnings; sub run_warnings { print "\nBeginning MyMod::run_warnings()\n"; warn 'This is your first warning!'; warn 'This is your second warning!'; warn 'This is your last warning!'; print "Leaving MyMod::run_warnings()\n"; } sub run_no_warnings { print "\nMyMod::run_no_warnings ... done\n"; } 1;

Output:

1:56 >perl 940_SoPW.pl Beginning MyMod::run_warnings() Leaving MyMod::run_warnings() Captured the following warnings: This is your first warning! at MyMod.pm line 9. This is your second warning! at MyMod.pm line 10. This is your last warning! at MyMod.pm line 11. MyMod::run_no_warnings ... done 1:56 >

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Log In?
Username:
Password:

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

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

    No recent polls found