Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Use of uninitialized value in string

by BrowserUk (Patriarch)
on Aug 23, 2002 at 13:15 UTC ( [id://192316]=note: print w/replies, xml ) Need Help??


in reply to Use of uninitialized value in string

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!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (7)
As of 2024-04-19 20:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found