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

So I'm getting a fairly typical "Use of uninitialized value", with the -w switch. How can I get more details, such as "Use of uninitialized value $foo"?
#!/usr/bin/perl -w use strict; use Carp; my $foo; my $fum="testing"; print "$fum $foo\n";
Sometimes it is not so obvious exactly what value is the problem...
  • Comment on Use of uninitialized value in hash element, -w, How to get details
  • Download Code

Replies are listed 'Best First'.
Re: Use of uninitialized value in hash element, -w, How to get details
by ikegami (Patriarch) on Aug 15, 2006 at 01:14 UTC

    You can't. It says which line the problem is in, and in this case, it even specifies it's in the concatenation. (String interpolation is a form of concatenation.)

    You can debug it real quick as follows:

    #!/usr/bin/perl -w use strict; use Carp; my $foo; my $fum="testing"; "$fum" && 0; "$foo" && 0; print "$fum $foo\n";

    If the data is allowed to be undefined, you can used defined to check if it and sanitize it.

    #!/usr/bin/perl -w use strict; use Carp; my $foo; my $fum="testing"; my $f_foo = defined $foo ? $foo : ''; my $f_fum = defined $fum ? $fum : ''; print "$f_fum $f_foo\n";

    ("f_" for "formatted")

      When I don't care, I prefer using:
      { no warnings 'uninitialized'; print "$fum $foo\n"; }
      But sometimes I want to track down the real cause. I'm at v5.8.7... I wonder how long until SUSE picks up perl 5.10 with the improvement?
Re: Use of uninitialized value in hash element, -w, How to get details
by ysth (Canon) on Aug 15, 2006 at 02:34 UTC
    FWIW, perl 5.10 will give
    Use of uninitialized value $foo in concatenation (.) or string at - li +ne 6. testing
Re: Use of uninitialized value in hash element, -w, How to get details
by explorer (Chaplain) on Aug 15, 2006 at 01:12 UTC

    use diagnostics;

    Update: ikegami have reason. diagnostics only give more info... but It's useful sometimes.

      No, diagnostics doesn't identify which variable is the culprit. At least not the one that came with perl5.8.8.