in reply to Re^2: Default Values in Subroutines
in thread Default Values in Subroutines
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.
# # SunPwdAging() runs passwd on a remote system using rsh # # Examples: # # my $result = SunPwdAging( # loginid => "mylogin", # n => 5, # password minimum lifetime # x => 30, # password maximum lifetime # w => 25, # password warning age # ); # # To exclude any of n, x or w from the command, set the value of the # argument to undef. For example, to exclude the password warning age # from the executed command, pass arguments such as the following: # # my $result = SunPwdAging( # loginid => "mylogin", # n => 5, # password minimum lifetime # x => 30, # password maximum lifetime # w => undef, # password warning age # ); # # Returns the string "SUCCESSFUL" if all goes well or a string beginin +g # with "ERROR" followed by an error message if something goes wrong. # sub SunPwdAging { my %defaults = (qw(n 1 w 5 x 60)); my %args = ( %defaults, @_ ); my $result = "ERROR: something went wrong"; return ("FAILED: missing loginid") unless ( $args{loginid} ); my $command = "/usr/bin/passwd"; foreach my $arg (qw(n w x)) { if ( defined( $args{$arg} ) ) { $command .= " -$arg $args{$arg}"; } } # Set the Aging $Login::rsh->clear_accum(); print $Login::rsh "$command\; echo \$?\r"; my ( $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; }
|
|---|