AppConfig
view release on metacpan or search on metacpan
lib/AppConfig/State.pm view on Meta::CPAN
#============================================================================
#
# AppConfig::State.pm
#
# Perl5 module in which configuration information for an application can
# be stored and manipulated. AppConfig::State objects maintain knowledge
# about variables; their identities, options, aliases, targets, callbacks
# and so on. This module is used by a number of other AppConfig::* modules.
#
# Written by Andy Wardley <abw@wardley.org>
#
# Copyright (C) 1997-2007 Andy Wardley. All Rights Reserved.
# Copyright (C) 1997,1998 Canon Research Centre Europe Ltd.
#
#----------------------------------------------------------------------------
#
# TODO
#
# * Change varlist() to varhash() and provide another varlist() method
# which returns a list. Multiple parameters passed implies a hash
# slice/list grep, a single parameter should indicate a regex.
#
# * Perhaps allow a callback to be installed which is called *instead* of
# the get() and set() methods (or rather, is called by them).
#
# * Maybe CMDARG should be in there to specify extra command-line only
# options that get added to the AppConfig::GetOpt alias construction,
# but not applied in config files, general usage, etc. The GLOBAL
# CMDARG might be specified as a format, e.g. "-%c" where %s = name,
# %c = first character, %u - first unique sequence(?). Will
# GetOpt::Long handle --long to -l application automagically?
#
# * ..and an added thought is that CASE sensitivity may be required for the
# command line (-v vs -V, -r vs -R, for example), but not for parsing
# config files where you may wish to treat "Name", "NAME" and "name" alike.
#
#============================================================================
package AppConfig::State;
use 5.006;
use strict;
use warnings;
our $VERSION = '1.71';
our $DEBUG = 0;
our $AUTOLOAD;
# need access to AppConfig::ARGCOUNT_*
use AppConfig ':argcount';
# internal per-variable hashes that AUTOLOAD should provide access to
my %METHVARS;
@METHVARS{ qw( EXPAND ARGS ARGCOUNT ) } = ();
# internal values that AUTOLOAD should provide access to
my %METHFLAGS;
@METHFLAGS{ qw( PEDANTIC ) } = ();
# variable attributes that may be specified in GLOBAL;
my @GLOBAL_OK = qw( DEFAULT EXPAND VALIDATE ACTION ARGS ARGCOUNT );
#------------------------------------------------------------------------
# new(\%config, @vars)
#
# Module constructor. A reference to a hash array containing
# configuration options may be passed as the first parameter. This is
lib/AppConfig/State.pm view on Meta::CPAN
# Dumps the contents of the Config object and all stored variables.
#------------------------------------------------------------------------
sub _dump {
my $self = shift;
my $var;
print STDERR "=" x 71, "\n";
print STDERR
"Status of AppConfig::State (version $VERSION) object:\n\t$self\n";
print STDERR "- " x 36, "\nINTERNAL STATE:\n";
foreach (qw( CREATE CASE PEDANTIC EHANDLER ERROR )) {
printf STDERR " %-12s => %s\n", $_,
defined($self->{ $_ }) ? $self->{ $_ } : "<undef>";
}
print STDERR "- " x 36, "\nVARIABLES:\n";
foreach $var (keys %{ $self->{ VARIABLE } }) {
$self->_dump_var($var);
}
print STDERR "- " x 36, "\n", "ALIASES:\n";
foreach $var (keys %{ $self->{ ALIAS } }) {
printf(" %-12s => %s\n", $var, $self->{ ALIAS }->{ $var });
}
print STDERR "=" x 72, "\n";
}
1;
__END__
=head1 NAME
AppConfig::State - application configuration state
=head1 SYNOPSIS
use AppConfig::State;
my $state = AppConfig::State->new(\%cfg);
$state->define("foo"); # very simple variable definition
$state->define("bar", \%varcfg); # variable specific configuration
$state->define("foo|bar=i@"); # compact format
$state->set("foo", 123); # trivial set/get examples
$state->get("foo");
$state->foo(); # shortcut variable access
$state->foo(456); # shortcut variable update
=head1 OVERVIEW
AppConfig::State is a Perl5 module to handle global configuration variables
for perl programs. It maintains the state of any number of variables,
handling default values, aliasing, validation, update callbacks and
option arguments for use by other AppConfig::* modules.
AppConfig::State is distributed as part of the AppConfig bundle.
=head1 DESCRIPTION
=head2 USING THE AppConfig::State MODULE
To import and use the AppConfig::State module the following line should
appear in your Perl script:
use AppConfig::State;
The AppConfig::State module is loaded automatically by the new()
constructor of the AppConfig module.
AppConfig::State is implemented using object-oriented methods. A
new AppConfig::State object is created and initialised using the
new() method. This returns a reference to a new AppConfig::State
object.
my $state = AppConfig::State->new();
This will create a reference to a new AppConfig::State with all
configuration options set to their default values. You can initialise
the object by passing a reference to a hash array containing
configuration options:
$state = AppConfig::State->new( {
CASE => 1,
ERROR => \&my_error,
} );
The new() constructor of the AppConfig module automatically passes all
parameters to the AppConfig::State new() constructor. Thus, any global
configuration values and variable definitions for AppConfig::State are
also applicable to AppConfig.
The following configuration options may be specified.
=over 4
=item CASE
Determines if the variable names are treated case sensitively. Any non-zero
value makes case significant when naming variables. By default, CASE is set
to 0 and thus "Variable", "VARIABLE" and "VaRiAbLe" are all treated as
"variable".
=item CREATE
By default, CREATE is turned off meaning that all variables accessed via
set() (which includes access via shortcut such as
C<$state-E<gt>variable($value)> which delegates to set()) must previously
have been defined via define(). When CREATE is set to 1, calling
set($variable, $value) on a variable that doesn't exist will cause it
to be created automatically.
When CREATE is set to any other non-zero value, it is assumed to be a
regular expression pattern. If the variable name matches the regex, the
( run in 1.926 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )