File: Action.pm

package info (click to toggle)
libnet-cli-interact-perl 2.142720-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 420 kB
  • ctags: 127
  • sloc: perl: 1,907; makefile: 2
file content (201 lines) | stat: -rw-r--r-- 5,556 bytes parent folder | download
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package Net::CLI::Interact::Action;
{
  $Net::CLI::Interact::Action::VERSION = '2.142720';
}

use Moo;
use Sub::Quote;
use MooX::Types::MooseLike::Base qw(Any Bool Str ArrayRef InstanceOf RegexpRef);
use Net::CLI::Interact::ActionSet;

has 'type' => (
    is => 'ro',
    isa => quote_sub(
        q{ die "$_[0] not send/match" unless $_[0] =~ m/^(?:send|match)$/ }),
    required => 1,
);

has 'value' => (
    is => 'ro',
    isa => Any, # FIXME 'Str|ArrayRef[RegexpRef]',
    required => 1,
);

has 'no_ors' => (
    is => 'ro',
    isa => Bool,
    default => quote_sub('0'),
);

has 'continuation' => (
    is => 'rw',
    isa => InstanceOf['Net::CLI::Interact::ActionSet'],
);

has 'params' => (
    is => 'rw',
    isa => ArrayRef,
    default => sub { [] },
);

has 'response' => (
    is => 'rw',
    isa => Str,
    default => quote_sub(q{''}),
);

has 'response_stash' => (
    is => 'rw',
    isa => Str,
    default => quote_sub(q{''}),
);

has 'prompt_hit' => (
    is => 'rw',
    isa => RegexpRef,
);

sub BUILDARGS {
    my ($class, @rest) = @_;

    # accept single hash ref or naked hash
    my $params = (ref {} eq ref $rest[0] ? $rest[0] : {@rest});

    if (exists $params->{continuation} and ref [] eq ref $params->{continuation}) {
        $params->{continuation} = Net::CLI::Interact::ActionSet->new({
            actions => $params->{continuation},
        });
    }

    return $params;
}

# Only a shallow copy so all the reference based slots still
# share data with the original Action's slots.
# 
# I asked in #web-simple and was told that if the object is more magical than
# a simple hashref, then someone is trying to be far too clever. agreed!
sub clone {
    my $self = shift;
    bless({ %{$self}, %{(shift) || {}} }, ref $self)
}

# count the number of sprintf parameters used in the value
sub num_params {
    my $self = shift;
    return 0 if ref $self->value;
    # this tricksy little number comes from the Perl FAQ
    my $count = () = $self->value =~ m/(?<!%)%/g;
    return $count;
}

1;

# ABSTRACT: Sent data or matched response from connected device


__END__
=pod

=head1 NAME

Net::CLI::Interact::Action - Sent data or matched response from connected device

=head1 VERSION

version 2.142720

=head1 DESCRIPTION

This class is used internally by L<Net::CLI::Interact> and it's unlikely that
an end-user will need to make use of Action objects directly. The interface is
documented here as a matter of record.

An Action object represents I<either> some kind of text or command to send to
a connected device, I<or> a regular expression matching the response from a
connected device. Such Actions are built up into ActionSets which describe a
conversation with the connected device.

If the Action is a C<send> type, then after execution it can be cloned and
augmented with the response text of the command. If the response is likely to
be paged, then the Action may also store instruction in how to trigger and
consume the pages.

=head1 INTERFACE

=head2 type

Denotes the kind of Action, which may be C<send> or C<match>.

=head2 value

In the case of C<send>, a String command to send to the device. In the case of
C<match>, a regular expression reference to match response from the device. In
special circumstances an array reference of regular expression references is
also valid, and each will be checked for a match against the device response.

=head2 no_ors

Only applies to the C<send> kind. Whether to skip appending the I<output
record separator> (newline) to the C<send> command when sent to the connected
device.

=head2 continuation

Only applies to the C<send> kind. When response output is likely to be paged,
this stores an L<ActionSet|Net::CLI::Interact::ActionSet> that contains two
Actions: one for the C<match> which indicates output has paused at the end of
a page, and one for the C<send> command which triggers printing of the next
page.

=head2 params

Only applies to the C<send> kind, and contains a list of parameters which are
substituted into the C<value> using Perl's C<sprintf> function. Insufficient
parameters causes C<sprintf> to die.

=head2 num_params

Only applies to the C<send> kind, and returns the number of parameters which
are required for the current C<value>. Used for error checking when setting
C<params>.

=head2 response

A stash for the returned prompt which matched and triggered the end of this
action.

=head2 response_stash

A stash for the returned output following a C<send> command, but not including
the matched prompt which ended the action. This slot is used by the C<match>
action as it slurps output, but the content is then transferred over to the
partner C<send> in the ActionSet.

=head2 prompt_hit

When a command is successfully issued, the response is terminated by a prompt.
However that prompt can be one of a list, defined in the Action. This slot
records the regular expression from that list which was actually matched.

=head2 clone

Returns a new Action, which is a shallow clone of the existing one. All the
reference based slots will share data, but you can add (for example) a
C<response> without affecting the original Action. Used when preparing to
execute an Action which has been retrieved from the
L<Phrasebook|Net::CLI::Interact::Phrasebook>.

=head1 AUTHOR

Oliver Gorwits <oliver@cpan.org>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2014 by Oliver Gorwits.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut