cbeckley has asked for the wisdom of the Perl Monks concerning the following question:
I have a subroutine in a module that uses Net::OpenSSH to run administration commands remotely. This sub gets its arguments through a hash ref.
sub ops_do_ssh_shell { my ($cmd) = @_; #etc ... }
sub ops_do_ssh_shell { my ($cmd) = @_; my $ssh; my $key_path = defined $cmd->{key} ? $cmd->{key} : $ENV{HOME} . '/. +ssh/id_rsa'; if ( ops_cmd_status($cmd) ) { $ssh = Net::OpenSSH->new($cmd->{host}, user => $cmd->{user}, key +_path => $key_path); if ( $ssh->error ) { $cmd->{status} = 'failure'; ($cmd->{ssh_retcode}, $cmd->{ssh_retmsg}) = (0 + $ssh->error, + '' . $ssh->error); } } if ( ops_cmd_status($cmd) ) { ($cmd->{output}, $cmd->{std_err}) = $ssh->capture2($cmd->{comman +d}); $cmd->{cmd_ret_code} = 0 + $ssh->error; if ( $ssh->error ) { $cmd->{status} = 'failure'; $cmd->{cmd_ret_msg} = '' . $ssh->error; } chomp $cmd->{output}; chomp $cmd->{std_err}; } return $cmd; }
And I invoke it like this:
my $cmd_status = ops_do_ssh_shell($cmd);
my @commands = ( { name => 'command_name1', user => 'user1', host => 'server1.foo.com', command => 'rsync_command yadda yadda' }, { name => 'command_name2', user => 'user2', host => 'server2.foo.com', command => 'rsync_command yadda yadda' } ); for my $cmd (@commands) { if ($state->{status} eq 'success') { my $cmd_status = ops_do_ssh_shell($cmd); if ( (!defined $cmd_status->{cmd_ret_code}) or ($cmd_status->{cm +d_ret_code} != 0) ) { $state->{status} = 'failure'; $state->{stack_trace} = $cmd_status; } } }
The keys in the $cmd hashref don't benefit from "use strict" and "use warnings". I've found Params::Check and Params::Validate, but is manual validation, even with help of these modules, the best way to achieve this "strictness"?
Or is my $cmd hash approaching the level of complexity that would benefit from implementing it as an object? Am I correct in thinking that would get the validation as a side effect?
Thank you for taking the time to look at this.
Thanks,
cbeckley
|
---|