sub ErrorHandler(&) { my $block = shift; my $output = ''; my $warnings = ''; { local *STDOUT; tie *STDOUT, 'Some::Class::Collecting::The::Output', \$output; local $SIG{__WARN__} = sub { local $_ = shift; $warnings .= $_; $warnings .= "\n" unless (/\n$/); }; eval { $block->(); } } if ($@) { # handle the errors } elsif ($warnings) { # handle the warnings } else { print $output; } }