1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
|
#!/usr/bin/perl -w
#========================================================================
#
# examples/complex
#
# AppConfig example file. This demonstrates most of the AppConfig::File
# features.
#
# Written by Andy Wardley <abw@cre.canon.co.uk>
#
# Copyright (C) 1998 Canon Research Centre Europe Ltd.
# All Rights Reserved.
#
# This is free software; you can redistribute it and/or modify it
# under the same terms as Perl itself.
#
#========================================================================
use strict;
# This line will help perl find the modules if you haven't installed them
# yet. It assumes you've run a "perl Makefile.PL; make" in the parent dir.
use lib '../blib/lib';
use AppConfig qw(:expand :argcount);
my $DEFAULT = "<default>";
my $DEFLOG = '/tmp/log';
#------------------------------------------------------------------------
# define various handlers, callbacks, validators, etc.,
#------------------------------------------------------------------------
# update notification handler
sub notify {
my $state = shift;
my $variable = shift;
my $value = shift;
print STDERR "NOTIFY: $variable has been turned ",
$value ? "on" : "off", "\n";
}
# error handler
sub error_handler {
my $format = shift;
printf STDERR "ERROR!: $format\n", @_;
}
# validation handler
sub check_file {
my $variable = shift;
my $value = shift;
print STDERR "check_file($variable, $value)\n";
# we'll assume it's ok to set this value
return 1;
}
#------------------------------------------------------------------------
# create an AppConfig insteance
#------------------------------------------------------------------------
my $config = AppConfig->new({
GLOBAL => {
DEFAULT => $DEFAULT,
ARGCOUNT => 1,
}
},
'verbose' => {
DEFAULT => 0,
ARGCOUNT => 0,
ACTION => \¬ify,
},
'debug' => {
ALIAS => 'help',
ACTION => \¬ify,
},
'logfile' => {
DEFAULT => $DEFLOG,
VALIDATE => \&check_file,
},
'summary' => {
EXPAND => EXPAND_VAR,
});
#------------------------------------------------------------------------
# now use it
#------------------------------------------------------------------------
# turn debug on ("help" is an alias)
$config->help(1);
$config->file('sample.config');
print "verbose: ", $config->verbose(), "\n";
print " debug: ", $config->debug(), "\n";
print "logfile: ", $config->logfile(), "\n";
print "summary: ", $config->summary(), "\n";
|