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

my $num = 0; my $status_count = 0; my $status_goal = 0; sub actions { my ($dev, $ino, $mode, $nlink, $uid, $gid); (( $dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)); $num++; $status_goal = $status_count + 2000; #47
Each time the script is run, it says "Use of uninitialized value in addition (+) at script.pl line 47. How can this possibly be?

Replies are listed 'Best First'.
Re: Use of uninitialized value
by The Mad Hatter (Priest) on Nov 07, 2004 at 03:37 UTC

    Like pg said, you need to show us the rest of your code. In lieu of that, I'm guessing you call this sub actions before the initializing code

    my $num = 0; my $status_count = 0; my $status_goal = 0;
    has been run.

    For example, examine how the following code runs:

    use strict; use warnings; print_foo(); my $foo = "bar\n"; sub print_foo { print $foo; } print_foo();

    The first call to the sub throws the uninitialized error because the statement my $foo = "bar\n"; hasn't been executed yet. By the second call, that statement has been executed, so there is no warning.

      That can be fixed as follows by reordering the statememnts, or by wrapping the my (and therefore the sub) in a BEGIN block:

      BEGIN { my $foo = "bar\n"; sub print_foo { print $foo; } }
      That would be my guess also. To avoid this kind of errors, I advise against the use of 'globally used lexicals'. use 'use constant' for constants or pass them to the subroutine and pass variables by reference. Paul
Re: Use of uninitialized value
by pg (Canon) on Nov 07, 2004 at 03:03 UTC

    A side note, what is the value for $_?

    (( $dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_));

    Cannot see any obvious problem with that line 47. You didn't post your entire code, the clue must be in the missing part.

      Yes, I'm positive. I checked the line number and besides, it's complaining about a line with addition (+) which the line you posted doesn't have.
        The line number in the warning may NOT be the line number with the error. Lacking the rest of the code, it's hard to be sure, but you should probably look hard at the rest of the code (and/or, post it here)