File Coverage

blib/lib/Net/CLI/Interact/Action.pm
Criterion Covered Total %
statement 12 25 48.0
branch 0 8 0.0
condition 0 3 0.0
subroutine 4 7 57.1
pod 2 3 66.6
total 18 46 39.1


line stmt bran cond sub pod time code
1             package Net::CLI::Interact::Action;
2             { $Net::CLI::Interact::Action::VERSION = '2.300005' }
3              
4 1     1   7 use Moo;
  1         2  
  1         6  
5 1     1   304 use Sub::Quote;
  1         3  
  1         60  
6 1     1   6 use MooX::Types::MooseLike::Base qw(Any Bool Str ArrayRef InstanceOf RegexpRef);
  1         2  
  1         72  
7 1     1   7 use Net::CLI::Interact::ActionSet;
  1         2  
  1         433  
8              
9             has 'type' => (
10             is => 'ro',
11             isa => quote_sub(
12             q{ die "$_[0] not send/match" unless $_[0] =~ m/^(?:send|match)$/ }),
13             required => 1,
14             );
15              
16             has 'value' => (
17             is => 'ro',
18             isa => Any, # FIXME 'Str|ArrayRef[RegexpRef]',
19             required => 1,
20             );
21              
22             has 'no_ors' => (
23             is => 'ro',
24             isa => Bool,
25             default => quote_sub('0'),
26             );
27              
28             has 'continuation' => (
29             is => 'rw',
30             isa => InstanceOf['Net::CLI::Interact::ActionSet'],
31             );
32              
33             has 'params' => (
34             is => 'rw',
35             isa => ArrayRef,
36             default => sub { [] },
37             );
38              
39             has 'response' => (
40             is => 'rw',
41             isa => Str,
42             default => quote_sub(q{''}),
43             );
44              
45             has 'response_stash' => (
46             is => 'rw',
47             isa => Str,
48             default => quote_sub(q{''}),
49             );
50              
51             has 'prompt_hit' => (
52             is => 'rw',
53             isa => RegexpRef,
54             );
55              
56             sub BUILDARGS {
57 0     0 0   my ($class, @rest) = @_;
58              
59             # accept single hash ref or naked hash
60 0 0         my $params = (ref {} eq ref $rest[0] ? $rest[0] : {@rest});
61              
62 0 0 0       if (exists $params->{continuation} and ref [] eq ref $params->{continuation}) {
63             $params->{continuation} = Net::CLI::Interact::ActionSet->new({
64             actions => $params->{continuation},
65 0           });
66             }
67              
68 0           return $params;
69             }
70              
71             # Only a shallow copy so all the reference based slots still
72             # share data with the original Action's slots.
73             #
74             # I asked in #web-simple and was told that if the object is more magical than
75             # a simple hashref, then someone is trying to be far too clever. agreed!
76             sub clone {
77 0     0 1   my $self = shift;
78 0 0         bless({ %{$self}, %{(shift) || {}} }, ref $self)
  0            
  0            
79             }
80              
81             # count the number of sprintf parameters used in the value
82             sub num_params {
83 0     0 1   my $self = shift;
84 0 0         return 0 if ref $self->value;
85             # this tricksy little number comes from the Perl FAQ
86 0           my $count = () = $self->value =~ m/(?<!%)%/g;
87 0           return $count;
88             }
89              
90             1;
91              
92             =pod
93              
94             =head1 NAME
95              
96             Net::CLI::Interact::Action - Sent data or matched response from connected device
97              
98             =head1 DESCRIPTION
99              
100             This class is used internally by L<Net::CLI::Interact> and it's unlikely that
101             an end-user will need to make use of Action objects directly. The interface is
102             documented here as a matter of record.
103              
104             An Action object represents I<either> some kind of text or command to send to
105             a connected device, I<or> a regular expression matching the response from a
106             connected device. Such Actions are built up into ActionSets which describe a
107             conversation with the connected device.
108              
109             If the Action is a C<send> type, then after execution it can be cloned and
110             augmented with the response text of the command. If the response is likely to
111             be paged, then the Action may also store instruction in how to trigger and
112             consume the pages.
113              
114             =head1 INTERFACE
115              
116             =head2 type
117              
118             Denotes the kind of Action, which may be C<send> or C<match>.
119              
120             =head2 value
121              
122             In the case of C<send>, a String command to send to the device. In the case of
123             C<match>, a regular expression reference to match response from the device. In
124             special circumstances an array reference of regular expression references is
125             also valid, and each will be checked for a match against the device response.
126              
127             =head2 no_ors
128              
129             Only applies to the C<send> kind. Whether to skip appending the I<output
130             record separator> (newline) to the C<send> command when sent to the connected
131             device.
132              
133             =head2 continuation
134              
135             Only applies to the C<send> kind. When response output is likely to be paged,
136             this stores an L<ActionSet|Net::CLI::Interact::ActionSet> that contains two
137             Actions: one for the C<match> which indicates output has paused at the end of
138             a page, and one for the C<send> command which triggers printing of the next
139             page.
140              
141             =head2 params
142              
143             Only applies to the C<send> kind, and contains a list of parameters which are
144             substituted into the C<value> using Perl's C<sprintf> function. Insufficient
145             parameters causes C<sprintf> to die.
146              
147             =head2 num_params
148              
149             Only applies to the C<send> kind, and returns the number of parameters which
150             are required for the current C<value>. Used for error checking when setting
151             C<params>.
152              
153             =head2 response
154              
155             A stash for the returned prompt which matched and triggered the end of this
156             action.
157              
158             =head2 response_stash
159              
160             A stash for the returned output following a C<send> command, but not including
161             the matched prompt which ended the action. This slot is used by the C<match>
162             action as it slurps output, but the content is then transferred over to the
163             partner C<send> in the ActionSet.
164              
165             =head2 prompt_hit
166              
167             When a command is successfully issued, the response is terminated by a prompt.
168             However that prompt can be one of a list, defined in the Action. This slot
169             records the regular expression from that list which was actually matched.
170              
171             =head2 clone
172              
173             Returns a new Action, which is a shallow clone of the existing one. All the
174             reference based slots will share data, but you can add (for example) a
175             C<response> without affecting the original Action. Used when preparing to
176             execute an Action which has been retrieved from the
177             L<Phrasebook|Net::CLI::Interact::Phrasebook>.
178              
179             =cut