[pm-h] print on warning tip

Paul Archer tigger at io.com
Thu Mar 30 10:47:12 PST 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


More information about the Houston mailing list