I have a subroutine in a perl module and I would like to know best way to have default values, or values that can be passed but have no defaults. For example, sometimes you want to set BOOTPW, but not always, and there really is no default value -- either it is set, or it is not. So, instead of having a default value, how would I set it up to accept it as a parameter without a default value? Also, there is no default value for ID, but not sure how to do that either. However, for some flags there IS a default value, such as exptm which has a default value of -1, but can be changed with parameters. Anyway, here's the code that I have thus far:

sub setAgeHP { my %defaults = (qw(mintm -1 expwarn -1 lftm -1 acctexp -1 exptm -1 + llog -1 bootpw NO umaxlntr -1 id null)) my %args = (%defaults, @_); my $return; if ($args{'pwdage'} == 30) { $hpmod="/usr/lbin/modprpw -k -m "; } else { $hpmod="/usr/lbin/modprpw -m "; } print $rsh "$hpmod $args{id};echo \$?\r"; ($match_num,$error,$match,$before,$after)= $rsh->expect($TIME_OUT, '-re', '^[1-9][0-9][0-9]\r?$' ,'-re', '^[1-9][0-9]*\r?$' ,'-re', '(?s).*\r\n0\r?$'); if ($match_num == 3) { $result = "SUCCESSFUL"; } else { $result = "FAILED with exit code $match"; } return $result; }

In reply to Default Values in Subroutines by walkingthecow

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.