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,