Trapping warnings

Discussion in 'Perl Misc' started by Raj, May 6, 2004.

  1. Raj

    Raj Guest

    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
    Raj, May 6, 2004
    #1
    1. Advertising

  2. "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)
    A. Sinan Unur, May 6, 2004
    #2
    1. Advertising

  3. Raj

    GM Guest

    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.
    GM, May 6, 2004
    #3
  4. Raj

    Raj Guest

    "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
    Raj, May 7, 2004
    #4
  5. Raj

    Thomas Kratz Guest

    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^..
    Thomas Kratz, May 7, 2004
    #5
  6. "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\\
    Brian McCauley, May 7, 2004
    #6
  7. Raj

    Anno Siegel Guest

    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
    Anno Siegel, May 7, 2004
    #7
  8. Raj

    Ben Morrow Guest

    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.
    Ben Morrow, May 7, 2004
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. toylet

    trapping file i/o error

    toylet, Feb 19, 2004, in forum: Perl
    Replies:
    15
    Views:
    865
    Tassilo v. Parseval
    Feb 23, 2004
  2. Jeff Ptak
    Replies:
    5
    Views:
    662
    Ken Cox [Microsoft MVP]
    Aug 6, 2003
  3. Chirag Malvi
    Replies:
    1
    Views:
    1,875
    Jim Cheshire [MSFT]
    Nov 11, 2003
  4. Tim Johnson

    Trapping MySQLdb warnings

    Tim Johnson, Jun 16, 2011, in forum: Python
    Replies:
    0
    Views:
    135
    Tim Johnson
    Jun 16, 2011
  5. Ted Sung
    Replies:
    1
    Views:
    299
    Sherm Pendley
    Aug 30, 2004
Loading...

Share This Page