Loading ...
Sorry, an error occurred while loading the content.
 

print on warning tip

Expand Messages
  • Paul Archer
    I had a problem where I needed to see what a few values where if I got a use of unititalized value warning, but I m parsing a logfile that can easily be over
    Message 1 of 1 , Mar 30, 2006
      I had a problem where I needed to see what a few values where if I got a
      'use of unititalized value' warning, but I'm parsing a logfile that can
      easily be over a million lines long (Solaris' nfslog), so I couldn't really
      just print everything a million times.

      I found this is perfaq8:
      BEGIN {
      $SIG{__WARN__} = sub{ print STDERR "Perl: ", @_; };
      $SIG{__DIE__} = sub{ print STDERR "Perl: ", @_; exit 1};
      }

      This hints at an answer, but the problem is that I need to print a lexically
      scoped variable.
      So I changed things up a bit:

      our $warnflag;
      BEGIN {
      $SIG{__WARN__} = sub{ print STDERR "Perl: ", @_; $warnflag=1;};
      }

      And in my code:
      print "username is $username\tid is $id, UID is $UID\n" if $warnflag;
      $warnflag =0 if $warnflag;


      Hope that helps someone. And if there's a better way to do this, I'd love to
      see that, too.

      Paul



      ------------------------------------------------------------
      "They're willing to kill people. That makes them dangerous."
      --News announcer, about South American drug lords
      ------------------------------------------------------------
    Your message has been successfully submitted and would be delivered to recipients shortly.