Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Trapping warnings

Reply
Thread Tools

Trapping warnings

 
 
Raj
Guest
Posts: n/a
 
      05-06-2004
Hi,

I am using some code to talk to a third party server via an HTTPS GET.

The problem I have is that when the server is unavailable, the object method
thrown an error of the form:

"(Use of uninitialized value in split at
/opt/perl5/lib/site_perl/5.6.1/sun4-solaris-64int/eo.pm line 151.)"

I need to be able to suppress these warnings, but trap them so that I can
report them back via my code gracefully.

I know I can trap die using eval() but what can I do to deal with the above
warning?

Thanks in advance!

Regards,
Raj


 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      05-06-2004
"Raj" <> wrote in
news:c7dqde$fbs$1$:

> Hi,
>
> I am using some code to talk to a third party server via an HTTPS GET.
>
> The problem I have is that when the server is unavailable, the object
> method thrown an error of the form:
>
> "(Use of uninitialized value in split at
> /opt/perl5/lib/site_perl/5.6.1/sun4-solaris-64int/eo.pm line 151.)"
>
> I need to be able to suppress these warnings, but trap them so that I
> can report them back via my code gracefully.
>
> I know I can trap die using eval() but what can I do to deal with the
> above warning?


Well, how about not using split if there is nothing to split?

--
A. Sinan Unur
(reverse each component for email address)
 
Reply With Quote
 
 
 
 
GM
Guest
Posts: n/a
 
      05-06-2004
A. Sinan Unur wrote:
> "Raj" <> wrote in
> news:c7dqde$fbs$1$:
>
>
>>Hi,
>>
>>I am using some code to talk to a third party server via an HTTPS GET.
>>
>>The problem I have is that when the server is unavailable, the object
>>method thrown an error of the form:
>>
>>"(Use of uninitialized value in split at
>>/opt/perl5/lib/site_perl/5.6.1/sun4-solaris-64int/eo.pm line 151.)"
>>
>>I need to be able to suppress these warnings, but trap them so that I
>>can report them back via my code gracefully.
>>
>>I know I can trap die using eval() but what can I do to deal with the
>>above warning?

>
>
> Well, how about not using split if there is nothing to split?
>


Ditto!

You should be looking at what you get back from the server before trying
to do anything with it.
 
Reply With Quote
 
Raj
Guest
Posts: n/a
 
      05-07-2004
"GM" <> wrote in message news:1Gumc.43088$I%1.2791545@attbi_s51...
> A. Sinan Unur wrote:
> > "Raj" <> wrote in
> > news:c7dqde$fbs$1$:
> >
> >
> >>Hi,
> >>
> >>I am using some code to talk to a third party server via an HTTPS GET.
> >>
> >>The problem I have is that when the server is unavailable, the object
> >>method thrown an error of the form:
> >>
> >>"(Use of uninitialized value in split at
> >>/opt/perl5/lib/site_perl/5.6.1/sun4-solaris-64int/eo.pm line 151.)"
> >>
> >>I need to be able to suppress these warnings, but trap them so that I
> >>can report them back via my code gracefully.
> >>
> >>I know I can trap die using eval() but what can I do to deal with the
> >>above warning?

> >
> >
> > Well, how about not using split if there is nothing to split?
> >

>
> Ditto!
>
> You should be looking at what you get back from the server before trying
> to do anything with it.


But it is someone else's PM that I am using:

my $e = GL::eo::new (
host=>"$host", key=>"$key",
) || die("500");

my $path = $cfg->{GL}->{Path};

my ($p,$r,$h) = $e->do_request('GET', $path, undef); # this is the method
in which the split occurs

I cannot change the code for the do_request() method. Can I stop it from
throwing this error?

Regards (and thanks)

Raj


 
Reply With Quote
 
Thomas Kratz
Guest
Posts: n/a
 
      05-07-2004
Raj wrote:

> "GM" <> wrote in message news:1Gumc.43088$I%1.2791545@attbi_s51...
>
>>A. Sinan Unur wrote:
>>
>>>"Raj" <> wrote in
>>>news:c7dqde$fbs$1$:
>>>
>>>
>>>
>>>>Hi,
>>>>
>>>>I am using some code to talk to a third party server via an HTTPS GET.
>>>>
>>>>The problem I have is that when the server is unavailable, the object
>>>>method thrown an error of the form:
>>>>
>>>>"(Use of uninitialized value in split at
>>>>/opt/perl5/lib/site_perl/5.6.1/sun4-solaris-64int/eo.pm line 151.)"
>>>>
>>>>I need to be able to suppress these warnings, but trap them so that I
>>>>can report them back via my code gracefully.
>>>>
>>>>I know I can trap die using eval() but what can I do to deal with the
>>>>above warning?
>>>
>>>
>>>Well, how about not using split if there is nothing to split?
>>>

>>
>>Ditto!
>>
>>You should be looking at what you get back from the server before trying
>>to do anything with it.

>
>
> But it is someone else's PM that I am using:
>
> my $e = GL::eo::new (
> host=>"$host", key=>"$key",
> ) || die("500");
>
> my $path = $cfg->{GL}->{Path};
>
> my ($p,$r,$h) = $e->do_request('GET', $path, undef); # this is the method
> in which the split occurs
>
> I cannot change the code for the do_request() method. Can I stop it from
> throwing this error?


