Created
November 29, 2012 14:18
-
-
Save fgabolde/4169386 to your computer and use it in GitHub Desktop.
Catching warnings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!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