The traditional idiom is to have optional parameters at the end of the list. If there's only one optional arg, then either is is present or it isn't, which works out fine. The variable inside the subroutine will be undef, which you can detect and replace with an empty string, if necessary.

But if you're going to have several optional parameters, then you are forced to have undef arguments in the subroutine call, which is ugly. Worse, if the optional argument has a value of '1', it's hard to read which option is present. If you're forced to do this with old code, use values that read well on the calling side, if possible.

#no so good createIndex( $table, $name, $keyfield, undef, undef, 1); createIndex( $table, $name, $keyfield, undef, 75); #slightly better createIndex( $table, $name, $keyfield, undef, undef, 'ignore_dup_keys' + ); createIndex( $table, $name, $keyfield, undef, 'fillfactor=75' );

If you have control, and there are optional element, it's best to use a hashref. Even if all the args are necessaary, if the number of elements goes over 3 or 4, a hashref is a good idea to improve readability.

As Occam said: Entia non sunt multiplicanda praeter necessitatem.


In reply to Re: Omitted subroutine arguments by TomDLux
in thread Omitted subroutine arguments by tel2

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.