Do you use

#!perl -w

or

use warnings;

in your script?

IIRC the former will turn on warnings for all used modules too, whereas
the latter will only do so in the lexical scope it is used in.

If it happens with 'use warnings', then either

my ($p,$r,$h);
{
no warnings qw/uninitialized/;
($p,$r,$h) = $e->do_request('GET', $path, undef);
}

or trapping the __WARN__ signal with

local SIG{__WARN__} = sub {
...
do something with the warning message in $_[0]
...
}
my($p,$r,$h) = $e->do_request('GET', $path, undef);

will perhaps do what you want.

see also 'perldoc -f warn' and the section on %SIG in 'perldoc perlvar'

Thomas

--
open STDIN,"<&DATA";$=+=14;$%=50;while($_=(seek( #J~.> a>n~>>e~.......>r.
STDIN,$:*$=+$,+$%,0),getc)){/\./&&last;/\w| /&&( #.u.t.^..oP..r.>h>a~.e..
print,$_=$~);/~/&&++$:;/\^/&&--$:;/>/&&++$,;/</ #.>s^~h<t< ..~. ...c.^..
&&--$,;$:%=4;$,%=23;$~=$_;++$i==1?++$,:_;}__END__#.... >>e>r^..>l^...>k^..
 
Reply With Quote
 
Brian McCauley
Guest
Posts: n/a
 
      05-07-2004
"Raj" <> writes:

> I am using some code to talk to a third party server via an HTTPS GET.
>
> The problem I have is that when the server is unavailable, the object method
> thrown an error of the form:
>
> "(Use of uninitialized value in split at
> /opt/perl5/lib/site_perl/5.6.1/sun4-solaris-64int/eo.pm line 151.)"
>
> I need to be able to suppress these warnings, but trap them so that I can
> report them back via my code gracefully.


As others have pointed out, it would seem more logical to cope
gracefully with the situation that lead to the warnings in the first
place instead.

> I know I can trap die using eval() but what can I do to deal with the above
> warning?


1) You can promote warnings to errors and then trap them as errors.
(See perldoc perllexwarn).

2) You can redirect STDERR to a file (or string). This is fiddly and
IIRC a tied STDERR didn't capture warnings until very recently.

3) You can define a warning handler by assigning $SIG{__WARN__}.

--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      05-07-2004
Brian McCauley <> wrote in comp.lang.perl.misc:
> "Raj" <> writes:
>
> > I am using some code to talk to a third party server via an HTTPS GET.
> >
> > The problem I have is that when the server is unavailable, the object method
> > thrown an error of the form:
> >
> > "(Use of uninitialized value in split at
> > /opt/perl5/lib/site_perl/5.6.1/sun4-solaris-64int/eo.pm line 151.)"
> >
> > I need to be able to suppress these warnings, but trap them so that I can
> > report them back via my code gracefully.

>
> As others have pointed out, it would seem more logical to cope
> gracefully with the situation that lead to the warnings in the first
> place instead.
>
> > I know I can trap die using eval() but what can I do to deal with the above
> > warning?

>
> 1) You can promote warnings to errors and then trap them as errors.
> (See perldoc perllexwarn).


You can do that, but it must happen in the lexical scope of the
code that issues the warning. Since the function that issues the
warning is from a module not under OP's control, that won't help here.

> 2) You can redirect STDERR to a file (or string). This is fiddly and
> IIRC a tied STDERR didn't capture warnings until very recently.


Up until 5.8.0 it caught some and didn't catch others in rather
unpredictable ways. In 5.8.1 and better it catches them all. That
is indeed very recently.

> 3) You can define a warning handler by assigning $SIG{__WARN__}.


I'm afraid that is the way to go. Something like

{
local $SIG{ __WARN__} = sub {
return if $_[ 0] =~ /uninitialized/;
warn @_;
};
# call the warning method
}

ought to do it.

Anno

 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      05-07-2004

Quoth Thomas Kratz <>:
> If it happens with 'use warnings', then either
>
> my ($p,$r,$h);
> {
> no warnings qw/uninitialized/;
> ($p,$r,$h) = $e->do_request('GET', $path, undef);
> }
>
> or trapping the __WARN__ signal with


my ($p, $r, $h);
{

> local SIG{__WARN__} = sub {
> ...
> do something with the warning message in $_[0]
> ...
> }
> my($p,$r,$h) = $e->do_request('GET', $path, undef);


}

>
> will perhaps do what you want.


Or, if it is old code that predates 'warnings',

my ($p, $r, $h);
{
local $^W;
($p, $r, $h) = ...;
}

Ben

--
Although few may originate a policy, we are all able to judge it.
- Pericles of Athens, c.430 B.C.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Trapping MySQLdb warnings Tim Johnson Python 0 06-16-2011 01:58 AM
use warnings; and use Warnings; give different results Ted Sung Perl Misc 1 08-30-2004 10:22 PM
trapping file i/o error toylet Perl 15 02-23-2004 09:16 AM
Trapping click events in the Image control Jeff Ptak ASP .Net 5 08-06-2003 10:08 PM
SNMP trapping/syslog on border routers CPJ Cisco 1 07-16-2003 09:32 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57