Net-CLI-Interact
view release on metacpan or search on metacpan
lib/Net/CLI/Interact/Role/Engine.pm view on Meta::CPAN
default => quote_sub('0'),
);
has 'params' => (
is => 'ro',
isa => ArrayRef[Str],
predicate => 1,
);
has 'timeout' => (
is => 'ro',
isa => quote_sub(q{die "$_[0] is not a posint!" unless $_[0] > 0 }),
);
has 'match' => (
is => 'rw',
isa => ArrayRef, # FIXME ArrayRef[RegexpRef|Str]
predicate => 1,
coerce => quote_sub(q{ (ref [] ne ref $_[0]) ? [$_[0]] : $_[0] }),
);
}
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
use Moo::Role;
use MooX::Types::MooseLike::Base qw(InstanceOf);
with 'Net::CLI::Interact::Role::Prompt';
use Net::CLI::Interact::Action;
use Net::CLI::Interact::ActionSet;
use Class::Load ();
# try to load Data::Printer for last_actionset debug output
if (Class::Load::try_load_class('Data::Printer', {-version => '0.27'})) {
Data::Printer->import({class => { expand => 'all' }});
}
has 'last_actionset' => (
is => 'rw',
isa => InstanceOf['Net::CLI::Interact::ActionSet'],
trigger => 1,
);
sub _trigger_last_actionset {
my ($self, $new) = @_;
$self->logger->log('prompt', 'notice',
sprintf ('output matching prompt was "%s"', $new->item_at(-1)->response));
if (Class::Load::is_class_loaded('Data::Printer', {-version => '0.27'})) {
Data::Printer::p($new, output => \my $debug);
$self->logger->log('object', 'debug', $debug);
}
}
sub last_response {
my $self = shift;
my $irs_re = $self->transport->irs_re;
(my $resp = $self->last_actionset->item_at(-2)->response) =~ s/$irs_re/\n/g;
$resp =~ s/\n+$//;
return (wantarray
? (split m/^/, $resp)
: ($resp ."\n"));
}
has 'default_continuation' => (
is => 'rw',
isa => InstanceOf['Net::CLI::Interact::ActionSet'],
writer => '_default_continuation',
predicate => 1,
clearer => 1,
);
sub set_default_continuation {
my ($self, $cont) = @_;
die "missing continuation" unless $cont;
die "unknown continuation [$cont]" unless
eval{ $self->phrasebook->macro($cont) };
$self->_default_continuation( $self->phrasebook->macro($cont) );
$self->logger->log('engine', 'info', 'default continuation set to', $cont);
}
sub cmd {
my ($self, $command, $options) = @_;
$options = Net::CLI::Interact::Role::Engine::ExecuteOptions->new($options || {});
$self->logger->log('engine', 'notice', 'running command', $command);
if ($options->has_match) {
# convert prompt name(s) from name into regexpref, or die
$options->match([
map { ref $_ eq ref '' ? @{ $self->phrasebook->prompt($_)->first->value }
: $_ }
@{ $options->match }
]);
$self->logger->log('engine', 'info', 'to match',
(ref $options->match eq ref [] ? (join '|', @{$options->match})
: $options->match));
}
# command will be run through sprintf but without any params
# so convert any embedded % to literal %
($command =~ s/%/%%/g) &&
$self->logger->log('engine', 'debug', 'command expanded to:', $command);
return $self->_execute_actions(
$options,
Net::CLI::Interact::Action->new({
type => 'send',
value => $command,
no_ors => $options->no_ors,
}),
);
}
sub macro {
my ($self, $name, $options) = @_;
$options = Net::CLI::Interact::Role::Engine::ExecuteOptions->new($options || {});
$self->logger->log('engine', 'notice', 'running macro', $name);
$self->logger->log('engine', 'info', 'macro params are:',
( run in 1.383 second using v1.01-cache-2.11-cpan-71847e10f99 )