Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

How to deal with undefined returned variables.

( #53040=categorized question: print w/ replies, xml ) Need Help??
Contributed by Pug on Jan 19, 2001 at 21:47 UTC
Q&A  > 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?

Answer: How to deal with undefined returned variables.
contributed by knight

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};
Answer: How to deal with undefined returned variables.
contributed by davorg

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.

Answer: How to deal with undefined returned variables.
contributed by Russ

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!
Answer: How to deal with undefined returned variables.
contributed by Boldra

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.

Please (register and) log in if you wish to add an answer

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others musing on the Monastery: (11)
    As of 2014-08-20 21:58 GMT
    Find Nodes?
      Voting Booth?

      The best computer themed movie is:

      Results (124 votes), past polls