in reply to use warnings => Use of uninitialized value...

Here is a good one. When I run my program I get the following warnings:
test 1 || Use of uninitialized value in string eq at /usr/lib/perl5/site_perl/5. +8.7/myTool.pm line 316. test 1 || Use of uninitialized value in string eq at /usr/lib/perl5/site_perl/5. +8.7/myTool.pm line 316.
Here is the code
read $fh, $binrec, $l ; print "test 1 |$binrec|\n" ; if ( $binrec ) { # this is line 316 print "test 2\n" ; ... }
So, how come that there is a problem with this line ??

luca

Replies are listed 'Best First'.
Re^2: use warnings => Use of uninitialized value...
by rhesa (Vicar) on May 24, 2006 at 11:00 UTC
    The actual problem is likely in an elsif() condition. Those are lumped together at the if() line:
    use strict; use warnings; my $foo; my $bar = 1; if( $bar - 1 ) { # line 7 print $bar; } elsif( $foo - 1 ) { # throws warning -- at line 7! print "$foo\n"; my $baz = 2 * $foo; } __END__ Use of uninitialized value in subtraction (-) at - line 7. Use of uninitialized value in concatenation (.) or string at - line 10 +. Use of uninitialized value in multiplication (*) at - line 11.
Re^2: use warnings => Use of uninitialized value...
by roboticus (Chancellor) on May 24, 2006 at 11:57 UTC
    jeanluca:

    The problem is that you're using $binrec before you know if it's defined. It appears that the error occurs in your print or if statement.

    After reading read, I don't see anything that indicates that SCALAR is changed if nothing can be read due to an error. In that case, SCALAR may still be undefined. In that event, you can change the code to:

    read $fh, $binrec, $1; if (defined $binrec) { print "test 1 |$binrec|\n"; . . . } else { print 'test 2: undefined $binrec!\n'; }
    --roboticus