AppConfig
view release on metacpan or search on metacpan
lib/AppConfig.pm view on Meta::CPAN
# name => /tmp/file
# path => "/foo:/bar:/baz"
=head2 READING CONFIGURATION FILES
The AppConfig module provides a streamlined interface for reading
configuration files with the AppConfig::File module. The file() method
automatically loads the AppConfig::File module and creates an object
to process the configuration file or files. Variables stored in the
internal AppConfig::State are automatically updated with values specified
in the configuration file.
$config->file($filename);
Multiple files may be passed to file() and should indicate the file name
or be a reference to an open file handle or glob.
$config->file($filename, $filehandle, \*STDIN, ...);
The file may contain blank lines and comments (prefixed by '#') which
lib/AppConfig.pm view on Meta::CPAN
args(), is a small and efficient implementation which offers basic
functionality. The second, getopt(), offers a more powerful and complete
facility by delegating the task to Johan Vroman's Getopt::Long module.
The trade-off between args() and getopt() is essentially one of speed/size
against flexibility. Use as appropriate. Both implement on-demand loading
of modules and incur no overhead until used.
The args() method is used to parse simple command line options. It
automatically loads the AppConfig::Args module and creates an object
to process the command line arguments. Variables stored in the internal
AppConfig::State are automatically updated with values specified in the
arguments.
The method should be passed a reference to a list of arguments to parse.
The @ARGV array is used if args() is called without parameters.
$config->args(\@myargs);
$config->args(); # uses @ARGV
Arguments are read and shifted from the array until the first is
encountered that is not prefixed by '-' or '--'. At that point, the
lib/AppConfig/Getopt.pm view on Meta::CPAN
#============================================================================
#
# AppConfig::Getopt.pm
#
# Perl5 module to interface AppConfig::* to Johan Vromans' Getopt::Long
# module. Getopt::Long implements the POSIX standard for command line
# options, with GNU extensions, and also traditional one-letter options.
# AppConfig::Getopt constructs the necessary Getopt:::Long configuration
# from the internal AppConfig::State and delegates the parsing of command
# line arguments to it. Internal variable values are updated by callback
# from GetOptions().
#
# 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.
#
#============================================================================
package AppConfig::Getopt;
lib/AppConfig/Getopt.pm view on Meta::CPAN
$getopt->parse(qw(auto_abbrev debug)); # uses @ARGV
$getopt->parse(qw(debug), \@myargs);
See Getopt::Long for details of the configuartion options available.
A Getopt::Long specification string is constructed for each variable
defined in the AppConfig::State. This consists of the name, any aliases
and the ARGS value for the variable.
These specification string are then passed to Getopt::Long, the arguments
are parsed and the values in the AppConfig::State updated.
See AppConfig for information about using the AppConfig::Getopt
module via the getopt() method.
=head1 AUTHOR
Andy Wardley, E<lt>abw@wardley.orgE<gt>
=head1 COPYRIGHT
lib/AppConfig/State.pm view on Meta::CPAN
return $negate ? !$value : $value;
}
#------------------------------------------------------------------------
# set($variable, $value)
#
# Assigns the value, $value, to the variable specified.
#
# Returns 1 if the variable is successfully updated or 0 if the variable
# does not exist. If an ACTION sub-routine exists for the variable, it
# will be executed and its return value passed back.
#------------------------------------------------------------------------
sub set {
my $self = shift;
my $variable = shift;
my $value = shift;
my $negate = 0;
my $create;
lib/AppConfig/State.pm view on Meta::CPAN
# are not converted. This function simply converts the parameter
# (variable) to lower case if $self->{ CASE } isn't set. _varname() also
# expands a variable alias to the name of the target variable.
#
# Variables with an ARGCOUNT of ARGCOUNT_ZERO may be specified as
# "no<var>" in which case, the intended value should be negated. The
# leading "no" part is stripped from the variable name. A reference to
# a scalar value can be passed as the second parameter and if the
# _varname() method identified such a variable, it will negate the value.
# This allows the intended value or a simple negate flag to be passed by
# reference and be updated to indicate any negation activity taking place.
#
# The (possibly modified) variable name is returned.
#------------------------------------------------------------------------
sub _varname {
my $self = shift;
my $variable = shift;
my $negated = shift;
# convert to lower case if case insensitive
lib/AppConfig/State.pm view on Meta::CPAN
=item DEBUG
Turns debugging on or off when set to 1 or 0 accordingly. Debugging may
also be activated by calling _debug() as an object method
(C<$state-E<gt>_debug(1)>) or as a package function
(C<AppConfig::State::_debug(1)>), passing in a true/false value to
set the debugging state accordingly. The package variable
$AppConfig::State::DEBUG can also be set directly.
The _debug() method returns the current debug value. If a new value
is passed in, the internal value is updated, but the previous value is
returned.
Note that any AppConfig::File or App::Config::Args objects that are
instantiated with a reference to an App::State will inherit the
DEBUG (and also PEDANTIC) values of the state at that time. Subsequent
changes to the AppConfig::State debug value will not affect them.
=item GLOBAL
The GLOBAL option allows default values to be set for the DEFAULT, ARGCOUNT,
lib/AppConfig/State.pm view on Meta::CPAN
print "OK\n" if $state->_validate($varname, $value);
The _pedantic() method can be called to determine the current value of the
PEDANTIC option.
print "pedantic mode is ", $state->_pedantic() ? "on" ; "off", "\n";
The _debug() method can be used to turn debugging on or off (pass 1 or 0
as a parameter). It can also be used to check the debug state,
returning the current internal value of $AppConfig::State::DEBUG. If a
new debug value is provided, the debug state is updated and the previous
state is returned.
$state->_debug(1); # debug on, returns previous value
The _dump_var($varname) and _dump() methods may also be called for
debugging purposes.
$state->_dump_var($varname); # show variable state
$state->_dump(); # show internal state and all vars
t/appconfig.t view on Meta::CPAN
ok( defined $config );
#3 - #5: check variables were defined
ok( $config->verbose() == 0 );
ok( $config->user() eq $anon );
ok( $config->age() eq $noage );
#6: read config file at DATA handle
ok( $config->file(\*DATA) );
#7 - #9: check values got updated correctly
ok( $config->verbose() == 1 );
ok( $config->user() eq 'abw' );
ok( $config->age() == 42 );
__DATA__
verbose = 1
user = abw
age = 42
#2 - #3: test the state and cfgargs got instantiated correctly
ok( defined $state );
ok( defined $cfgcgi );
my $args = "v&u=$user&age=$age";
#4: process the args
ok( $cfgcgi->parse($args) );
#5 - #7: check variables got updated
ok( $state->verbose() == 1 );
ok( $state->user() eq $user );
ok( $state->age() == $age );
$age = $age * 2;
#8 - #9: check args defaults to using $ENV{ QUERY_STRING }
$ENV{ QUERY_STRING } = "v&u=$user=$user&age=$age";
ok( $cfgcgi->parse() );
ok( $state->age() == $age );
t/compact.t view on Meta::CPAN
'--file', $file1, '-f', $file2,
@defargs,
'-multi', 1, '--multi', 2, '-m', 3,
$notarg);
#3: process the args
# $config->_debug(1);
ok( $config->getopt(qw(default auto_abbrev), \@args) );
# $config->_debug(0);
#4 - #6: check variables got updated
ok( $config->verbose() == 1 );
ok( $config->user() eq $user );
ok( $config->age() eq $age );
#7 - #10: check list variable (file) got set
my $files;
ok( defined ($files = $config->filelist()) );
ok( scalar @$files == 2 );
ok ($files->[0] eq $file1 );
ok ($files->[1] eq $file2 );
#2: test the AppConfig got instantiated correctly
ok( defined $config );
my @args = ('-v', '-u', $user, '--age', $age, $notarg);
#3: process the args
# $config->_debug(1);
ok( $config->getopt(qw(default auto_abbrev), \@args) );
# $config->_debug(0);
#4 - #6: check variables got updated
ok( $config->verbose() == 1 );
ok( $config->user() eq $user );
ok( $config->age() eq $age );
#7: next arg should be $notarg
ok( $args[0] = $notarg );
#8 - #10: check args defaults to using @ARGV
@ARGV = ('--age', $age * 2, $notarg);
ok( $config->getopt() );
#15: test that the verbose ACTION was called and $verbose set
ok( $verbose == 1 );
#16 - #19: test the VALIDATE patterns/subs by attempting to set invalid values
ok( ! $state->age('old') );
ok( $state->age() == $age );
ok( ! $state->user('dud') );
ok( $state->user() eq $user );
#20: check that the error handler correctly updated $errors
ok( $errors == 2 );
#21 - #22: access variables via alias
ok( $state->name() eq $user );
ok( $state->uid() eq $user );
#23 - #25: test case insensitivity
ok( $state->USER() eq $user );
ok( $state->NAME() eq $user );
ok( $state->UID() eq $user );
( run in 0.288 second using v1.01-cache-2.11-cpan-2b0bae70ee8 )