Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Use of uninitialized value in hash element, -w, How to get details

by brycen (Monk)
on Aug 15, 2006 at 01:07 UTC ( [id://567373]=perlquestion: print w/replies, xml ) Need Help??

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://567373]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (3)
As of 2024-04-18 01:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found