Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Default Values in Subroutines

by dragonchild (Archbishop)
on Nov 04, 2008 at 14:22 UTC ( #721404=note: print w/ replies, xml ) Need Help??


in reply to Default Values in Subroutines

Params::Validate was built to solve this problem.


My criteria for good software:
  1. Does it work?
  2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?


Comment on Re: Default Values in Subroutines
Re^2: Default Values in Subroutines
by walkingthecow (Friar) on Nov 04, 2008 at 18:24 UTC

    You see, I want to rewrite my current subroutines and allow for default values, or null values. I was trying the code above, but can't figure out how to get it to work. You see, the command changes based upon input.

    Sometimes the command will be:
    /usr/bin/passwd -n 1 -w 5 -x 30

    Sometimes the command will be:
    /usr/bin/passwd -n 1 -w 5 -x 60

    and sometimes the command will be:
    /usr/bin/passwd -x -1

    I would like to set default values for n (1), w(5) and x(60), but also allow them to be null, like in the case of the last command... Just trying to figure out the best way to do this.

    Current Code:
    sub SUNPwdAging { my $loginid = shift; my $pwminlife = shift; my $pwmaxlife = shift; my $pwdwarn = shift; my $command; my $match_num; my $error; my $match; my $before; my $after; my $passwd = "/usr/bin/passwd"; my $options = "-n $pwminlife -x $pwmaxlife -w $pwdwarn"; # Define full command $command = "$passwd $options $loginid"; # Set the Aging $Login::rsh->clear_accum(); print $Login::rsh "$command\; echo \$?\r"; ($match_num,$error,$match,$before,$after)= $Login::rsh->expect($Login::TIME_OUT, '-re', '(?s).*\r\n[1-7]\r?$' ,'-re', '(?s).*\r\n0\r?$' ); if ($match_num == 1) { $result = "FAILED: ".$match.": ".$after; } elsif ($match_num == 2) { $result = "SUCCESSFUL"; } elsif (defined($error)) { $result = "FAILED: ".$error; } else { $result = "FAILED: Unknown error occurred."; } return $result; }
    Would like it to be something more like this...
    sub SunPwdAging { my %defaults = (qw(n 1 w 5 x 60)) my %args = (%defaults, @_); my $result; ....
    But then, how do I setup the command to even use what's passed to the subroutine?
      What about something like:
      use warnings; use strict; use Data::Dumper; namedMunge(p1=>'aValU', foo=>undef); sub namedMunge { my %ParamHash = @_; my %defaults = ( foo=>0, bar=>42, baz=>'pie'); ApplyDefaultParams(\%ParamHash, \%defaults); # Do Munge here! print Dumper \%ParamHash; } sub ApplyDefaultParams { my $ParamHashRef = shift; my $DefaultsHashRef = shift; foreach my $key (keys(%$DefaultsHashRef)) { if (not exists($ParamHashRef->{$key})) { $ParamHashRef->{$key} = $DefaultsHashRef->{$key}; } } return $ParamHashRef; }
      Resulting in:
      $VAR1 = { 'foo' => undef, 'baz' => 'pie', 'p1' => 'aValU', 'bar' => 42 };

      Maybe something like the following would work for you.

      I note that in your example you had $loginid as first argument but didn't use the variable anywhere.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://721404]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2014-04-17 01:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (437 votes), past polls