| 1 | #============================================================================= | ||
| 2 | # | ||
| 3 | # Module: Term::CLI::State | ||
| 4 | # | ||
| 5 | # Description: Role for keeping state in Term::CLI objects. | ||
| 6 | # | ||
| 7 | # Author: Steven Bakker (SBAKKER), <sbakker@cpan.org> | ||
| 8 | # Created: 09/02/2022 | ||
| 9 | # | ||
| 10 | # Copyright (c) 2022 Steven Bakker | ||
| 11 | # | ||
| 12 | # This module is free software; you can redistribute it and/or modify | ||
| 13 | # it under the same terms as Perl itself. See "perldoc perlartistic." | ||
| 14 | # | ||
| 15 | # This software is distributed in the hope that it will be useful, | ||
| 16 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 17 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
| 18 | # | ||
| 19 | #============================================================================= | ||
| 20 | |||
| 21 | package Term::CLI::Role::State 0.061000; | ||
| 22 | |||
| 23 | 7 | ▶ | use 5.014; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 24 | 7 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 25 | |||
| 26 | 7 | use Types::Standard 1.000005 qw( | |
| 27 | HashRef | ||
| 28 | 7 | ▶ | ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 29 | |||
| 30 | 7 | ▶ | use Moo::Role; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 31 | 7 | ▶ | use namespace::clean 0.25; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 32 | |||
| 33 | has state => ( | ||
| 34 | is => 'lazy', | ||
| 35 | isa => HashRef, | ||
| 36 | 2 | ▶ | default => sub { {} }, |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 37 | 7 | ); | |
| 38 | |||
| 39 | sub clear_state { | ||
| 40 | 1 | ▶ | my ($self) = @_; |
|
Subroutine
clear_state: called CC=1 SCAR=0.0
Pod
documented
| |||
| 41 | 1 | %{ $self->state } = (); | |
| 42 | } | ||
| 43 | |||
| 44 | 7 | 1; | |
| 45 | |||
| 46 | __END__ | ||
| 47 | |||
| 48 | =pod | ||
| 49 | |||
| 50 | =head1 NAME | ||
| 51 | |||
| 52 | Term::CLI::Role::State - Keep a "state" hash in Term::CLI objects | ||
| 53 | |||
| 54 | =head1 VERSION | ||
| 55 | |||
| 56 | version 0.061000 | ||
| 57 | |||
| 58 | =head1 SYNOPSIS | ||
| 59 | |||
| 60 | package Term::CLI::CommandSet { | ||
| 61 | |||
| 62 | use Moo; | ||
| 63 | |||
| 64 | with('Term::CLI::Role::State'); | ||
| 65 | } | ||
| 66 | |||
| 67 | ... | ||
| 68 | |||
| 69 | my $cmd = Term::CLI::Command->new( ... ); | ||
| 70 | |||
| 71 | $cmd->state->{'key'} = $val; | ||
| 72 | |||
| 73 | =head1 DESCRIPTION | ||
| 74 | |||
| 75 | Role for L<Term::CLI::CommandSet|Term::CLI::CommandSet>(3p) elements to represent | ||
| 76 | state across readline operations. | ||
| 77 | |||
| 78 | This role is consumed by | ||
| 79 | L<Term::CLI::CommandSet|Term::CLI::CommandSet>(3p) and thus available | ||
| 80 | in L<Term::CLI::Command|Term::CLI::Command>(3p) and L<Term::CLI>(3p) | ||
| 81 | object instances. | ||
| 82 | |||
| 83 | =head1 ATTRIBUTES | ||
| 84 | |||
| 85 | This role defines one additional attribute: | ||
| 86 | |||
| 87 | =over | ||
| 88 | |||
| 89 | =item B<state> =E<gt> I<HashRef> | ||
| 90 | |||
| 91 | Reference to an hash containing any information you want. | ||
| 92 | |||
| 93 | =back | ||
| 94 | |||
| 95 | =head1 ACCESSORS | ||
| 96 | |||
| 97 | =over | ||
| 98 | |||
| 99 | =item B<state> | ||
| 100 | X<state> | ||
| 101 | |||
| 102 | Return the C<HashRef> representing the state. | ||
| 103 | |||
| 104 | =back | ||
| 105 | |||
| 106 | =head1 METHODS | ||
| 107 | |||
| 108 | =over | ||
| 109 | |||
| 110 | =item B<clear_state> | ||
| 111 | X<clear_state> | ||
| 112 | |||
| 113 | Clear the state hash, such that subsequent calls to L<state|/state> | ||
| 114 | still return the same C<HashRef>, but without any contents. | ||
| 115 | |||
| 116 | E.g.: | ||
| 117 | |||
| 118 | my $cli = Term::CLI->new(); | ||
| 119 | my $hash = $cli->state(); | ||
| 120 | |||
| 121 | $cli->state()->{'foo'} = 'foo'; | ||
| 122 | $hash->{'bar'} = 'bar'; | ||
| 123 | |||
| 124 | say scalar keys %{ $hash }; # prints "2" | ||
| 125 | say scalar keys %{ $cli->state }; # prints "2" | ||
| 126 | |||
| 127 | $cli->clear_state(); | ||
| 128 | |||
| 129 | say scalar keys %{ $hash }; # prints "0" | ||
| 130 | say scalar keys %{ $cli->state }; # prints "0" | ||
| 131 | |||
| 132 | =back | ||
| 133 | |||
| 134 | =head1 EXAMPLE | ||
| 135 | |||
| 136 | See F<examples/state_demo.pl> in the source distribution for an example of | ||
| 137 | how to keep state in the C<Term::CLI> object. | ||
| 138 | |||
| 139 | =head1 SEE ALSO | ||
| 140 | |||
| 141 | L<Term::CLI>(3p), | ||
| 142 | L<Term::CLI::Command>(3p), | ||
| 143 | L<Term::CLI::CommandSet>(3p). | ||
| 144 | |||
| 145 | =head1 FILES | ||
| 146 | |||
| 147 | =over | ||
| 148 | |||
| 149 | =item F<examples/state_demo.pl> | ||
| 150 | |||
| 151 | Example script that demonstrates how to keep in the C<Term::CLI> object. | ||
| 152 | |||
| 153 | =back | ||
| 154 | |||
| 155 | =head1 AUTHOR | ||
| 156 | |||
| 157 | Steven Bakker E<lt>sbakker@cpan.orgE<gt>, 2022. | ||
| 158 | |||
| 159 | =head1 COPYRIGHT AND LICENSE | ||
| 160 | |||
| 161 | Copyright (c) 2022 Steven Bakker | ||
| 162 | |||
| 163 | This module is free software; you can redistribute it and/or modify | ||
| 164 | it under the same terms as Perl itself. See "perldoc perlartistic." | ||
| 165 | |||
| 166 | This software is distributed in the hope that it will be useful, | ||
| 167 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 168 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
| 169 | |||
| 170 | =begin __PODCOVERAGE | ||
| 171 | |||
| 172 | =head1 THIS SECTION SHOULD BE HIDDEN | ||
| 173 | |||
| 174 | This section is meant for methods that should not be considered | ||
| 175 | for coverage. This typically includes things like BUILD and DEMOLISH from | ||
| 176 | Moo/Moose. It is possible to skip these when using the Pod::Coverage class | ||
| 177 | (using C<also_private>), but this is not an option when running C<cover> | ||
| 178 | from the command line. | ||
| 179 | |||
| 180 | The simplest trick is to add a hidden section with an item list containing | ||
| 181 | these methods. | ||
| 182 | |||
| 183 | =over | ||
| 184 | |||
| 185 | =item BUILD | ||
| 186 | |||
| 187 | =item DEMOLISH | ||
| 188 | |||
| 189 | =back | ||
| 190 | |||
| 191 | =end __PODCOVERAGE | ||
| 192 | |||
| 193 | =cut | ||