Term::CLI::Role::State - Keep a "state" hash in Term::CLI objects
version 0.059000
package Term::CLI::CommandSet { use Moo; with('Term::CLI::Role::State'); } ... my $cmd = Term::CLI::Command->new( ... ); $cmd->state->{'key'} = $val;
Role for Term::CLI::CommandSet(3p) elements to represent state across readline operations.
This role is consumed by Term::CLI::CommandSet(3p) and thus available in Term::CLI::Command(3p) and Term::CLI(3p) object instances.
This role defines one additional attribute:
Reference to an hash containing any information you want.
Return the HashRef representing the state.
HashRef
Clear the state hash, such that subsequent calls to state still return the same HashRef, but without any contents.
E.g.:
my $cli = Term::CLI->new(); my $hash = $cli->state(); $cli->state()->{'foo'} = 'foo'; $hash->{'bar'} = 'bar'; say scalar keys %{ $hash }; # prints "2" say scalar keys %{ $cli->state }; # prints "2" $cli->clear_state(); say scalar keys %{ $hash }; # prints "0" say scalar keys %{ $cli->state }; # prints "0"
See examples/state_demo.pl in the source distribution for an example of how to keep state in the Term::CLI object.
Term::CLI
Term::CLI(3p), Term::CLI::Command(3p), Term::CLI::CommandSet(3p).
Example script that demonstrates how to keep in the Term::CLI object.
Steven Bakker <sbakker@cpan.org>, 2022.
Copyright (c) 2022 Steven Bakker
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See "perldoc perlartistic."
This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
To install Term::CLI, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Term::CLI
CPAN shell
perl -MCPAN -e shell install Term::CLI
For more information on module installation, please visit the detailed CPAN module installation guide.