A slightly different approach is to define a default for every argument and explicitly make any required argument undefined. Then check for undefined arguments, take action as appropriate. This can generate a lot of useful warnings.

>perl -wMstrict -le "print send_values({name => 'Joe Doe', addr => '123 4th St.'}); print send_values({ addr => '666 Mobil Ave.', phone => '321-432-5432' }); print send_values({ name => 'Moe Foe', addr => '7 6th Rd.', phone => '432-543-6543' }); ;; sub send_values { my ($hr_args) = @_; ;; my %defaults = ( name => undef, addr => undef, phone => '(not given)', ); ;; my %args = (%defaults, %$hr_args); my @bad = grep !defined($args{$_}), keys %args; return undef if @bad and warn((caller 0)[3], qq{: bad arg(s): @bad}); ;; return sprintf qq{<n>%s</n> <a>%s</a> <p>%s</p>}, @args{ qw(name addr phone) }; } " <n>Joe Doe</n> <a>123 4th St.</a> <p>(not given)</p> main::send_values: bad arg(s): name at -e line 1. Use of uninitialized value in print at -e line 1. <n>Moe Foe</n> <a>7 6th Rd.</a> <p>432-543-6543</p>

In reply to Re: Subroutine question on use of uninitialized value. by AnomalousMonk
in thread Subroutine question on use of uninitialized value. by Anonymous Monk

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.