in reply to How best to validate the keys of hashref arguments?
I am writing a program that does the same thing with Net::OpenSSH. I've created a OO role written in Moose that wraps Net::OpenSSH and I send commands through the objects that use the role. I call it with: WebServerRemote->new({ssh => 'me@198.168.1.2'}); Then I execute commands like this: $self->grab('ls -l') where "grab" is a wrapper for the capture command. Here's a rough skeleton of it:
package MyOpenSSH 0.000001; use Carp; use Moose::Role; use Modern::Perl; use Net::OpenSSH; use Params::Validate; has 'ssh' => (is => 'rw', isa => 'Net::OpenSSH', required => 1, lazy = +> 0, handles => qr/.*/, ); sub BUILD { # do object validation stuff here } sub exec { my $self = shift; my $opts = shift; if (ref $opts eq 'HASH') { my $cmd = shift; $self->ssh->system($opts, $cmd) || croak 'Command failed: ' . $sel +f->ssh->error; } else { my $cmd = $opts; $self->ssh->system($cmd, @_) || croak 'Command failed: ' . $self-> +ssh->error; } } # wrapper for capture method # this has been edited to simplify and fix a bug since last post. sub grab { my $self = shift; return $self->ssh->capture(@_) if !$self->ssh->error; croak ('ssh command failed'); }
DISCLAIMER: this code is still a work in progress and was written mostly as an experiment as a way for me to get more proficient with Moose and I haven't worked out some details with it. But maybe this will inspire some ideas for you.
$PM = "Perl Monk's";
$MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate";
$nysus = $PM . ' ' . $MCF;
Click here if you love Perl Monks
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: How best to validate the keys of hashref arguments?
by cbeckley (Curate) on Mar 16, 2017 at 18:09 UTC | |
by nysus (Parson) on Mar 16, 2017 at 18:26 UTC | |
by afoken (Chancellor) on Mar 17, 2017 at 07:33 UTC |