Note that you have a two different issues here: has a scalar (either a stand-alone scalar, or a value in an array or hash) has ever been assigned to; or, for hashes in particular, whether a key/value pair has ever been created.

Perl has two keywords that answer these questions: defined and exists, respectively. If you actually want to assign or create those values, a good idiom is to use unless:

$a = "default" unless defined $a; $h{key} = "value" unless exists $h{key};

If you know that valid values do not include the values that perl considers false (undef, empty string, and 0), you can generally just test the value directly. As an added amusement, you can use the ||= operator to "assign unless true":

$a = "default" unless $a; $h{key} ||= "value";

Finally, if you want to extract a value or a default without modifying the original, the tinary arithmetic conditional operator is a nice fit:

my $a_val = defined $a ? $a : "default"; my $h_key_val = exists $h{key} ? $h{key} : "value";

Again, if you know the values are never "false" in the perl sense, you can dispense with the specific tests and use the || operator:

my $a_val = $a || "default"; my $h_key_val = $h{key} || "value";

Finally, there is a useful idiom for assigning default values into hashes, overriding only keys that are explicitly mentioned. This takes advantage of the fact that, when assigning into a hash from a list, later key/value pairs overwrite the same key seen earlier:

sub do_stuff { # get args from the call my ( %raw_args ) = @_; # build up hash of default arg values my %default_args = ( foo => 'bar', baz => 'quux' ); # now join the two, giving precedence to %raw_args my %args = ( %default_args, %raw_args ); }

In reply to Re: check for existing value, else set to default by tkil
in thread check for existing value, else set to default by nmerriweather

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.