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,


In reply to Re: Catch warning from external module by Athanasius
in thread Catch warning from external module by p4luch

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.