| 1 | # You may distribute under the terms of either the GNU General Public License | ||||||||||||||||||||||||||||
| 2 | # or the Artistic License (the same terms as Perl itself) | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | # (C) Paul Evans, 2008-2012 -- leonerd@leonerd.org.uk | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | package IPC::PerlSSH::Async; | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 6 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 6 | ▶ | use base qw( IO::Async::Notifier IPC::PerlSSH::Base ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 6 | IPC::PerlSSH::Base->VERSION( '0.16' ); | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 6 | ▶ | use IO::Async::Process 0.37; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 6 | our $VERSION = '0.07'; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 6 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | =head1 NAME | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | C<IPC::PerlSSH::Async> - Asynchronous wrapper around L<IPC::PerlSSH> | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | I<Note:> the constructor has changed since version 0.03. | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | use IO::Async::Loop; | ||||||||||||||||||||||||||||
| 28 | use IPC::PerlSSH::Async; | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | my $loop = IO::Async::Loop->new(); | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | my $ips = IPC::PerlSSH::Async->new( | ||||||||||||||||||||||||||||
| 33 | on_exception => sub { die "Failed - $_[0]\n" }, | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | Host => "over.there", | ||||||||||||||||||||||||||||
| 36 | ); | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | $loop->add( $ips ); | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | $ips->eval( | ||||||||||||||||||||||||||||
| 41 | code => "use POSIX qw( uname ); uname()", | ||||||||||||||||||||||||||||
| 42 | on_result => sub { print "Remote uname is ".join( ",", @_ )."\n"; }, | ||||||||||||||||||||||||||||
| 43 | ); | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | # We can pass arguments | ||||||||||||||||||||||||||||
| 46 | $ips->eval( | ||||||||||||||||||||||||||||
| 47 | code => 'open FILE, ">", shift; print FILE shift; close FILE;', | ||||||||||||||||||||||||||||
| 48 | args => [ "foo.txt", "Hello, world!" ], | ||||||||||||||||||||||||||||
| 49 | on_result => sub { print "Wrote foo.txt\n" }, | ||||||||||||||||||||||||||||
| 50 | ); | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | # We can load pre-defined libraries | ||||||||||||||||||||||||||||
| 53 | $ips->use_library( | ||||||||||||||||||||||||||||
| 54 | library => "FS", | ||||||||||||||||||||||||||||
| 55 | funcs => [qw( unlink )], | ||||||||||||||||||||||||||||
| 56 | on_loaded => sub { | ||||||||||||||||||||||||||||
| 57 | $ips->call( | ||||||||||||||||||||||||||||
| 58 | name => "unlink", | ||||||||||||||||||||||||||||
| 59 | args => [ "foo.txt" ], | ||||||||||||||||||||||||||||
| 60 | on_result => sub { print "Removed foo.txt\n" }, | ||||||||||||||||||||||||||||
| 61 | ); | ||||||||||||||||||||||||||||
| 62 | }, | ||||||||||||||||||||||||||||
| 63 | ); | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | $loop->loop_forever; | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | This module provides an object class that implements the C<IPC::PerlSSH> | ||||||||||||||||||||||||||||
| 70 | behaviour in an asynchronous way, suitable for use in an C<IO::Async>-based | ||||||||||||||||||||||||||||
| 71 | program. | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | Briefly, C<IPC::PerlSSH> is a module that allows execution of perl code in a | ||||||||||||||||||||||||||||
| 74 | remote perl instance, usually accessed via F<ssh>, with the notable | ||||||||||||||||||||||||||||
| 75 | distinction that the module does not need to be present in the remote end, nor | ||||||||||||||||||||||||||||
| 76 | does any special server need to be running, besides F<ssh> itself. For more | ||||||||||||||||||||||||||||
| 77 | detail, see the L<IPC::PerlSSH> documentation. | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | =cut | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | =head1 INITIAL PARAMETERS | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | As well as the L</PARAMETERS> named below, the constructor will take any of | ||||||||||||||||||||||||||||
| 84 | the constructor arguments named by L<IPC::PerlSSH>, to set up the connection. | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | =cut | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | =head1 PARAMETERS | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | The following named parameters may be passed to C<new> or C<configure>: | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | =over 8 | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | =item on_exception => CODE | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | Optional. A default callback to use if a call to C<eval()>, C<store()> or | ||||||||||||||||||||||||||||
| 97 | C<call()> does not provide one. If it is changed while a result it | ||||||||||||||||||||||||||||
| 98 | outstanding, the handler that was in place at the time it was invoked will be | ||||||||||||||||||||||||||||
| 99 | used in case of errors. Changes will only affect new C<eval()>, C<store()> or | ||||||||||||||||||||||||||||
| 100 | C<call()> calls made after the change. | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | =item on_exit => CODE | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | Optional. A callback to invoke if the remote perl process exits. Will be | ||||||||||||||||||||||||||||
| 105 | passed directly to the C<IO::Async::Process> C<on_finish> method. | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | =back | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | =cut | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | sub new | ||||||||||||||||||||||||||||
| 112 | 5 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 113 | 5 | my $class = shift; | |||||||||||||||||||||||||||
| 114 | 5 | my %args = @_; | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 5 | my $loop = delete $args{loop}; | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 5 | my $self = $class->SUPER::new( %args ); | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 5 | ▶ | if( $loop ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | 1 | warnings::warnif( deprecated => "'loop' constructor argument is deprecated" ); | |||||||||||||||||||||||||||
| 122 | 1 | $loop->add( $self ); | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 5 | return $self; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | sub _init | ||||||||||||||||||||||||||||
| 129 | 5 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_init: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 130 | 5 | my $self = shift; | |||||||||||||||||||||||||||
| 131 | 5 | my ( $params ) = @_; | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | # This will delete keys | ||||||||||||||||||||||||||||
| 134 | 5 | $self->{IPC_PerlSSH_command} = [ $self->build_command_from( $params ) ]; | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 5 | $self->{message_queue} = []; | |||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 5 | return $self->SUPER::_init( $params ); | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | sub configure | ||||||||||||||||||||||||||||
| 142 | 5 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
configure: called CC=7 SCAR=20.4
Pod
documented
| |||||||||||||||||||||||||||||
| 143 | 5 | my $self = shift; | |||||||||||||||||||||||||||
| 144 | 5 | my %params = @_; | |||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 5 | ▶ | if( exists $params{on_exception} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | 4 | my $on_exception = delete $params{on_exception}; | |||||||||||||||||||||||||||
| 148 | 4 | ▶ | !$on_exception or ref $on_exception eq "CODE" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $on_exception or ref $on_exception eq "CODE"
MC/DC0%0/2
not $on_exception or ref $on_exception eq "CODE"
not $on_exception
ref $on_exception eq "CODE"
Truth table33%1/3
not(A) or B
A= not $on_exceptionB= ref $on_exception eq "CODE"
| |||||||||||||||||||||||||||||
| 149 | or croak "Expected 'on_exception' to be a CODE reference"; | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 4 | $self->{on_exception} = $on_exception; | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 5 | ▶ | if( exists $params{on_exit} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | 1 | my $on_exit = delete $params{on_exit}; | |||||||||||||||||||||||||||
| 156 | 1 | ▶ | !$on_exit or ref $on_exit eq "CODE" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $on_exit or ref $on_exit eq "CODE"
MC/DC0%0/2
not $on_exit or ref $on_exit eq "CODE"
not $on_exit
ref $on_exit eq "CODE"
Truth table33%1/3
not(A) or B
A= not $on_exitB= ref $on_exit eq "CODE"
| |||||||||||||||||||||||||||||
| 157 | or croak "Expected 'on_exit' to be a CODE reference"; | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 1 | $self->{on_exit} = $on_exit; | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 5 | $self->SUPER::configure( %params ); | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | sub _add_to_loop | ||||||||||||||||||||||||||||
| 166 | 5 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_add_to_loop: called CC=3 SCAR=11.3
| |||||||||||||||||||||||||||||
| 167 | 5 | my $self = shift; | |||||||||||||||||||||||||||
| 168 | 5 | $self->SUPER::_add_to_loop( @_ ); | |||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 0 | ▶ | my $on_exit = $self->{on_exit} || sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 171 | 0 | print STDERR "Remote SSH died early"; | |||||||||||||||||||||||||||
| 172 | 5 | ▶ | }; | ||||||||||||||||||||||||||
|
Condition66%2/3
$self->{'on_exit'} || sub {
print STDERR 'Remote SSH died early';
}
MC/DC0%0/2
$self->{'on_exit'} || sub {
print STDERR 'Remote SSH died early';
}
$self->{'on_exit'}
sub {
print STDERR 'Remote SSH died early';
}
Truth table66%2/3
A || B
A= $self->{'on_exit'}B= sub {
print STDERR 'Remote SSH died early';
}
| |||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 5 | ▶ | if( my $command = delete $self->{IPC_PerlSSH_command} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 175 | # TODO: IO::Async ought to have nice ways to do this | ||||||||||||||||||||||||||||
| 176 | 5 | my $process = $self->{process} = IO::Async::Process->new( | |||||||||||||||||||||||||||
| 177 | command => $command, | ||||||||||||||||||||||||||||
| 178 | stdio => { via => "pipe_rdwr" }, | ||||||||||||||||||||||||||||
| 179 | on_finish => $on_exit, | ||||||||||||||||||||||||||||
| 180 | ); | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 5 | $process->stdio->configure( | |||||||||||||||||||||||||||
| 183 | on_read => $self->_replace_weakself( "on_read" ), | ||||||||||||||||||||||||||||
| 184 | ); | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | 5 | $self->add_child( $process ); | |||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | 5 | $self->send_firmware; | |||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | sub on_read | ||||||||||||||||||||||||||||
| 193 | 19 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
on_read: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 194 | 19 | my $self = shift; | |||||||||||||||||||||||||||
| 195 | 19 | my ( $buffref, $closed ) = @_; | |||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | 19 | ▶ | if( $closed ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 198 | 1 | ▶ | while( my $cb = shift @{ $self->{message_queue} } ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 199 | 1 | $cb->( "CLOSED" ); | |||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||
| 201 | 1 | return 0; | |||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 18 | my ( $message, @args ) = $self->parse_message( $$buffref ); | |||||||||||||||||||||||||||
| 205 | 18 | ▶ | return 0 unless defined $message; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | 14 | my $cb = shift @{ $self->{message_queue} }; | |||||||||||||||||||||||||||
| 208 | 14 | $cb->( $message, @args ); | |||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | 14 | return 1; | |||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | sub write | ||||||||||||||||||||||||||||
| 214 | 20 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
write: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 215 | 20 | my $self = shift; | |||||||||||||||||||||||||||
| 216 | 20 | $self->{process}->stdio->write( @_ ); | |||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | sub do_message | ||||||||||||||||||||||||||||
| 220 | 15 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
do_message: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 221 | 15 | my $self = shift; | |||||||||||||||||||||||||||
| 222 | 15 | my %args = @_; | |||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | 15 | my $message = $args{message}; | |||||||||||||||||||||||||||
| 225 | 15 | my $args = $args{args}; | |||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | 15 | my $on_response = $args{on_response}; | |||||||||||||||||||||||||||
| 228 | 15 | ▶ | ref $on_response eq "CODE" or croak "Expected 'on_response' as a CODE reference"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | 15 | $self->write_message( $message, @$args ); | |||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | 15 | push @{ $self->{message_queue} }, $on_response; | |||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | =head1 METHODS | ||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | =cut | ||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | =head2 $ips->eval( %args ) | ||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | This method evaluates code in the remote host, passing arguments and returning | ||||||||||||||||||||||||||||
| 242 | the result. | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | The C<%args> hash takes the following keys: | ||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | =over 8 | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | =item code => STRING | ||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | The perl code to execute, in a string. (i.e. NOT a CODE reference). | ||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||
| 252 | =item args => ARRAY | ||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | Optional. An ARRAY reference containing arguments to pass to the code. | ||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | =item on_result => CODE | ||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||
| 258 | Continuation to invoke when the code returns a result. | ||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||
| 260 | =item on_exception => CODE | ||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||
| 262 | Optional. Continuation to invoke if the code throws an exception. | ||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | =back | ||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | The code should be passed in a string, and is evaluated using a string | ||||||||||||||||||||||||||||
| 267 | C<eval> in the remote host, in list context. If this method is called in | ||||||||||||||||||||||||||||
| 268 | scalar context, then only the first element of the returned list is returned. | ||||||||||||||||||||||||||||
| 269 | Only string scalar values are supported in either the arguments or the return | ||||||||||||||||||||||||||||
| 270 | values; no deeply-nested structures can be passed. | ||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | To pass or return a more complex structure, consider using a module such as | ||||||||||||||||||||||||||||
| 273 | L<Storable>, which can serialise the structure into a plain string, to be | ||||||||||||||||||||||||||||
| 274 | deserialised on the remote end. | ||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | If the remote code threw an exception, then this function propagates it as a | ||||||||||||||||||||||||||||
| 277 | plain string. If the remote process exits before responding, this will be | ||||||||||||||||||||||||||||
| 278 | propagated as an exception. | ||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | =cut | ||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||
| 282 | sub eval | ||||||||||||||||||||||||||||
| 283 | 10 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
eval: called CC=5 SCAR=16.7
Pod
documented
| |||||||||||||||||||||||||||||
| 284 | 10 | my $self = shift; | |||||||||||||||||||||||||||
| 285 | 10 | my %args = @_; | |||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||
| 287 | 10 | my $code = $args{code}; | |||||||||||||||||||||||||||
| 288 | 10 | my $args = $args{args}; | |||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||
| 290 | 10 | my $on_result = $args{on_result}; | |||||||||||||||||||||||||||
| 291 | 10 | ▶ | ref $on_result eq "CODE" or croak "Expected 'on_result' as a CODE reference"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||
| 293 | 10 | ▶ | my $on_exception = $args{on_exception} || $self->{on_exception}; | ||||||||||||||||||||||||||
|
Condition66%2/3
$args{'on_exception'} || $self->{'on_exception'}
MC/DC0%0/2
$args{'on_exception'} || $self->{'on_exception'}
$args{'on_exception'}
$self->{'on_exception'}
Truth table66%2/3
A || B
A= $args{'on_exception'}B= $self->{'on_exception'}
| |||||||||||||||||||||||||||||
| 294 | 10 | ▶ | ref $on_exception eq "CODE" or croak "Expected 'on_exception' as a CODE reference"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||
| 296 | $self->do_message( | ||||||||||||||||||||||||||||
| 297 | message => "EVAL", | ||||||||||||||||||||||||||||
| 298 | args => [ $code, $args ? @$args : () ], | ||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | 10 | ▶ | on_response => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=15.2
| |||||||||||||||||||||||||||||
| 301 | 10 | my ( $ret, @args ) = @_; | |||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||
| 303 | 10 | ▶ | if( $ret eq "RETURNED" ) { $on_result->( @args ); } | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 304 | 0 | elsif( $ret eq "DIED" ) { $on_exception->( $args[0] ); } | |||||||||||||||||||||||||||
| 305 | 1 | elsif( $ret eq "CLOSED" ) { $on_exception->( "Remote connection closed" ); } | |||||||||||||||||||||||||||
| 306 | 0 | else { warn "Unknown return result $ret"; } | |||||||||||||||||||||||||||
| 307 | }, | ||||||||||||||||||||||||||||
| 308 | 10 | ▶ | ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 309 | } | ||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||
| 311 | =head2 $ips->store( %args ) | ||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||
| 313 | This method sends code to the remote host to store in a named procedure which | ||||||||||||||||||||||||||||
| 314 | can be executed later. | ||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | The C<%args> hash takes the following keys: | ||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||
| 318 | =over 8 | ||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||
| 320 | =item name => STRING | ||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||
| 322 | A name for the stored procedure. | ||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||
| 324 | =item code => STRING | ||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||
| 326 | The perl code to store, in a string. (i.e. NOT a CODE reference). | ||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||
| 328 | =item on_stored => CODE | ||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||
| 330 | Continuation to invoke when the code is successfully stored. | ||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||
| 332 | =item on_exception => CODE | ||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | Optional. Continuation to invoke if compiling the code throws an exception. | ||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||
| 336 | =back | ||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||
| 338 | The code should be passed in a string, along with a name which can later be | ||||||||||||||||||||||||||||
| 339 | called by the C<call> method. | ||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||
| 341 | While the code is not executed, it will still be compiled into a CODE | ||||||||||||||||||||||||||||
| 342 | reference in the remote host. Any compile errors that occur will still invoke | ||||||||||||||||||||||||||||
| 343 | the C<on_exception> continuation. If the remote process exits before | ||||||||||||||||||||||||||||
| 344 | responding, this will be propagated as an exception. | ||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||
| 346 | =cut | ||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||
| 348 | sub store | ||||||||||||||||||||||||||||
| 349 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
store: called CC=5 SCAR=19.0
Pod
documented
| |||||||||||||||||||||||||||||
| 350 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 351 | 1 | my %args = @_; | |||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||
| 353 | 1 | my $name = $args{name}; | |||||||||||||||||||||||||||
| 354 | 1 | my $code = $args{code}; | |||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||
| 356 | 1 | my $on_stored = $args{on_stored}; | |||||||||||||||||||||||||||
| 357 | 1 | ▶ | ref $on_stored eq "CODE" or croak "Expected 'on_stored' as a CODE reference"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||
| 359 | 1 | ▶ | my $on_exception = $args{on_exception} || $self->{on_exception}; | ||||||||||||||||||||||||||
|
Condition33%1/3
$args{'on_exception'} || $self->{'on_exception'}
MC/DC0%0/2
$args{'on_exception'} || $self->{'on_exception'}
$args{'on_exception'}
$self->{'on_exception'}
Truth table33%1/3
A || B
A= $args{'on_exception'}B= $self->{'on_exception'}
| |||||||||||||||||||||||||||||
| 360 | 1 | ▶ | ref $on_exception eq "CODE" or croak "Expected 'on_exception' as a CODE reference"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||
| 362 | 1 | ▶ | $self->_has_stored_code( $name ) and return $on_exception->( "Already have a stored function called '$name'" ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||
| 364 | $self->do_message( | ||||||||||||||||||||||||||||
| 365 | message => "STORE", | ||||||||||||||||||||||||||||
| 366 | args => [ $name, $code ], | ||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||
| 368 | 1 | ▶ | on_response => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=20.4
| |||||||||||||||||||||||||||||
| 369 | 1 | my ( $ret, @args ) = @_; | |||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||
| 371 | 1 | ▶ | if( $ret eq "OK" ) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 372 | 1 | $self->{stored}{$name} = 1; | |||||||||||||||||||||||||||
| 373 | 1 | $on_stored->(); | |||||||||||||||||||||||||||
| 374 | } | ||||||||||||||||||||||||||||
| 375 | 0 | elsif( $ret eq "DIED" ) { $on_exception->( $args[0] ); } | |||||||||||||||||||||||||||
| 376 | 0 | elsif( $ret eq "CLOSED" ) { $on_exception->( "Remote connection closed" ); } | |||||||||||||||||||||||||||
| 377 | 0 | else { warn "Unknown return result $ret"; } | |||||||||||||||||||||||||||
| 378 | }, | ||||||||||||||||||||||||||||
| 379 | 1 | ); | |||||||||||||||||||||||||||
| 380 | } | ||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||
| 382 | sub _has_stored_code | ||||||||||||||||||||||||||||
| 383 | 5 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_has_stored_code: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 384 | 5 | my $self = shift; | |||||||||||||||||||||||||||
| 385 | 5 | my ( $name ) = @_; | |||||||||||||||||||||||||||
| 386 | 5 | return exists $self->{stored}{$name}; | |||||||||||||||||||||||||||
| 387 | } | ||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||
| 389 | =head2 $ips->call( %args ) | ||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||
| 391 | This method invokes a stored procedure that has earlier been defined using the | ||||||||||||||||||||||||||||
| 392 | C<store> method. The arguments are passed and the result is returned in the | ||||||||||||||||||||||||||||
| 393 | same way as with the C<eval> method. | ||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||
| 395 | The C<%params> hash takes the following keys: | ||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||
| 397 | =over 8 | ||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||
| 399 | =item name => STRING | ||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||
| 401 | The name of the stored procedure. | ||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||
| 403 | =item args => ARRAY | ||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||
| 405 | Optional. An ARRAY reference containing arguments to pass to the code. | ||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||
| 407 | =item on_result => CODE | ||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||
| 409 | Continuation to invoke when the code returns a result. | ||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||
| 411 | =item on_exception => CODE | ||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||
| 413 | Optional. Continuation to invoke if the code throws an exception or exits. | ||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||
| 415 | =back | ||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||
| 417 | =cut | ||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||
| 419 | sub call | ||||||||||||||||||||||||||||
| 420 | 2 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
call: called CC=6 SCAR=21.7
Pod
documented
| |||||||||||||||||||||||||||||
| 421 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 422 | 2 | my %args = @_; | |||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||
| 424 | 2 | my $name = $args{name}; | |||||||||||||||||||||||||||
| 425 | 2 | my $args = $args{args}; | |||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||
| 427 | 2 | my $on_result = $args{on_result}; | |||||||||||||||||||||||||||
| 428 | 2 | ▶ | ref $on_result eq "CODE" or croak "Expected 'on_result' as a CODE reference"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||
| 430 | 2 | ▶ | my $on_exception = $args{on_exception} || $self->{on_exception}; | ||||||||||||||||||||||||||
|
Condition33%1/3
$args{'on_exception'} || $self->{'on_exception'}
MC/DC0%0/2
$args{'on_exception'} || $self->{'on_exception'}
$args{'on_exception'}
$self->{'on_exception'}
Truth table33%1/3
A || B
A= $args{'on_exception'}B= $self->{'on_exception'}
| |||||||||||||||||||||||||||||
| 431 | 2 | ▶ | ref $on_exception eq "CODE" or croak "Expected 'on_exception' as a CODE reference"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||
| 433 | 2 | ▶ | $self->_has_stored_code( $name ) or return $on_exception->( "Do not have a stored function called '$name'" ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||
| 435 | $self->do_message( | ||||||||||||||||||||||||||||
| 436 | message => "CALL", | ||||||||||||||||||||||||||||
| 437 | args => [ $name, $args ? @$args : () ], | ||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||
| 439 | 2 | ▶ | on_response => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=21.7
| |||||||||||||||||||||||||||||
| 440 | 2 | my ( $ret, @args ) = @_; | |||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||
| 442 | 2 | ▶ | if( $ret eq "RETURNED" ) { $on_result->( @args ); } | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 443 | 0 | elsif( $ret eq "DIED" ) { $on_exception->( $args[0] ); } | |||||||||||||||||||||||||||
| 444 | 0 | elsif( $ret eq "CLOSED" ) { $on_exception->( "Remote connection closed" ); } | |||||||||||||||||||||||||||
| 445 | 0 | else { warn "Unknown return result $ret"; } | |||||||||||||||||||||||||||
| 446 | }, | ||||||||||||||||||||||||||||
| 447 | 2 | ▶ | ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 448 | } | ||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||
| 450 | =head2 $ips->use_library( %args ) | ||||||||||||||||||||||||||||
| 451 | |||||||||||||||||||||||||||||
| 452 | This method loads a library of code from a module, and stores them to the | ||||||||||||||||||||||||||||
| 453 | remote perl by calling C<store> on each one. | ||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||
| 455 | The C<%params> hash takes the following keys: | ||||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||
| 457 | =over 8 | ||||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||
| 459 | =item library => STRING | ||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||
| 461 | Name of the library to load | ||||||||||||||||||||||||||||
| 462 | |||||||||||||||||||||||||||||
| 463 | =item funcs => ARRAY | ||||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||
| 465 | Optional. Reference to an array containing names of functions to load. | ||||||||||||||||||||||||||||
| 466 | |||||||||||||||||||||||||||||
| 467 | =item on_loaded => CODE | ||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||
| 469 | Continuation to invoke when all the functions are stored. | ||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||
| 471 | =item on_exception => CODE | ||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||
| 473 | Optional. Continuation to invoke if storing a function throws an exception or | ||||||||||||||||||||||||||||
| 474 | exits. | ||||||||||||||||||||||||||||
| 475 | |||||||||||||||||||||||||||||
| 476 | =back | ||||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||
| 478 | The library name may be a full class name, or a name within the | ||||||||||||||||||||||||||||
| 479 | C<IPC::PerlSSH::Library::> space. | ||||||||||||||||||||||||||||
| 480 | |||||||||||||||||||||||||||||
| 481 | If the funcs list is non-empty, then only those named functions are stored | ||||||||||||||||||||||||||||
| 482 | (analogous to the C<use> perl statement). This may be useful in large | ||||||||||||||||||||||||||||
| 483 | libraries that define many functions, only a few of which are actually used. | ||||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||
| 485 | For more information, see L<IPC::PerlSSH::Library>. | ||||||||||||||||||||||||||||
| 486 | |||||||||||||||||||||||||||||
| 487 | =cut | ||||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||
| 489 | sub use_library | ||||||||||||||||||||||||||||
| 490 | 4 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
use_library: called CC=7 SCAR=20.6
Pod
documented
| |||||||||||||||||||||||||||||
| 491 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 492 | 4 | my %args = @_; | |||||||||||||||||||||||||||
| 493 | |||||||||||||||||||||||||||||
| 494 | 4 | my $library = $args{library}; | |||||||||||||||||||||||||||
| 495 | 4 | my $funcs = $args{funcs}; | |||||||||||||||||||||||||||
| 496 | |||||||||||||||||||||||||||||
| 497 | 4 | my $on_loaded = $args{on_loaded}; | |||||||||||||||||||||||||||
| 498 | 4 | ▶ | ref $on_loaded eq "CODE" or croak "Expected 'on_loaded' as a CODE reference"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 499 | |||||||||||||||||||||||||||||
| 500 | 4 | ▶ | my $on_exception = $args{on_exception} || $self->{on_exception}; | ||||||||||||||||||||||||||
|
Condition66%2/3
$args{'on_exception'} || $self->{'on_exception'}
MC/DC0%0/2
$args{'on_exception'} || $self->{'on_exception'}
$args{'on_exception'}
$self->{'on_exception'}
Truth table66%2/3
A || B
A= $args{'on_exception'}B= $self->{'on_exception'}
| |||||||||||||||||||||||||||||
| 501 | 4 | ▶ | ref $on_exception eq "CODE" or croak "Expected 'on_exception' as a CODE reference"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 502 | |||||||||||||||||||||||||||||
| 503 | 4 | ▶ | my ( $package, $funcshash ) = eval { $self->load_library_pkg( $library, $funcs ? @$funcs : () ) }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 504 | 4 | ▶ | if( $@ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 505 | 2 | $on_exception->( $@ ); | |||||||||||||||||||||||||||
| 506 | 2 | return; | |||||||||||||||||||||||||||
| 507 | } | ||||||||||||||||||||||||||||
| 508 | |||||||||||||||||||||||||||||
| 509 | 2 | ▶ | $self->{stored_pkg}{$package} and delete $funcshash->{_init}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 510 | |||||||||||||||||||||||||||||
| 511 | $self->do_message( | ||||||||||||||||||||||||||||
| 512 | message => "STOREPKG", | ||||||||||||||||||||||||||||
| 513 | args => [ $package, %$funcshash ], | ||||||||||||||||||||||||||||
| 514 | |||||||||||||||||||||||||||||
| 515 | 2 | ▶ | on_response => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=22.7
| |||||||||||||||||||||||||||||
| 516 | 2 | my ( $ret, @args ) = @_; | |||||||||||||||||||||||||||
| 517 | |||||||||||||||||||||||||||||
| 518 | 2 | ▶ | if( $ret eq "OK" ) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 519 | 2 | $self->{stored_pkg}{$package} = 1; | |||||||||||||||||||||||||||
| 520 | 2 | $self->{stored}{$_} = 1 for keys %$funcshash; | |||||||||||||||||||||||||||
| 521 | 2 | $on_loaded->(); | |||||||||||||||||||||||||||
| 522 | } | ||||||||||||||||||||||||||||
| 523 | 0 | elsif( $ret eq "DIED" ) { $on_exception->( $args[0] ); } | |||||||||||||||||||||||||||
| 524 | 0 | elsif( $ret eq "CLOSED" ) { $on_exception->( "Remote connection closed" ); } | |||||||||||||||||||||||||||
| 525 | 0 | else { warn "Unknown return result $ret"; } | |||||||||||||||||||||||||||
| 526 | }, | ||||||||||||||||||||||||||||
| 527 | 2 | ); | |||||||||||||||||||||||||||
| 528 | } | ||||||||||||||||||||||||||||
| 529 | |||||||||||||||||||||||||||||
| 530 | sub DESTROY | ||||||||||||||||||||||||||||
| 531 | 2 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||
| 532 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 533 | |||||||||||||||||||||||||||||
| 534 | # Be safe at global destruction time | ||||||||||||||||||||||||||||
| 535 | 2 | ▶ | $self->{stream}->close if defined $self->{stream}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 536 | } | ||||||||||||||||||||||||||||
| 537 | |||||||||||||||||||||||||||||
| 538 | # Keep perl happy; keep Britain tidy | ||||||||||||||||||||||||||||
| 539 | 6 | 1; | |||||||||||||||||||||||||||
| 540 | |||||||||||||||||||||||||||||
| 541 | __END__ | ||||||||||||||||||||||||||||