NAME

Term::CLI::Role::State - Keep a "state" hash in Term::CLI objects

VERSION

version 0.059000

SYNOPSIS

package Term::CLI::CommandSet {

   use Moo;

   with('Term::CLI::Role::State');
}

...

my $cmd = Term::CLI::Command->new( ... );

$cmd->state->{'key'} = $val;

DESCRIPTION

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.

ATTRIBUTES

This role defines one additional attribute:

state => HashRef

Reference to an hash containing any information you want.

ACCESSORS

state

Return the HashRef representing the state.

METHODS

clear_state

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"

EXAMPLE

See examples/state_demo.pl in the source distribution for an example of how to keep state in the Term::CLI object.

SEE ALSO

Term::CLI(3p), Term::CLI::Command(3p), Term::CLI::CommandSet(3p).

FILES

examples/state_demo.pl

Example script that demonstrates how to keep in the Term::CLI object.

AUTHOR

Steven Bakker <sbakker@cpan.org>, 2022.

COPYRIGHT AND LICENSE

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.