home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / perl / AppConfig.pm < prev    next >
Encoding:
Perl POD Document  |  2004-02-04  |  31.6 KB  |  1,055 lines

  1. #============================================================================
  2. #
  3. # AppConfig.pm
  4. #
  5. # Perl5 module for reading and parsing configuration files and command line 
  6. # arguments.
  7. #
  8. # Written by Andy Wardley <abw@wardley.org>
  9. #
  10. # Copyright (C) 1997-2003 Andy Wardley.  All Rights Reserved.
  11. # Copyright (C) 1997,1998 Canon Research Centre Europe Ltd.
  12. #
  13. # $Id: AppConfig.pm,v 1.7 2004/02/04 10:28:28 abw Exp $
  14. #
  15. #============================================================================
  16.  
  17. package AppConfig;
  18.  
  19. require 5.005;
  20.  
  21. use strict;
  22. use Exporter;
  23. use vars qw( $VERSION $AUTOLOAD @EXPORT_OK %EXPORT_TAGS );
  24. use base qw( Exporter );
  25.  
  26. ## This is the main version number for AppConfig
  27. ## It is extracted by ExtUtils::MakeMaker and inserted in various places.
  28. $VERSION = '1.56';
  29.  
  30. # variable expansion constants
  31. use constant EXPAND_NONE    => 0;
  32. use constant EXPAND_VAR     => 1;
  33. use constant EXPAND_UID     => 2;
  34. use constant EXPAND_ENV     => 4;
  35. use constant EXPAND_ALL     => EXPAND_VAR | EXPAND_UID | EXPAND_ENV;
  36. use constant EXPAND_WARN    => 8;
  37.  
  38. # argument count types
  39. use constant ARGCOUNT_NONE  => 0;
  40. use constant ARGCOUNT_ONE   => 1;
  41. use constant ARGCOUNT_LIST  => 2;
  42. use constant ARGCOUNT_HASH  => 3;
  43.  
  44. # Exporter tagsets
  45. my @EXPAND   = qw(EXPAND_NONE EXPAND_VAR EXPAND_UID EXPAND_ENV 
  46.                   EXPAND_ALL EXPAND_WARN);
  47. my @ARGCOUNT = qw(ARGCOUNT_NONE ARGCOUNT_ONE ARGCOUNT_LIST ARGCOUNT_HASH);
  48.  
  49. @EXPORT_OK   = (@EXPAND, @ARGCOUNT);
  50. %EXPORT_TAGS = (
  51.     expand   => [ @EXPAND   ],
  52.     argcount => [ @ARGCOUNT ],
  53. );
  54.  
  55.  
  56. #------------------------------------------------------------------------
  57. # new(\%config, @vars)
  58. #
  59. # Module constructor.  All parameters passed are forwarded onto the 
  60. # AppConfig::State constructor.  Returns a reference to a newly created 
  61. # AppConfig object.
  62. #------------------------------------------------------------------------
  63.  
  64. sub new {
  65.     my $class = shift;
  66.  
  67.     require AppConfig::State;
  68.  
  69.     my $self = {
  70.         STATE => AppConfig::State->new(@_)
  71.     };
  72.  
  73.     bless $self, $class;
  74.     
  75.     return $self;
  76. }
  77.  
  78.  
  79. #------------------------------------------------------------------------
  80. # file(@files)
  81. #
  82. # The file() method is called to parse configuration files.  An 
  83. # AppConfig::File object is instantiated and stored internally for
  84. # use in subsequent calls to file().
  85. #------------------------------------------------------------------------
  86.  
  87. sub file {
  88.     my $self  = shift;
  89.     my $state = $self->{ STATE };
  90.     my $file;
  91.  
  92.     require AppConfig::File;
  93.  
  94.     # create an AppConfig::File object if one isn't defined 
  95.     $file = $self->{ FILE } ||= AppConfig::File->new($state);
  96.  
  97.     # call on the AppConfig::File object to process files.
  98.     $file->parse(@_);
  99. }
  100.  
  101.  
  102. #------------------------------------------------------------------------
  103. # args(\@args)
  104. #
  105. # The args() method is called to parse command line arguments.  An 
  106. # AppConfig::Args object is instantiated and then stored internally for
  107. # use in subsequent calls to args().
  108. #------------------------------------------------------------------------
  109.  
  110. sub args {
  111.     my $self  = shift;
  112.     my $state = $self->{ STATE };
  113.     my $args;
  114.  
  115.     require AppConfig::Args;
  116.  
  117.     # create an AppConfig::Args object if one isn't defined
  118.     $args = $self->{ ARGS } ||= AppConfig::Args->new($state);
  119.  
  120.     # call on the AppConfig::Args object to process arguments.
  121.     $args->parse(shift);
  122. }
  123.  
  124.  
  125. #------------------------------------------------------------------------
  126. # getopt(@config, \@args)
  127. #
  128. # The getopt() method is called to parse command line arguments.  The
  129. # AppConfig::Getopt module is require()'d and an AppConfig::Getopt object
  130. # is created to parse the arguments.
  131. #------------------------------------------------------------------------
  132.  
  133. sub getopt {
  134.     my $self  = shift;
  135.     my $state = $self->{ STATE };
  136.     my $getopt;
  137.  
  138.     require AppConfig::Getopt;
  139.  
  140.     # create an AppConfig::Getopt object if one isn't defined
  141.     $getopt = $self->{ GETOPT } ||= AppConfig::Getopt->new($state);
  142.  
  143.     # call on the AppConfig::Getopt object to process arguments.
  144.     $getopt->parse(@_);
  145. }
  146.  
  147.  
  148. #------------------------------------------------------------------------
  149. # cgi($query)
  150. #
  151. # The cgi() method is called to parse a CGI query string.  An 
  152. # AppConfig::CGI object is instantiated and then stored internally for
  153. # use in subsequent calls to args().
  154. #------------------------------------------------------------------------
  155.  
  156. sub cgi {
  157.     my $self  = shift;
  158.     my $state = $self->{ STATE };
  159.     my $cgi;
  160.  
  161.     require AppConfig::CGI;
  162.  
  163.     # create an AppConfig::CGI object if one isn't defined
  164.     $cgi = $self->{ CGI } ||= AppConfig::CGI->new($state);
  165.  
  166.     # call on the AppConfig::CGI object to process a query.
  167.     $cgi->parse(shift);
  168. }
  169.  
  170.     
  171. #------------------------------------------------------------------------
  172. # AUTOLOAD
  173. #
  174. # Autoload function called whenever an unresolved object method is 
  175. # called.  All methods are delegated to the $self->{ STATE } 
  176. # AppConfig::State object.
  177. #
  178. #------------------------------------------------------------------------
  179. sub AUTOLOAD {
  180.     my $self = shift;
  181.     my $method;
  182.  
  183.     # splat the leading package name
  184.     ($method = $AUTOLOAD) =~ s/.*:://;
  185.  
  186.     # ignore destructor
  187.     $method eq 'DESTROY' && return;
  188.  
  189.     # delegate method call to AppConfig::State object in $self->{ STATE } 
  190.     $self->{ STATE }->$method(@_);
  191. }
  192.  
  193.  
  194. 1;
  195.  
  196. __END__
  197.  
  198. =head1 NAME
  199.  
  200. AppConfig - Perl5 module for reading configuration files and parsing command line arguments.
  201.  
  202. =head1 SYNOPSIS
  203.  
  204.     use AppConfig;
  205.  
  206.     # create a new AppConfig object
  207.     my $config = AppConfig->new(\%cfg);
  208.  
  209.     # define a new variable
  210.     $config->define($varname => \%varopts);
  211.  
  212.     # create/define combined
  213.     my $config = AppConfig->new(\%cfg, 
  214.     $varname => \%varopts, $varname => \%varopts, ...);
  215.  
  216.     # set/get the value
  217.     $config->set($varname, $value);
  218.     $config->get($varname);
  219.  
  220.     # shortcut form
  221.     $config->varname($value);
  222.     $config->varname();
  223.  
  224.     # read configuration file
  225.     $config->file($file);
  226.  
  227.     # parse command line options
  228.     $config->args(\@args);    # default to \@ARGV
  229.  
  230.     # advanced command line options with Getopt::Long
  231.     $config->getopt(\@args);    # default to \@ARGV
  232.  
  233.     # parse CGI parameters (GET method)
  234.     $config->cgi($query);    # default to $ENV{ QUERY_STRING }
  235.  
  236. =head1 OVERVIEW
  237.  
  238. AppConfig is a Perl5 module for managing application configuration 
  239. information.  It maintains the state of any number of variables and 
  240. provides methods for parsing configuration files, command line 
  241. arguments and CGI script parameters.
  242.  
  243. Variables values may be set via configuration files.  Variables may be 
  244. flags (On/Off), take a single value, or take multiple values stored as a
  245. list or hash.  The number of arguments a variable expects is determined
  246. by its configuration when defined.
  247.  
  248.     # flags
  249.     verbose 
  250.     nohelp
  251.     debug = On
  252.  
  253.     # single value
  254.     home  = /home/abw/
  255.  
  256.     # multiple list value
  257.     file = /tmp/file1
  258.     file = /tmp/file2
  259.  
  260.     # multiple hash value
  261.     book  camel = Programming Perl
  262.     book  llama = Learning Perl
  263.  
  264. The '-' prefix can be used to reset a variable to its default value and
  265. the '+' prefix can be used to set it to 1
  266.  
  267.     -verbose
  268.     +debug
  269.  
  270. Variable, environment variable and tilde (home directory) expansions
  271. can be applied (selectively, if necessary) to the values read from 
  272. configuration files:
  273.  
  274.     home = ~                    # home directory
  275.     nntp = ${NNTPSERVER}        # environment variable
  276.     html = $home/html           # internal variables
  277.     img  = $html/images
  278.  
  279. Configuration files may be arranged in blocks as per the style of Win32 
  280. "INI" files.
  281.  
  282.     [file]
  283.     site = kfs
  284.     src  = ~/websrc/docs/$site
  285.     lib  = ~/websrc/lib
  286.     dest = ~/public_html/$site
  287.  
  288.     [page]
  289.     header = $lib/header
  290.     footer = $lib/footer
  291.  
  292. You can also use Perl's "heredoc" syntax to define a large block of
  293. text in a configuration file.
  294.  
  295.     multiline = <<FOOBAR
  296.     line 1
  297.     line 2
  298.     FOOBAR
  299.  
  300.     paths  exe  = "${PATH}:${HOME}/.bin"
  301.     paths  link = <<'FOO'
  302.     ${LD_LIBARRAY_PATH}:${HOME}/lib
  303.     FOO
  304.  
  305. Variables may also be set by parsing command line arguments.
  306.  
  307.     myapp -verbose -site kfs -file f1 -file f2
  308.  
  309. AppConfig provides a simple method (args()) for parsing command line 
  310. arguments.  A second method (getopt()) allows more complex argument 
  311. processing by delegation to Johan Vroman's Getopt::Long module.
  312.  
  313. AppConfig also allows variables to be set by parameters passed to a 
  314. CGI script via the URL (GET method).
  315.  
  316.     http://www.nowhere.com/cgi-bin/myapp?verbose&site=kfs
  317.  
  318. =head1 PREREQUISITES
  319.  
  320. AppConfig requires Perl 5.005 or later.  
  321.  
  322. The Getopt::Long and Test::More modules should be installed.  If you
  323. are using a recent version of Perl (e.g. 5.8.0) then these should 
  324. already be installed.
  325.  
  326. =head1 OBTAINING AND INSTALLING THE AppConfig MODULE BUNDLE
  327.  
  328. The AppConfig module bundle is available from CPAN.  As the 'perlmod' 
  329. manual page explains:
  330.  
  331.     CPAN stands for the Comprehensive Perl Archive Network.
  332.     This is a globally replicated collection of all known Perl
  333.     materials, including hundreds of unbundled modules.  
  334.  
  335.     [...]
  336.  
  337.     For an up-to-date listing of CPAN sites, see
  338.     http://www.perl.com/perl/ or ftp://ftp.perl.com/perl/ .
  339.  
  340. Within the CPAN archive, AppConfig is in the category:
  341.  
  342.     12) Option, Argument, Parameter and Configuration File Processing
  343.  
  344. The module is available in the following directories:
  345.  
  346.     /modules/by-module/AppConfig/AppConfig-<version>.tar.gz
  347.     /authors/id/ABW/AppConfig-<version>.tar.gz
  348.  
  349. AppConfig is distributed as a single gzipped tar archive file:
  350.  
  351.     AppConfig-<version>.tar.gz
  352.  
  353. Note that "<version>" represents the current AppConfig version
  354. number, of the form "n.nn", e.g. "3.14".  See the REVISION section
  355. below to determine the current version number for AppConfig.
  356.  
  357. Unpack the archive to create a AppConfig installation directory:
  358.  
  359.     gunzip AppConfig-<version>.tar.gz
  360.     tar xvf AppConfig-<version>.tar
  361.  
  362. 'cd' into that directory, make, test and install the modules:
  363.  
  364.     cd AppConfig-<version>
  365.     perl Makefile.PL
  366.     make
  367.     make test
  368.     make install
  369.  
  370. The 't' sub-directory contains a number of test scripts that are run when 
  371. a 'make test' is run.
  372.  
  373. The 'make install' will install the module on your system.  You may need 
  374. administrator privileges to perform this task.  If you install the module 
  375. in a local directory (for example, by executing "perl Makefile.PL
  376. LIB=~/lib" in the above - see C<perldoc MakeMaker> for full details), you
  377. will need to ensure that the PERL5LIB environment variable is set to
  378. include the location, or add a line to your scripts explicitly naming the
  379. library location:
  380.  
  381.     use lib '/local/path/to/lib';
  382.  
  383. The 'examples' sub-directory contains some simple examples of using the 
  384. AppConfig modules.
  385.  
  386. =head1 DESCRIPTION
  387.  
  388. =head2 USING THE AppConfig MODULE
  389.  
  390. To import and use the AppConfig module the following line should 
  391. appear in your Perl script:
  392.  
  393.      use AppConfig;
  394.  
  395. To import constants defined by the AppConfig module, specify the name of
  396. one or more of the constant or tag sets as parameters to C<use>:
  397.  
  398.     use AppConfig qw(:expand :argcount);
  399.  
  400. See L<CONSTANT DEFINITIONS> below for more information on the constant
  401. tagsets defined by AppConfig.
  402.  
  403. AppConfig is implemented using object-oriented methods.  A 
  404. new AppConfig object is created and initialised using the 
  405. new() method.  This returns a reference to a new AppConfig 
  406. object.
  407.        
  408.     my $config = AppConfig->new();
  409.  
  410. This will create and return a reference to a new AppConfig object.
  411.  
  412. In doing so, the AppConfig object also creates an internal reference
  413. to an AppConfig::State object in which to store variable state.  All 
  414. arguments passed into the AppConfig constructor are passed directly
  415. to the AppConfig::State constructor.  
  416.  
  417. The first (optional) parameter may be a reference to a hash array
  418. containing configuration information.  
  419.  
  420.     my $config = AppConfig->new({
  421.         CASE   => 1,
  422.         ERROR  => \&my_error,
  423.         GLOBAL => { 
  424.             DEFAULT  => "<unset>", 
  425.             ARGCOUNT => ARGCOUNT_ONE,
  426.         },
  427.     });
  428.  
  429. See L<AppConfig::State> for full details of the configuration options
  430. available.  These are, in brief:
  431.  
  432. =over 4
  433.  
  434. =item CASE
  435.  
  436. Used to set case sensitivity for variable names (default: off).
  437.  
  438. =item CREATE
  439.  
  440. Used to indicate that undefined variables should be created automatically
  441. (default: off).
  442.  
  443. =item GLOBAL 
  444.  
  445. Reference to a hash array of global values used by default when defining 
  446. variables.  Valid global values are DEFAULT, ARGCOUNT, EXPAND, VALIDATE
  447. and ACTION.
  448.  
  449. =item PEDANTIC
  450.  
  451. Used to indicate that command line and configuration file parsing routines
  452. should return immediately on encountering an error.
  453.  
  454. =item ERROR
  455.  
  456. Used to provide a error handling routine.  Arguments as per printf().
  457.  
  458. =back
  459.  
  460. Subsequent parameters may be variable definitions.  These are passed 
  461. to the define() method, described below in L<DEFINING VARIABLES>.
  462.  
  463.     my $config = AppConfig->new("foo", "bar", "baz");
  464.     my $config = AppConfig->new({ CASE => 1 }, qw(foo bar baz));
  465.  
  466. Note that any unresolved method calls to AppConfig are automatically 
  467. delegated to the AppConfig::State object.  In practice, it means that
  468. it is possible to treat the AppConfig object as if it were an 
  469. AppConfig::State object:
  470.  
  471.     # create AppConfig
  472.     my $config = AppConfig->new('foo', 'bar');
  473.  
  474.     # methods get passed through to internal AppConfig::State
  475.     $config->foo(100);
  476.     $config->set('bar', 200);
  477.     $config->define('baz');
  478.     $config->baz(300);
  479.  
  480. =head2 DEFINING VARIABLES
  481.  
  482. The C<define()> method (delegated to AppConfig::State) is used to 
  483. pre-declare a variable and specify its configuration.
  484.  
  485.     $config->define("foo");
  486.  
  487. Variables may also be defined directly from the AppConfig new()
  488. constructor.
  489.  
  490.     my $config = AppConfig->new("foo");
  491.  
  492. In both simple examples above, a new variable called "foo" is defined.  A 
  493. reference to a hash array may also be passed to specify configuration 
  494. information for the variable:
  495.  
  496.     $config->define("foo", {
  497.         DEFAULT   => 99,
  498.         ALIAS     => 'metavar1',
  499.     });
  500.  
  501. Configuration items specified in the GLOBAL option to the module 
  502. constructor are applied by default when variables are created.  e.g.
  503.  
  504.     my $config = AppConfig->new({ 
  505.         GLOBAL => {
  506.             DEFAULT  => "<undef>",
  507.             ARGCOUNT => ARGCOUNT_ONE,
  508.         }
  509.     });
  510.     
  511.     $config->define("foo");
  512.     $config->define("bar", { ARGCOUNT => ARGCOUNT_NONE } );
  513.  
  514. is equivalent to:
  515.  
  516.     my $config = AppConfig->new();
  517.  
  518.     $config->define("foo", {
  519.         DEFAULT  => "<undef>",
  520.         ARGCOUNT => ARGCOUNT_ONE,
  521.         });
  522.  
  523.     $config->define("bar", 
  524.         DEFAULT  => "<undef>",
  525.         ARGCOUNT => ARGCOUNT_NONE,
  526.         });
  527.  
  528. Multiple variables may be defined in the same call to define().
  529. Configuration hashes for variables can be omitted.
  530.  
  531.     $config->define("foo", "bar" => { ALIAS = "boozer" }, "baz");
  532.  
  533. See L<AppConfig::State> for full details of the configuration options
  534. available when defining variables.  These are, in brief:
  535.  
  536. =over 
  537.  
  538. =item DEFAULT
  539.  
  540. The default value for the variable (default: undef).
  541.  
  542. =item ALIAS
  543.  
  544. One or more (list reference or "list|like|this") alternative names for the
  545. variable.
  546.  
  547. =item ARGCOUNT
  548.  
  549. Specifies the number and type of arguments that the variable expects.
  550. Constants in C<:expand> tag set define ARGCOUNT_NONE - simple on/off flag
  551. (default), ARGCOUNT_ONE - single value, ARGCOUNT_LIST - multiple values
  552. accessed via list reference, ARGCOUNT_HASH - hash table, "key=value",
  553. accessed via hash reference.
  554.  
  555. =item ARGS 
  556.  
  557. Used to provide an argument specification string to pass to Getopt::Long 
  558. via AppConfig::Getopt.  E.g. "=i", ":s", "=s@".  This can also be used to 
  559. implicitly set the ARGCOUNT value (C</^!/> = ARGCOUNT_NONE, C</@/> = 
  560. ARGCOUNT_LIST, C</%/> = ARGCOUNT_HASH, C</[=:].*/> = ARGCOUNT_ONE)
  561.  
  562. =item EXPAND
  563.  
  564. Specifies which variable expansion policies should be used when parsing 
  565. configuration files.  Constants in C<:expand> tag set define EXPAND_NONE
  566. - no expansion (default), EXPAND_VAR - expand C<$var> or C<$(var)> as 
  567. other AppConfig variables, EXPAND_UID - expand C<~uid> as user's home 
  568. directory, EXPAND_ENV - expand C<${var}> as environment variable,
  569. EXPAND_ALL - do all expansions.  May be logically or'd.
  570.  
  571. =item VALIDATE
  572.  
  573. Regex which the intended variable value should match or code reference 
  574. which returns 1 to indicate successful validaton (variable may now be set).
  575.  
  576. =item ACTION
  577.  
  578. Code reference to be called whenever variable value changes.
  579.  
  580. =back
  581.  
  582. =head2 COMPACT FORMAT DEFINITION
  583.  
  584. Variables can be specified using a compact format.  This is identical to 
  585. the specification format of Getopt::Long and is of the form:
  586.  
  587.     "name|alias|alias<argopts>"
  588.  
  589. The first element indicates the variable name and subsequent ALIAS 
  590. values may be added, each separated by a vertical bar '|'.
  591.  
  592. The E<lt>argoptsE<gt> element indicates the ARGCOUNT value and may be one of 
  593. the following;
  594.  
  595.     !                  ARGCOUNT_NONE
  596.     =s                 ARGCOUNT_ONE
  597.     =s@                ARGCOUNT_LIST
  598.     =s%                ARGCOUNT_HASH
  599.  
  600. Additional constructs supported by Getopt::Long may be specified instead
  601. of the "=s" element (e.g. "=f").  The entire E<lt>argoptsE<gt> element 
  602. is stored in the ARGS parameter for the variable and is passed intact to 
  603. Getopt::Long when the getopt() method is called.  
  604.  
  605. The following examples demonstrate use of the comapct format, with their
  606. equivalent full specifications:
  607.  
  608.     $config->define("foo|bar|baz!");
  609.  
  610.     $config->define(
  611.         "foo" => { 
  612.         ALIAS    => "bar|baz", 
  613.         ARGCOUNT => ARGCOUNT_NONE,
  614.         });
  615.  
  616.     $config->define("name=s");
  617.  
  618.     $config->define(
  619.         "name" => { 
  620.         ARGCOUNT => ARGCOUNT_ONE,
  621.         });
  622.  
  623.     $config->define("file|filelist|f=s@");
  624.  
  625.     $config->define(
  626.         "file" => { 
  627.         ALIAS    => "filelist|f", 
  628.         ARGCOUNT => ARGCOUNT_LIST,
  629.         });
  630.  
  631.  
  632.     $config->define("user|u=s%");
  633.  
  634.     $config->define(
  635.         "user" => { 
  636.         ALIAS    => "u", 
  637.         ARGCOUNT => ARGCOUNT_HASH,
  638.         });
  639.  
  640. Additional configuration options may be specified by hash reference, as per 
  641. normal.  The compact definition format will override any configuration 
  642. values provided for ARGS and ARGCOUNT.
  643.  
  644.     $config->define("file|filelist|f=s@", { VALIDATE = \&check_file() } );
  645.  
  646. =head2 READING AND MODIFYING VARIABLE VALUES
  647.  
  648. AppConfig defines two methods (via AppConfig::State) to manipulate variable 
  649. values
  650.  
  651.     set($variable, $value);
  652.     get($variable);
  653.  
  654. Once defined, variables may be accessed directly as object methods where
  655. the method name is the same as the variable name.  i.e.
  656.  
  657.     $config->set("verbose", 1);
  658.  
  659. is equivalent to 
  660.  
  661.     $config->verbose(1); 
  662.  
  663. Note that AppConfig defines the following methods:
  664.  
  665.     new();
  666.     file();
  667.     args();
  668.     getopt();
  669.  
  670. And also, through delegation to AppConfig::State:
  671.  
  672.     define()
  673.     get()
  674.     set()
  675.     varlist()
  676.  
  677. If you define a variable with one of the above names, you will not be able
  678. to access it directly as an object method.  i.e.
  679.  
  680.     $config->file();
  681.  
  682. This will call the file() method, instead of returning the value of the 
  683. 'file' variable.  You can work around this by explicitly calling get() and 
  684. set() on a variable whose name conflicts:
  685.  
  686.     $config->get('file');
  687.  
  688. or by defining a "safe" alias by which the variable can be accessed:
  689.  
  690.     $config->define("file", { ALIAS => "fileopt" });
  691. or
  692.     $config->define("file|fileopt");
  693.  
  694.     ...
  695.     $config->fileopt();
  696.  
  697. Without parameters, the current value of the variable is returned.  If
  698. a parameter is specified, the variable is set to that value and the 
  699. result of the set() operation is returned.
  700.  
  701.     $config->age(29);        # sets 'age' to 29, returns 1 (ok)
  702.     print $config->age();    # prints "29"
  703.  
  704. The varlist() method can be used to extract a number of variables into
  705. a hash array.  The first parameter should be a regular expression 
  706. used for matching against the variable names. 
  707.  
  708.     my %vars = $config->varlist("^file");   # all "file*" variables
  709.  
  710. A second parameter may be specified (any true value) to indicate that 
  711. the part of the variable name matching the regex should be removed 
  712. when copied to the target hash.
  713.  
  714.     $config->file_name("/tmp/file");
  715.     $config->file_path("/foo:/bar:/baz");
  716.  
  717.     my %vars = $config->varlist("^file_", 1);
  718.  
  719.     # %vars:
  720.     #    name => /tmp/file
  721.     #    path => "/foo:/bar:/baz"
  722.  
  723.  
  724. =head2 READING CONFIGURATION FILES
  725.  
  726. The AppConfig module provides a streamlined interface for reading 
  727. configuration files with the AppConfig::File module.  The file() method
  728. automatically loads the AppConfig::File module and creates an object 
  729. to process the configuration file or files.  Variables stored in the 
  730. internal AppConfig::State are automatically updated with values specified 
  731. in the configuration file.  
  732.  
  733.     $config->file($filename);
  734.  
  735. Multiple files may be passed to file() and should indicate the file name 
  736. or be a reference to an open file handle or glob.
  737.  
  738.     $config->file($filename, $filehandle, \*STDIN, ...);
  739.  
  740. The file may contain blank lines and comments (prefixed by '#') which 
  741. are ignored.  Continutation lines may be marked by ending the line with 
  742. a '\'.
  743.  
  744.     # this is a comment
  745.     callsign = alpha bravo camel delta echo foxtrot golf hipowls \
  746.                india juliet kilo llama mike november oscar papa  \
  747.            quebec romeo sierra tango umbrella victor whiskey \
  748.            x-ray yankee zebra
  749.  
  750. Variables that are simple flags and do not expect an argument (ARGCOUNT = 
  751. ARGCOUNT_NONE) can be specified without any value.  They will be set with 
  752. the value 1, with any value explicitly specified (except "0" and "off")
  753. being ignored.  The variable may also be specified with a "no" prefix to 
  754. implicitly set the variable to 0.
  755.  
  756.     verbose                              # on  (1)
  757.     verbose = 1                          # on  (1)
  758.     verbose = 0                          # off (0)
  759.     verbose off                          # off (0)
  760.     verbose on                           # on  (1)
  761.     verbose mumble                       # on  (1)
  762.     noverbose                            # off (0)
  763.  
  764. Variables that expect an argument (ARGCOUNT = ARGCOUNT_ONE) will be set to 
  765. whatever follows the variable name, up to the end of the current line 
  766. (including any continuation lines).  An optional equals sign may be inserted 
  767. between the variable and value for clarity.
  768.  
  769.     room = /home/kitchen     
  770.     room   /home/bedroom
  771.  
  772. Each subsequent re-definition of the variable value overwrites the previous
  773. value.
  774.  
  775.     print $config->room();               # prints "/home/bedroom"
  776.  
  777. Variables may be defined to accept multiple values (ARGCOUNT = ARGCOUNT_LIST).
  778. Each subsequent definition of the variable adds the value to the list of
  779. previously set values for the variable.  
  780.  
  781.     drink = coffee
  782.     drink = tea
  783.  
  784. A reference to a list of values is returned when the variable is requested.
  785.  
  786.     my $beverages = $config->drinks();
  787.     print join(", ", @$beverages);      # prints "coffee, tea"
  788.  
  789. Variables may also be defined as hash lists (ARGCOUNT = ARGCOUNT_HASH).
  790. Each subsequent definition creates a new key and value in the hash array.
  791.  
  792.     alias l="ls -CF"
  793.     alias e="emacs"
  794.  
  795. A reference to the hash is returned when the variable is requested.
  796.  
  797.     my $aliases = $config->alias();
  798.     foreach my $k (keys %$aliases) {
  799.     print "$k => $aliases->{ $k }\n";
  800.     }
  801.  
  802. The '-' prefix can be used to reset a variable to its default value and
  803. the '+' prefix can be used to set it to 1
  804.  
  805.     -verbose
  806.     +debug
  807.  
  808. =head2 VARIABLE EXPANSION
  809.  
  810. Variable values may contain references to other AppConfig variables, 
  811. environment variables and/or users' home directories.  These will be 
  812. expanded depending on the EXPAND value for each variable or the GLOBAL
  813. EXPAND value.
  814.  
  815. Three different expansion types may be applied:
  816.  
  817.     bin = ~/bin          # expand '~' to home dir if EXPAND_UID
  818.     tmp = ~abw/tmp       # as above, but home dir for user 'abw'
  819.  
  820.     perl = $bin/perl     # expand value of 'bin' variable if EXPAND_VAR
  821.     ripl = $(bin)/ripl   # as above with explicit parens
  822.  
  823.     home = ${HOME}       # expand HOME environment var if EXPAND_ENV
  824.  
  825. See L<AppConfig::State> for more information on expanding variable values.
  826.  
  827. The configuration files may have variables arranged in blocks.  A block 
  828. header, consisting of the block name in square brackets, introduces a 
  829. configuration block.  The block name and an underscore are then prefixed 
  830. to the names of all variables subsequently referenced in that block.  The 
  831. block continues until the next block definition or to the end of the current 
  832. file.
  833.  
  834.     [block1]
  835.     foo = 10             # block1_foo = 10
  836.  
  837.     [block2]
  838.     foo = 20             # block2_foo = 20
  839.  
  840. =head2 PARSING COMMAND LINE OPTIONS
  841.  
  842. There are two methods for processing command line options.  The first, 
  843. args(), is a small and efficient implementation which offers basic 
  844. functionality.  The second, getopt(), offers a more powerful and complete
  845. facility by delegating the task to Johan Vroman's Getopt::Long module.  
  846. The trade-off between args() and getopt() is essentially one of speed/size
  847. against flexibility.  Use as appropriate.  Both implement on-demand loading 
  848. of modules and incur no overhead until used.  
  849.  
  850. The args() method is used to parse simple command line options.  It
  851. automatically loads the AppConfig::Args module and creates an object 
  852. to process the command line arguments.  Variables stored in the internal
  853. AppConfig::State are automatically updated with values specified in the 
  854. arguments.  
  855.  
  856. The method should be passed a reference to a list of arguments to parse.
  857. The @ARGV array is used if args() is called without parameters.
  858.  
  859.     $config->args(\@myargs);
  860.     $config->args();               # uses @ARGV
  861.  
  862. Arguments are read and shifted from the array until the first is
  863. encountered that is not prefixed by '-' or '--'.  At that point, the
  864. method returns 1 to indicate success, leaving any unprocessed arguments
  865. remaining in the list.
  866.  
  867. Each argument should be the name or alias of a variable prefixed by 
  868. '-' or '--'.  Arguments that are not prefixed as such (and are not an
  869. additional parameter to a previous argument) will cause a warning to be
  870. raised.  If the PEDANTIC option is set, the method will return 0 
  871. immediately.  With PEDANTIC unset (default), the method will continue
  872. to parse the rest of the arguments, returning 0 when done.
  873.  
  874. If the variable is a simple flag (ARGCOUNT = ARGCOUNT_NONE)
  875. then it is set to the value 1.  The variable may be prefixed by "no" to
  876. set its value to 0.
  877.  
  878.     myprog -verbose --debug -notaste     # $config->verbose(1)
  879.                                          # $config->debug(1)
  880.                                          # $config->taste(0)
  881.  
  882. Variables that expect an additional argument (ARGCOUNT != 0) will be set to 
  883. the value of the argument following it.  
  884.  
  885.     myprog -f /tmp/myfile                # $config->file('/tmp/file');
  886.  
  887. Variables that expect multiple values (ARGCOUNT = ARGCOUNT_LIST or
  888. ARGCOUNT_HASH) will have sucessive values added each time the option
  889. is encountered.
  890.  
  891.     myprog -file /tmp/foo -file /tmp/bar # $config->file('/tmp/foo')
  892.                                          # $config->file('/tmp/bar')
  893.  
  894.     # file => [ '/tmp/foo', '/tmp/bar' ]
  895.     
  896.     myprog -door "jim=Jim Morrison" -door "ray=Ray Manzarek"
  897.                                     # $config->door("jim=Jim Morrison");
  898.                                     # $config->door("ray=Ray Manzarek");
  899.  
  900.     # door => { 'jim' => 'Jim Morrison', 'ray' => 'Ray Manzarek' }
  901.  
  902. See L<AppConfig::Args> for further details on parsing command line
  903. arguments.
  904.  
  905. The getopt() method provides a way to use the power and flexibility of
  906. the Getopt::Long module to parse command line arguments and have the 
  907. internal values of the AppConfig object updates automatically.
  908.  
  909. The first (non-list reference) parameters may contain a number of 
  910. configuration string to pass to Getopt::Long::Configure.  A reference 
  911. to a list of arguments may additionally be passed or @ARGV is used by 
  912. default.
  913.  
  914.     $config->getopt();                       # uses @ARGV
  915.     $config->getopt(\@myargs);
  916.     $config->getopt(qw(auto_abbrev debug));  # uses @ARGV
  917.     $config->getopt(qw(debug), \@myargs);
  918.  
  919. See Getopt::Long for details of the configuration options available.
  920.  
  921. The getopt() method constructs a specification string for each internal
  922. variable and then initialises Getopt::Long with these values.  The
  923. specification string is constructed from the name, any aliases (delimited
  924. by a vertical bar '|') and the value of the ARGS parameter.
  925.  
  926.     $config->define("foo", {
  927.     ARGS  => "=i",
  928.     ALIAS => "bar|baz",
  929.     });
  930.  
  931.     # Getopt::Long specification: "foo|bar|baz=i"
  932.  
  933. Errors and warning generated by the Getopt::Long module are trapped and 
  934. handled by the AppConfig error handler.  This may be a user-defined 
  935. routine installed with the ERROR configuration option.
  936.  
  937. Please note that the AppConfig::Getopt interface is still experimental
  938. and may not be 100% operational.  This is almost undoubtedly due to 
  939. problems in AppConfig::Getopt rather than Getopt::Long.
  940.  
  941. =head2 PARSING CGI PARAMETERS
  942.  
  943. The cgi() method provides an interface to the AppConfig::CGI module
  944. for updating variable values based on the parameters appended to the
  945. URL for a CGI script.  This is commonly known as the CGI 
  946. "GET" method.  The CGI "POST" method is currently not supported.
  947.  
  948. Parameter definitions are separated from the CGI script name by a 
  949. question mark and from each other by ampersands.  Where variables
  950. have specific values, these are appended to the variable with an 
  951. equals sign:
  952.  
  953.     http://www.here.com/cgi-bin/myscript?foo=bar&baz=qux&verbose
  954.  
  955.     # $config->foo('bar');
  956.     # $config->baz('qux');
  957.     # $config->verbose(1);
  958.  
  959. Certain values specified in a URL must be escaped in the appropriate 
  960. manner (see CGI specifications at http://www.w3c.org/ for full details).  
  961. The AppConfig::CGI module automatically unescapes the CGI query string
  962. to restore the parameters to their intended values.
  963.  
  964.     http://where.com/mycgi?title=%22The+Wrong+Trousers%22
  965.  
  966.     # $config->title('"The Wrong Trousers"');
  967.  
  968. Please be considerate of the security implications of providing writeable
  969. access to script variables via CGI.
  970.  
  971.     http://rebel.alliance.com/cgi-bin/...
  972.     .../send_report?file=%2Fetc%2Fpasswd&email=darth%40empire.com
  973.  
  974. To avoid any accidental or malicious changing of "private" variables, 
  975. define only the "public" variables before calling the cgi() (or any 
  976. other) method.  Further variables can subequently be defined which 
  977. can not be influenced by the CGI parameters.
  978.  
  979.     $config->define('verbose', 'debug')
  980.     $config->cgi();        # can only set verbose and debug
  981.  
  982.     $config->define('email', 'file');
  983.     $config->file($cfgfile);    # can set verbose, debug, email + file
  984.  
  985.  
  986. =head1 CONSTANT DEFINITIONS
  987.  
  988. A number of constants are defined by the AppConfig module.  These may be
  989. accessed directly (e.g. AppConfig::EXPAND_VARS) or by first importing them
  990. into the caller's package.  Constants are imported by specifying their 
  991. names as arguments to C<use AppConfig> or by importing a set of constants
  992. identified by its "tag set" name.
  993.  
  994.     use AppConfig qw(ARGCOUNT_NONE ARGCOUNT_ONE);
  995.  
  996.     use AppConfig qw(:argcount);
  997.  
  998. The following tag sets are defined:
  999.  
  1000. =over 4
  1001.  
  1002. =item :expand
  1003.  
  1004. The ':expand' tagset defines the following constants:
  1005.  
  1006.     EXPAND_NONE
  1007.     EXPAND_VAR
  1008.     EXPAND_UID 
  1009.     EXPAND_ENV
  1010.     EXPAND_ALL       # EXPAND_VAR | EXPAND_UID | EXPAND_ENV
  1011.     EXPAND_WARN
  1012.  
  1013. See AppConfig::File for full details of the use of these constants.
  1014.  
  1015. =item :argcount
  1016.  
  1017. The ':argcount' tagset defines the following constants:
  1018.  
  1019.     ARGCOUNT_NONE
  1020.     ARGCOUNT_ONE
  1021.     ARGCOUNT_LIST 
  1022.     ARGCOUNT_HASH
  1023.  
  1024. See AppConfig::State for full details of the use of these constants.
  1025.  
  1026. =back
  1027.  
  1028. =head1 AUTHOR
  1029.  
  1030. Andy Wardley, E<lt>abw@wardley.orgE<gt>
  1031.  
  1032. With contributions from Dave Viner, Ijon Tichy, Axel Gerstmair and
  1033. many others whose names have been lost to the sands of time (reminders
  1034. welcome).
  1035.  
  1036. =head1 REVISION
  1037.  
  1038. Revision $Revision: 1.7 $ distributed as part of AppConfig 1.56.
  1039.  
  1040. =head1 COPYRIGHT
  1041.  
  1042. Copyright (C) 1997-2004 Andy Wardley.  All Rights Reserved.
  1043.  
  1044. Copyright (C) 1997,1998 Canon Research Centre Europe Ltd.
  1045.  
  1046. This module is free software; you can redistribute it and/or modify it 
  1047. under the same terms as Perl itself.
  1048.  
  1049. =head1 SEE ALSO
  1050.  
  1051. AppConfig::State, AppConfig::File, AppConfig::Args, AppConfig::Getopt,
  1052. AppConfig::CGI, Getopt::Long
  1053.  
  1054. =cut
  1055.