Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^2: How come undef eq '' ??

by LanX (Canon)
on Jan 30, 2013 at 17:20 UTC ( #1016109=note: print w/ replies, xml ) Need Help??


in reply to Re: How come undef eq '' ??
in thread How come undef eq '' ??

> To get out of the Use of uninitialized value I started adding the $blah AND $blah ... ... which is when I got totally caught up in this.

IMHO thats wrong!

Initialize a value, preferably at the moment where those variables are declared.

Either "" or 0 depending on the way it's used in the line where you got the warning from.

E.g.

DB<115> use warnings; print " $z bla" Use of uninitialized value $z in concatenation (.) or string at (eval +39)[multi_perl5db.pl:644] line 2.

Most likely means that $z is a string, so change my $z to my $z=""

Cheers Rolf


Comment on Re^2: How come undef eq '' ??
Select or Download Code
Re^3: How come undef eq '' ??
by tmharish (Friar) on Jan 30, 2013 at 17:23 UTC
    Struggling with this:
    my $data ; $data = ref($hash) eq 'HASH' ? $$hash{$name} : $hash ; $data = $$data{CONTENT} if ref($data) eq 'HASH' ;
    There is some circular reference also ... Guess I have to stick with catching __WARN__
      I don't get a warning, so whats the problem?

      Cheers Rolf

        Sorry ... dint get that.

        Warning on the code chunk or the Module?

      This code feels... itchy... to me. No offense ;)

      Let's assume ref($hash) ne 'HASH', then $data = $hash, so ref($data) will never eq 'HASH', so that statement modifier on the last line is redundant in this case.

      However, say ref($hash) eq 'HASH' is true, then $data = $$hash{$name}, and then $data = $$data{CONTENT} provided that $$hash{$name} is a hashref. For the sake of argument, let's say it isn't. Then $data will still be $$hash{$name}. Is that how it's supposed to work?

        This code feels... itchy... to me. No offense ;)

        None Taken!

        The idea of this bit is to extract the next level of an XML tree ( stored in a hash, in this case $hash )

        So if ref( $hash ) ne 'HASH' then we are at a leaf and $hash is the actual value and not a sub-tree, and hence the $data = $hash ( now they are both scalar )

        On the other hand, if ref($hash) eq 'HASH' then we are dealing with a sub-tree - so $data = $$hash{$name} is fine.

        There is, however, one exception, and that is when '$hash' contains the key 'CONTENT' like so: $$hash{ CONTENT }. In that case $$hash{ CONTENT } is not a sub-tree but a place holder for the actual data and hence the  $data = $$data{CONTENT}

        Of course this requires that CONTENT is never a key in our hash (although it might be a node in the XML) - but that is achieved during parsing of the XML into the hash.

Re^3: How come undef eq '' ??
by muba (Priest) on Jan 31, 2013 at 01:30 UTC

    I subscribe to this opinion as well.

    To me, undef could mean a couple of things, depending on context:

    • if a function returns it, the function means to tell me
      • there's nothing more to tell you (e.g. when <$filehandle> has read the last line of the file, or when each %hash has returned the last key/value pair of the hash)
      • something went wrong (e.g. "you asked me to list your appointments on Feb 30th, but I couldn't find that date in my calendar?")
      • it wants to return a false value, where 0 or "" don't cut it (e.g. if you use some module to log in to your Amazon account and get the number of items on your wish list, it would be bogus for $amazon->items_on_wish_list to return 0 when $amazon->log_in(user => "muba", password => "soopr sekret!111") failed. 0 here would mean that my wish list is empty, whereas undef would mean "failed to fetch wish list")
      • it pulled its return value from some external source (a JSON object, a database record, whatever) and needs a way to represent the external source's notion of null or whatayamaycallit)
    • if I set a value to undef manually, I could do that because
      • I haven't come around to set it to something more definite yet (e.g. in writing code that reads one line from a file at a time, but has to remember the previous line it read, I'd write
        my $previous = undef; while (my $line = <$filehandle>) { chomp $line; ...; # process $line $previous = $line; }
        because initially, there is no previous line yet, and setting $previous to "" initially would make it look like the previous line was just an empty line. There is a difference between "no previous line" and "previous line was empty")
      • I need it to exist, but I really don't care about the value right now (e.g. I'm preparing the data for a to-be-created user account, which will have a birthday column, but I really don't need to set a definite value for that column while creating the account. The user can set that whenever she fees like. So I'll leave it undef for now)
      • I'm writing a function and I want to return undef for any or more of the reasons I mentioned above

    What all of these reasons have in common, is that they essentially mean to represent mu.

    If asked the question, "did you stop beating your wife?" then the answer "no" would imply that you still beat your wife. "Yes" doesn't cut it either, because it directly means that you used to beat her. But "mu" or "undef" would mean, "the question does not apply, I have never beaten her."

    Therefore, I think that if you're about to do something with a variable (print it, interpolate it, do arithmetic operations with it, throw it against a wall, feed it to your dog), you should either make sure it isn't undef, or explicitly check for its definedness.

      muba - Makes perfect sense.

      I deal with errors ( such as login fail ) by either Croaking or returning two values so:

      my( $is_success, $return_val ) = this_function_might_return_mu();

      But I see the obvious merit in returning undef - Something I intend to use in the future.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (14)
As of 2014-08-27 14:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (241 votes), past polls