Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

return a hash or undef

by odrevet (Acolyte)
on Aug 03, 2009 at 19:54 UTC ( [id://785538]=perlquestion: print w/replies, xml ) Need Help??

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

It's a simple question. I've got a function that return a hash or undef if no value is found.

my %resp = my_func(); if(defined %resp) { #do something like print $resp{foo}; print{bar}; }

If the function return undef, I've got the warning "Odd number of elements in hash assignment".

That's makes sense, since the hash got only one value (undef). But I need to check if the hash has values, or the application will crash if I just assum there are values and try to use it...

Replies are listed 'Best First'.
Re: return a hash or undef
by moritz (Cardinal) on Aug 03, 2009 at 20:05 UTC
    This is why the book "Perl Best Practices" recommends an empty return; over return undef; - in list context it will produce the empty list, which evaluates to false.
Re: return a hash or undef
by hobbs (Monk) on Aug 03, 2009 at 20:25 UTC
    A) Don't return undef. Return () instead, and instead of checking for if (defined %resp), just check if (%resp). This works as long as you don't need to distinguish between a successful-but-empty return and a failure.

    B) Return a hashref instead of a flattened hash, and assign the function's result to a scalar instead of a hash. Then undef, {}, and { foo => bar } are all distinguishable values (!defined $resp, !%$resp, and everything else, respectively.)

Re: return a hash or undef
by psini (Deacon) on Aug 03, 2009 at 19:59 UTC

    Two possible solutions:

    • Modify the sub to return a hash reference or undefined, if defined dereference it
    • If you can't change the sub, store the result in an array and, if defined, copy it to an hash

    Rule One: "Do not act incautiously when confronting a little bald wrinkly smiling man."

Re: return a hash or undef
by jethro (Monsignor) on Aug 03, 2009 at 20:14 UTC
Re: return a hash or undef
by halfcountplus (Hermit) on Aug 03, 2009 at 20:14 UTC
    If the function return undef, I've got the warning "Odd number of elements in hash assignment".
    Yeah, that is kind of irritating isn't it? Probably there is a good reason.

    You might want to investigate the use of eval...but also notice that this is allowed, even if %hash is "undef":
    if (defined($hash{non-existant key})) ...
    the condition, of course, will be untrue, unless there really is a key called "non-existant key" and it is defined.

Log In?
Username:
Password:

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

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

    No recent polls found