On 12/10/2014 12:32 PM, Martin G. McCormick wrote:
        We've all seen and probably pounded the table a few
times when that "Use of uninitialized value" warning pops up.
        Recently a worker in our group ran a script I wrote and
got that warning due to an unforeseen circumstance.
Unfortunately since the script continues to run, the caller was
prompted to save his changes and he did.
        The result was corruption of data in a file since it was
the missing value that caused the warning in the first place.
        In this case, the problem was annoying but not critical
but it made me wonder if there is a way to cause a perl program
to die if that condition exists? Usually, the uninitialized
value condition is unexpected so it is not likely that one can
anticipate every possibility that could cause it but in this
case, the script would have failed in a safe condition rather
than just grinding on as if all was well.
        Good practices keep this sort of thing to a minimum but
I like to protect people from themselves whenever possible. The
condition that caused this incident was not the caller's fault
but I can't believe that he saw this warning and went ahead to
answer a prompt to finish.

        What would be good would be some sort of flag that a
warning occurred and then one could disable any part of the
program that would be dangerous while allowing safer parts to
run.

Thanks for all constructive suggestions.

Martin McCormick


Just firing from memory here...

  my $warning_occured = 0;
  my $default_warn = $SIG{__WARN__};
  $SIG{__WARN__} = sub {
    $warning_occured = 1;
    $default_warn(@_);
  }

Then in later code:
  if ($warning_occured) {
      # Code to run if a warning occured
  } else {
      # Code to run if no warning occured
  }

Something like that should work, I think.



--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to