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