Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

How to deal with undefined returned variables.

by Pug (Monk)
on Jan 19, 2001 at 21:47 UTC ( #53040=perlquestion: print w/replies, xml ) Need Help??

Pug has asked for the wisdom of the Perl Monks concerning the following question: (subroutines)

I'm using IniConf which returns undefined if the section does not exist. I have found that this works but it is painful to read:
if ( ! my $temp = $cfg->val("MISC","HASHTYPE") ) { $CONFIG{HASHTYPE}="sha1"; } else { $CONFIG{HASHTYPE}=$temp; }
Is there a better way?

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: How to deal with undefined returned variables.
by knight (Friar) on Jan 19, 2001 at 21:55 UTC
    davorg presents the common idiom, but note that it can lose if the actual value returned is "defined but false", e.g. 0 or "0". To handle this robustly can be done in two lines:
    $CONFIG{HASHTYPE} = $cfg->val("MISC","HASHTYPE"); $CONFIG{HASHTYPE} = "shal" if ! defined $CONFIG{HASHTYPE};
Re: How to deal with undefined returned variables.
by davorg (Chancellor) on Jan 19, 2001 at 21:54 UTC

    If zero or the empty string aren't valid values then you could use:

    $CONFIG{HASHTYPE} = $cfg->val("MISC","HASHTYPE") || 'sha1';

    Otherwise, you'd need:

    $CONFIG{HASHTYPE} = defined $cfg->val("MISC","HASHTYPE") ? $cfg->val("MISC","HASHTYPE") : 'sha1';

    which isn't much different to what you've got.

Re: How to deal with undefined returned variables.
by Russ (Deacon) on Mar 05, 2002 at 19:51 UTC
    knight makes a good point. Fortunately, in Perl 6, there will be a defaulting operator to simplify this very problem.
    A binary // operator is the defaulting operator.
    That is:
    $a // $b
    is short for:
    defined($a) ?? $a :: $b
    except that the left side is evaluated only once. It will work on arrays and hashes as well as scalars. It also has a corresponding assignment operator, which only does the assignment if the left side is undefined:
    $pi //= 3;
    Perl 6 should be such fun!
      I take it that the difference between // and || is the difference between testing truthfulness and testing definedness.

      Hence there is not difference between $a // $b and $a || $b unless $a is 0 or "".

      It doesn't sound that useful to me.

Re: How to deal with undefined returned variables.
by Boldra (Deacon) on Dec 09, 2008 at 10:23 UTC
    Russ mentioned that the // operator is in Perl 6, but in fact it is available in Perl 5.10:
    use 5.010; $CONFIG{HASHTYPE} = $cfg->val("MISC","HASHTYPE") // 'sha1';
    This only puts 'sha1' in your config value if your $cfg->val returns undef or nothing.

    Definitely a valuable addition to the language.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2022-06-30 19:57 GMT
Find Nodes?
    Voting Booth?
    My most frequent journeys are powered by:

    Results (98 votes). Check out past polls.