#!/usr/bin/perl -w # note Perl 5.005 or later may be required to run this program use AppConfig qw/:argcount/; use strict; # be good, li'l program. use Data::Dumper; # for hash and array references $| = 1; # flush all output immediately my $config = AppConfig->new(); # define all the variables we will use, with defaults were necessary $config->define( 'DEBUG' => { ARGCOUNT => ARGCOUNT_NONE, DEFAULT => 0 }, 'NAME' => { ARGCOUNT => ARGCOUNT_ONE, DEFAULT => 'Ishmael' }, 'HOSTS' => { ARGCOUNT => ARGCOUNT_LIST }, 'PHONE' => { ARGCOUNT => ARGCOUNT_HASH }, ); # read multiple configuration files on startup foreach my $file ('example.conf', 'example2.conf') { # process file if it's available $config->file($file) if -f $file; } # note that args() can be called BEFORE files are processed as well, in case files to # be loaded are passed as command-line options (e.g. "-f") $config->args(); # process command-line options my %varlist = $config->varlist('.*'); foreach my $varname (keys %varlist) { print "Variable name $varname, value = ", Dumper($config->get($varname)), "\n"; } # variable names are autoloaded methods in the AppConfig object print 'Accessing variables directly as a method is easy too: name = ', $config->NAME, "\n";