Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Turn matching against undef to be deadly

by vsespb (Hermit)
on Sep 26, 2013 at 13:16 UTC ( #1055826=note: print w/ replies, xml ) Need Help??


in reply to Turn matching against undef to be deadly

use strict; use warnings; use Carp; local $SIG{__WARN__} = sub { if ($_[0] =~ /Use of uninitialized value in regexp compilation/) { confess $_[0]; } else { warn @_; } }; print "x".undef, "\n"; printf "matched: %s\n" , ( 'x' =~ undef() ? 'yes' : 'no' ) ; __END__ Use of uninitialized value in concatenation (.) or string at 1.pl line + 14. x Use of uninitialized value in regexp compilation at 1.pl line 16. at 1.pl line 7. main::__ANON__('Use of uninitialized value in regexp compilati +on at 1.pl line...') called at 1.pl line 16


pros: it works.

cons:
1. I think you should at least unit-test it, because warning format can change in different perl versions.
2. Dynamic scoped (this can be worked around with %^H, probably)


Comment on Re: Turn matching against undef to be deadly
Download Code
Re^2: Turn matching against undef to be deadly ( use warnings qw/ FATAL all /;
by Anonymous Monk on Sep 26, 2013 at 13:25 UTC
Re^2: Turn matching against undef to be deadly
by Anonymous Monk on Sep 26, 2013 at 13:37 UTC

    Thanks, vsespb.

    The likelihood of change in warning and having to match against a string are the reasons why I forgot to mention about WARN signal as soon as I had thought about it before posting.

      As another Anonymous Monk pointed above you can use warnings 'FATAIL' for all warnings or only "uninitialized". Lexical scope used. But this will catch other types of "uninitialized" warnings.
      use strict; use warnings; use Carp; use warnings FATAL => 'uninitialized'; printf "matched: %s\n" , ( 'x' =~ undef() ? 'yes' : 'no' ) ; print "end\n";

        See perllexwarn for all the warnings that can be turned into FATAL-ities, etc.     (Update: Oops — didn't notice Anonymonk already linked these!)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1055826]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2014-08-21 03:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (127 votes), past polls