diotalevi has asked for the wisdom of the Perl Monks concerning the following question:

It occurred to me that in some cases I'd like to really be aware of any uninitialized variables being interpolated. I'd probably want to even promote that to a die/carp-type exception. So - is there any way to make  'text' . undef . ' string' throw an error instead of a warning?

Replies are listed 'Best First'.
Re: Fatal warnings?
by robartes (Priest) on Mar 26, 2003 at 14:33 UTC
    There's fatal warnings in warnings.pm:
    use warnings FATAL => qw(uninitialized); print "Hi there, Mr. $name\n"; __END__ Use of uninitialized value in concatenation (.) or string at warnfatal +.pl line 5.
    Catching it is just an eval away :).

    CU
    Robartes-

      Great, thanks! I knew I could selectively disable certain types of warnings via the warnings pragma but didn't know I could make it do other things as well.

Re: Fatal warnings?
by Mr. Muskrat (Canon) on Mar 26, 2003 at 14:44 UTC
    What if you don't want to use warnings (or maybe it's not available for a particular build) then you could always add a
    local $SIG{__WARN__} = sub { die $_[0] if $_[0] =~ /uninitialized/; };
    to the top of your script or perhaps a
    BEGIN { $SIG{__WARN__} = sub { die $_[0] if $_[0] =~ /uninitialized/; print STDERR $_[0]; }; }.

    Updated 2nd code snippet to output non-fatal warnings.

Re: Fatal warnings?
by Bilbo (Pilgrim) on Mar 26, 2003 at 14:33 UTC

    If you are currently enabling warnings using -w on your #!/usr/bin/perl -w line then you might want to switch to use warnings;. This gives you a lot more control, including the ability to ignore types of warnings and make others fatal. See perllexwarn.

Re: Fatal warnings?
by davis (Vicar) on Mar 26, 2003 at 14:38 UTC
    From the camel p.g. 673:
    use warnings qw/FATAL all/; use strict; print undef; print "hello\n";
    cheers
    davis
    It's not easy to juggle a pregnant wife and a troubled child, but somehow I managed to fit in eight hours of TV a day.