1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
package CuptInteractive;
use strict;
use warnings;
use Expect::Simple;
sub new {
my ($class, $command, $prompt) = @_;
my $o = {};
$o->{_impl} = Expect::Simple->new({
'Cmd' => $command,
'Prompt' => $prompt//'cupt> ',
'DisconnectCmd' => 'q',
'Timeout' => '20',
});
$o->{_error} = '';
my $self = bless($o, $class);
$self->{_initial_output} = $self->_last_output();
return $self;
}
sub initial_output {
my $self = shift;
return $self->{_initial_output};
}
sub execute {
my ($self, $text) = @_;
if ($self->{_error}) {
return 'Previously: ' . $self->{_error};
}
eval {
$self->{_impl}->send($text);
};
if ($@) {
$self->{_error} = $self->{_impl}->before() . "\n\n" . $@;
return $self->{_error};
}
my $result = $self->_last_output();
$result =~ s/^\Q$text\E\n//;
return $result;
}
sub _last_output {
my $self = shift;
my $result = $self->{_impl}->before();
$result =~ s/\r//g;
return $result;
}
1;
|