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

Hi,

I would like to know the value of some variable when the warning occurs when I run the program.
Here is one example..:

Use of uninitialized value in pattern match (m//) at subs.pl line 138, <IN> lin\
e 1061.
Use of uninitialized value in numeric eq (==) at subs.pl line 139, <IN> line 10\
Thanks,
Artist

Replies are listed 'Best First'.
(tye)Re: print if warn
by tye (Sage) on Jan 14, 2002 at 23:02 UTC

    I think you might want something like this:

    $SIG{__WARN__}= sub { print "\$var= ($var)\n"; warn @_; };
    so that you can see the value of some variable other than the one directly causing the warning (which would show as being undef, of course) in the process of trying to debug this warning.

            - tye (but my friends call me "Tye")
      Yep, that's helpful..
      Thanks, tye
      Artist
Re: print if warn
by jonjacobmoon (Pilgrim) on Jan 14, 2002 at 22:47 UTC
    Try and restate this question. What you are asking is what is the value of an uninitialized variable. Well, it is uninitialized so it has no value. However, I think you are asking what variable is uninitialized. What you might want to do is put some simple print statements above the line with the variables in them, so you can tell which ones are empty.


    I admit it, I am Paco.
Re: print if warn
by count0 (Friar) on Jan 14, 2002 at 22:48 UTC
    Well, there is (effectively) nothing in the variables in question in that particular case.
    If you're looking to examine other variables around those lines, it will probably be easiest to just put some print()'s in there.
Re: print if warn
by mkmcconn (Chaplain) on Jan 15, 2002 at 00:02 UTC

    I think that you are asking how to test for definedness

    #!/usr/bin/perl -w use strict; my @a = qw(1 2 3); for (@a){ if ( not defined ($a[$_])){ print "\$a[$_] can't match because it's not initialized\n" unless $a[$_] =~ m/3/; print "\$a[$_] can't be tested for equivalence because it's not initialized\n" unless $a[$_] == 3; } }

    mkmcconn

Re: print if warn
by metadoktor (Hermit) on Jan 15, 2002 at 00:05 UTC
    Your problem here is that whatever variable that you're using in the match or conditional is not defined because you fail to initialize it will some data (any data). You will want to study your code (or post it here) to figure out where the mishap is occurring.

    Basically, the variable is undefined (not set to any value) and so you get the error.

    For instance the following code would produce this error.

    my $str; if ($str eq "") { print "str is null\n"; } else { print "str is not null\n"; }
    Will issue one of these "uninitialized" errors.

    metadoktor

    "The doktor is in."

Re: print if warn
by broquaint (Abbot) on Jan 14, 2002 at 22:55 UTC
    I think you're perhaps getting a little confused here since you can't know the _value_ of a variable if it's uninitialised, hence the warning. Also, I very much doubt you can easily find the name of the variable either (unless you do some serious perl grokking ;o).
    HTH

    broquaint

Re: print if warn
by rbc (Curate) on Jan 15, 2002 at 00:11 UTC
    Have you tried turning warnings off
    and then outputting the variable?

    --
    Its like a dog that can sing and dance.
    It's remarkable because it can do it.
    Not that it can do it well.