Skip to content

Instantly share code, notes, and snippets.

@fgabolde
Created November 29, 2012 14:18
Show Gist options
  • Save fgabolde/4169386 to your computer and use it in GitHub Desktop.
Save fgabolde/4169386 to your computer and use it in GitHub Desktop.
Catching warnings
#!perl
use strict;
use warnings;
use 5.010;
my $escape = 0;
while (not $escape) {
# Signal handler style
local $SIG{__WARN__} = sub { say 'caught you'; $escape = 1 };
my $substring = rand > 0.9 ? undef : "moo";
say "Now substring is $substring";
}
while (1) {
# exception style
use warnings FATAL => qw/uninitialized/;
my $substring = rand > 0.9 ? undef : "moo";
eval {
say "Now substring is $substring";
};
if (my $fatalled_warning = $@) {
say "caught you: $fatalled_warning";
last;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment