walkingthecow has asked for the wisdom of the Perl Monks concerning the following question:
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; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Default Values in Subroutines
by ig (Vicar) on Nov 04, 2008 at 04:02 UTC | |
|
Re: Default Values in Subroutines
by ysth (Canon) on Nov 04, 2008 at 05:10 UTC | |
|
Re: Default Values in Subroutines
by repellent (Priest) on Nov 04, 2008 at 03:20 UTC | |
|
Re: Default Values in Subroutines
by dHarry (Abbot) on Nov 04, 2008 at 08:17 UTC | |
|
Re: Default Values in Subroutines
by dragonchild (Archbishop) on Nov 04, 2008 at 14:22 UTC | |
by walkingthecow (Friar) on Nov 04, 2008 at 18:24 UTC | |
by SuicideJunkie (Vicar) on Nov 04, 2008 at 20:07 UTC | |
by ig (Vicar) on Nov 06, 2008 at 19:33 UTC |