catching "Use of uninitialized value" warnings

Discussion in 'Perl Misc' started by badarisj@gmail.com, Sep 30, 2006.

  1. Guest

    folks,

    now and then we see
    Use of uninitialized value in xxx
    warning messages while running a perl program.

    is there a way to catch such warning messages that perl throws and
    display
    the stack that is leading to the warning message?

    thanks,
    -badari
    , Sep 30, 2006
    #1
    1. Advertising

  2. wrote:

    > folks,
    >
    > now and then we see
    > Use of uninitialized value in xxx
    > warning messages while running a perl program.
    >
    > is there a way to catch such warning messages that perl throws and
    > display
    > the stack that is leading to the warning message?
    >
    > thanks,
    > -badari

    umm, a good thing to do would be to check if variables you use are
    inizialised. you can do so with for example:
    if ( defined($variable) )
    {
    ...code here...
    }
    Oobi Van Doobi, Sep 30, 2006
    #2
    1. Advertising

  3. Guest

    wrote:

    > now and then we see
    > Use of uninitialized value in xxx
    > warning messages while running a perl program.
    >
    > is there a way to catch such warning messages that perl throws and
    > display
    > the stack that is leading to the warning message?


    For development purposes you can install a SIGWARN handler that will
    print a stacktrace when the warning message conforms to certain
    criteria:

    #!/usr/bin/perl

    use warnings;

    $SIG{__WARN__} = sub {
    if ($_[0] =~ /^Use of uninitialized value/) {
    require Carp;
    Carp::cluck();
    } else {
    warn @_;
    }
    };

    sub foo {
    print undef;
    }

    sub bla {
    foo();
    }

    bla(42);
    __END__
    at cluck.pl line 6
    main::__ANON__('Use of uninitialized value in print at
    cluck.pl line 13.\x{a}') called at cluck.pl line 13
    main::foo() called at confess.pl line 17
    main::bla(42) called at confess.pl line 20

    Cheers,
    Tassilo
    , Sep 30, 2006
    #3
  4. Guest

    hi tassilo,

    thanks a lot. i didn't realize that all messages that the perl
    interpreter
    shows can be trapped using __WARN__ or __DIE__ sighandlers.
    works well for what i want.

    -badari


    wrote:
    > wrote:
    >
    > > now and then we see
    > > Use of uninitialized value in xxx
    > > warning messages while running a perl program.
    > >
    > > is there a way to catch such warning messages that perl throws and
    > > display
    > > the stack that is leading to the warning message?

    >
    > For development purposes you can install a SIGWARN handler that will
    > print a stacktrace when the warning message conforms to certain
    > criteria:
    >
    > #!/usr/bin/perl
    >
    > use warnings;
    >
    > $SIG{__WARN__} = sub {
    > if ($_[0] =~ /^Use of uninitialized value/) {
    > require Carp;
    > Carp::cluck();
    > } else {
    > warn @_;
    > }
    > };
    >
    > sub foo {
    > print undef;
    > }
    >
    > sub bla {
    > foo();
    > }
    >
    > bla(42);
    > __END__
    > at cluck.pl line 6
    > main::__ANON__('Use of uninitialized value in print at
    > cluck.pl line 13.\x{a}') called at cluck.pl line 13
    > main::foo() called at confess.pl line 17
    > main::bla(42) called at confess.pl line 20
    >
    > Cheers,
    > Tassilo
    , Oct 3, 2006
    #4
    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. G Kannan
    Replies:
    1
    Views:
    1,223
    Eric J. Roode
    Oct 11, 2003
  2. Sukhbir Dhillon
    Replies:
    1
    Views:
    6,236
    Joe Smith
    Apr 5, 2004
  3. smartins68
    Replies:
    1
    Views:
    5,990
    Joe Smith
    Jun 9, 2004
  4. Liang
    Replies:
    2
    Views:
    751
    Liang
    Aug 11, 2004
  5. Ted Sung
    Replies:
    1
    Views:
    301
    Sherm Pendley
    Aug 30, 2004
Loading...

Share This Page