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

I read http://www.perlmonks.org/index.pl/?node_id=685329 but did not get it to work in my code, so I added the SIG WARN code:
sub _logroll { eval 'use Compress::Zlib'; my $gzip = $@ ? '/usr/bin/gzip' : 'lib'; for my $log_file ( @_ ) { my $rotator = Logfile::Rotate->new( File => $log_file, Count => 15, Dir => $dir, Gzip => $gzip, Flock => 'yes', Persist => 'yes' ); $rotator->rotate(); } $SIG{__WARN__} = sub { open (STDERR, ">/dev/null"); print STDERR $@; } }
But it still prints the "cant find Zlib warnings" in my csv file, which I do not want. Please help! thx!

Replies are listed 'Best First'.
Re: ignoring warnings in eval
by chromatic (Archbishop) on Aug 12, 2011 at 20:55 UTC

    This code adds a warning handler after doing everything else. What happens if you move the warning handler declaration to the start of this function?

    sub _logroll { local $SIG{__WARN__} = sub {}; ... }
Re: ignoring warnings in eval
by ikegami (Patriarch) on Aug 12, 2011 at 20:52 UTC
    sub _logroll { local $SIG{__WARN__} = sub {}; eval 'use Compress::Zlib'; my $gzip = $@ ? '/usr/bin/gzip' : 'lib'; for my $log_file ( @_ ) { my $rotator = Logfile::Rotate->new( File => $log_file, Count => 15, Dir => $dir, Gzip => $gzip, Flock => 'yes', Persist => 'yes' ); $rotator->rotate(); } }
Re: ignoring warnings in eval
by JavaFan (Canon) on Aug 12, 2011 at 21:49 UTC
    Uh, why go through the trouble of re-opening STDERR (to /dev/null) and printing the error on STDERR? If you want nothing to show, then why not just do nothing?

    Also note that your re-opening of STDERR isn't a local action. After the first warning that triggers the handler, no warning or error message will appear, unless STDERR is reopened again.

    Of course, the reason you are seeing the unwanted warning is that you set up the handler afterwards. That's like putting on a bullet proof jacket after you've been shot dead.