Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Catch warning from external module

by p4luch (Initiate)
on Jul 18, 2014 at 08:42 UTC ( [id://1094180]=perlquestion: print w/replies, xml ) Need Help??

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

Hello, I'm quite new to programming in Perl and need help. I have script which downloads data from web server through function from external module. In case of errors (like 500 read timeout) function generates warning. Is it possible to catch this warning in my main script? I've tried to use eval{} on function call but it didn't seem to work. Sorry for not specyfing. I'm using BioMart Perl API to download data from EnsEMBL BioMart mirror. And code for function which downloads data by default isn't avaible for user. Piece of code which generetes warning looks like that:

my $ua = LWP::UserAgent->new; $ua->timeout(20); # default is 180 seconds $ua->proxy( ['http', 'https'], $self->proxy ) if defined $self->pr +oxy; my $response = $ua->request($request); my @results; if ($response->is_success) { my @arr=split(/\n/,$response->content); # much neater to use 'cont +ent' instead of 'as_string', we don't need to explicitly ignore heade +r part of the http response any more. foreach my $el(@arr){ $logger->warn("RESPONSE: $el"); push (@results,$el); } } else { warn ("\n\nProblems with the web server: ". $response->status_line."\n\n"); }
And I'm intrested "catching" warning from last warn().But I have to do it in my main script.

Replies are listed 'Best First'.
Re: Catch warning from external module
by marto (Cardinal) on Jul 18, 2014 at 08:47 UTC

      Put this at the very beginning of your code before anything else

      BEGIN { $| = 1; open (STDERR, ">&STDOUT"); print qq~Content-type: text/html\n\n~; }
      Works for most things that matter...
Re: Catch warning from external module
by Anonymous Monk on Jul 18, 2014 at 08:50 UTC

    I've tried to use eval{} on function call but it didn't seem to work.

    What does the documentation say, how are you supposed to tell when this function fails, and how are you supposed to tell why it fails (an error variable/attribute/method)?

    Yes, you can trap warnings in perl, but its not as convenient as eval

    local $SIG{__WARN__} = \&catch_warnings; emit_some_warnings(); # catch_warnings($msg) sub catch_warnings { my( $msg ) = @_; print "I caught this warning $msg\n"; }

    Whatever this external module is, you should study its documentation :)

Re: Catch warning from external module
by Athanasius (Archbishop) on Jul 18, 2014 at 16:00 UTC

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2024-04-23 23:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found