Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Use of uninitialized value in string

by c (Hermit)
on Aug 22, 2002 at 22:10 UTC ( #192192=perlquestion: print w/replies, xml ) Need Help??

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

I'm a bit confused as to why I am getting the above error in a subroutine. My sub starts out with...

1427 sub foo { 1428 my $args = shift; 1429 1430 if ($args->{err}) { 1431 print "There is a problem : $args->{err}\n"; 1432 } 1433 1434 if ($args->{src} eq "commit") { 1435 print "I've got my sources!\n"; 1436 print "I'm committed!\n" 1437 if (!$args->{err}); 1438 } 1439 }

The curious thing for me is that when I run my script, I get

Use of uninitialized value in string eq at ./pancho-ini line 1430. I've got my sources! I'm committed!

I'm just not understanding why one test against $args->{err} seems to think it is a valid value, while the other, does not. I had thought that defined or exists would be the missing key for line 1430, but when I add either of them, line 1430 seems to prove 'true'

Thanks! -c

Replies are listed 'Best First'.
Re: Use of uninitialized value in string
by sauoq (Abbot) on Aug 22, 2002 at 22:22 UTC

    Are you positive that you have given us correct information? The reason I ask is that the warning you are getting doesn't even jive with the code you've shown us.

    Use of uninitialized value in string eq at ./pancho-ini line 1430.

    That states the unitialized value is being used in a "string eq". The only string eq you have in the code you've shown us is on line 1434. Is that your actual code? (Do you really have sub called "foo"?

    -sauoq
    "My two cents aren't worth a dime.";
    
      Yes. Its the real deal. I've been hunting and pecking for and "eq" in my code, even near line 1430, and its just not there.
Re: Use of uninitialized value in string
by blakem (Monsignor) on Aug 22, 2002 at 22:23 UTC
    Looks odd to me... could it be that line 1434 is actually the troublesome one? Try replacing it with:
    1434 if ($args->{src} && $args->{src} eq "commit") {

    -Blake

      No line 1434 can't be the troublemaker because with or without warnings, undef ne 'commit'. The result of this boolean test would be false, and nothing would be printed. And as you can see, there is printout from this block:
      I've got my sources! I'm committed!

      To the op: use Data::Dumper, and print out what's in the dubious variables (here: $arg). If this sub is called a lot without any warnings, then it's better to restrict output to when it is actually useful. For that, I sometimes do things like:

      use Data::Dumper; local $SIG{__WARN__} = sub { print STDERR @_, Dumper $arg };
      You must put that where $arg is in scope, thus in the sub, and it needs to be run before the warning happens.

      Also, print out the line number perl thinks it's on, it's in __LINE__. Sometimes it's off from the actual line number by 1, maybe sometimes even more (though I don't think I've ever seen that happen).

        The line numbering seems to drift when your statement itself is more than one line, or on occasion when the if call is labelled the culprit, when it's actually a line of code in the if-BLOCK. So, it seems that 1431 is the problem, not 1430, and yet, no output is produced to support of that theory.
Re: Use of uninitialized value in string
by BrowserUk (Pope) on Aug 23, 2002 at 13:15 UTC

    This doesn't explain your error message, but it does highlight a couple of problems with your code that could be a contributory factors.

    #! perl -w use strict; my $args = { src=>'commit' }; if ($args->{err}) { print "There is a problem : $args->{err}\n"; } if ($args->{src} eq "commit") { print "I've got my sources!\n"; print "I'm committed!\n" if (!$args->{err}); } __END__ # Output C:\test>192192 I've got my sources! I'm committed!

    You''ll notice that there is no key 'err' defined in the hash, and that, "I'm commited" was printed.

    If I modify this as follows:

    #! perl -w use strict; my $args = { src=>'commit' }; if ( exists $args->{err} && $args->{err} ) { print "There is a problem + : $args->{err}\n"; } if ($args->{src} eq "commit") { print "I've got my sources!\n"; print "I'm committed!\n" if (!exists $args->{err} && !$args->{err} +); } __END__ # Output C:\test>192192 I've got my sources!

    This time, the "I'm commited" wasn't printed!

    The reason for this is autovivifcation. Even testing for the presence of a hash element if you do it in any other way than using exists. Even doing if (defined $args->{key}) {...} will cause the key to 'spring into existance'.

    Another clue to your error message is that coding if ($somevar) is semantically similar to doing if ( $somevar ne '' ) and the test is probably translated as such by the compiler.

    Changing your code to use exists key && key may make your warning go away.

    I did have a thought that if the reference being shifted into $args is a bless'd reference, the source of the warning might lie outside your code in the blessing package, but my knowledge of Perl's OO stuff is still in it's infancy, so I can't add anything to the thought.


    What's this about a "crooked mitre"? I'm good at woodwork!
Re: Use of uninitialized value in string
by tadman (Prior) on Aug 22, 2002 at 22:42 UTC
    Just out of curiousity, what version of Perl?

    Also, what happens when you do this:
    use Data::Dumper; print Dumper($args);
      I'm using 5.6.1. and Dumper (doh! i forgot about what a great tool this is!) shows:

      $VAR1 = { 'path' => '', 'desc' => 'Cisco Internetwork Operating System Software IOS (tm) C2600 Software (C2600-I-M), Version 12.0(7)T, RELEASE SOFTWA +RE (fc2) Copyright (c) 1986-1999 by cisco Systems, Inc. Compiled Tue 07-Dec-99 02:12 by phanguye', 'utftp' => 'TFTP.LUNARMEDIA.LAB, 'host' => 'ROUTER', 'tftp' => '10.12.71.130', 'src' => 'commit', 'file' => 'router.cfg', 'err' => '' };

      Oddly, this is what I believe that it *should* be returning. the Net::SNMP docs show that error() will return empty if there is no error found. Does this output make more sense for anyone else? It just seems to validate the confusion for me.

      Thanks VERY much -c

Re: Use of uninitialized value in string
by ducky (Scribe) on Aug 22, 2002 at 23:28 UTC

    You may have already tried this, but when I've run into this kind of test, I end up writing the clunky:

    ... if ( defined $args->{err} and $args->{err} ) { print "There is a problem : $args->{err}\n"; } ...
    Which bugged me in perl ~5.6. DWIM gone awry, me thinks.

    HTH

    -Ducky

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2021-06-23 18:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)












    Results (121 votes). Check out past polls.

    Notices?