Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

ignoring warnings in eval

by dbs (Sexton)
on Aug 12, 2011 at 20:39 UTC ( #920085=perlquestion: print w/replies, xml ) Need Help??

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 (Pope) 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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://920085]
Approved by Tanktalus
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2021-04-23 17:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?