cvsuser 03/06/20 10:17:46
Modified: App-BEGIN Makefile.PL README TODO
App-BEGIN/lib/App BEGIN.pm
App-BEGIN/t app.conf main.t
Log:
sync
Revision Changes Path
1.2 +10 -1 p5ee/App-BEGIN/Makefile.PL
Index: Makefile.PL
===================================================================
RCS file: /cvs/public/p5ee/App-BEGIN/Makefile.PL,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -w -r1.1 -r1.2
--- Makefile.PL 9 May 2003 16:59:41 -0000 1.1
+++ Makefile.PL 20 Jun 2003 17:17:46 -0000 1.2
@@ -1,6 +1,6 @@
######################################################################
-## File: $Id: Makefile.PL,v 1.1 2003/05/09 16:59:41 spadkins Exp $
+## File: $Id: Makefile.PL,v 1.2 2003/06/20 17:17:46 spadkins Exp $
######################################################################
use ExtUtils::MakeMaker;
@@ -20,4 +20,13 @@
######################################################################
WriteMakefile(%opts);
+
+sub MY::postamble {
+ return <<EOF;
+
+install ::
+ @\$(MOD_INSTALL) bin/prefix "\$(PREFIX)/bin/prefix
+
+EOF
+}
1.2 +5 -2 p5ee/App-BEGIN/README
Index: README
===================================================================
RCS file: /cvs/public/p5ee/App-BEGIN/README,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -w -r1.1 -r1.2
--- README 9 May 2003 16:59:41 -0000 1.1
+++ README 20 Jun 2003 17:17:46 -0000 1.2
@@ -1,5 +1,5 @@
######################################################################
-## File: $Id: README,v 1.1 2003/05/09 16:59:41 spadkins Exp $
+## File: $Id: README,v 1.2 2003/06/20 17:17:46 spadkins Exp $
######################################################################
WHAT IS THIS?
@@ -9,9 +9,12 @@
configuration file within the BEGIN block of a
program.
+It is part of the P5EE/App-Context variant of the Perl 5 Enterprise
+Environment.
+
For more information, see the web pages at
- http://www.officevision.com/pub/App-BEGIN
+ http://www.officevision.com/pub/p5ee
and/or join the mailing list at
1.2 +7 -1 p5ee/App-BEGIN/TODO
Index: TODO
===================================================================
RCS file: /cvs/public/p5ee/App-BEGIN/TODO,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -w -r1.1 -r1.2
--- TODO 9 May 2003 16:59:41 -0000 1.1
+++ TODO 20 Jun 2003 17:17:46 -0000 1.2
@@ -1,4 +1,10 @@
######################################################################
-## File: $Id: TODO,v 1.1 2003/05/09 16:59:41 spadkins Exp $
+## File: $Id: TODO,v 1.2 2003/06/20 17:17:46 spadkins Exp $
######################################################################
+ o quoting, var = " hello world "
+ o variable substitutions, var = {:prefix=/usr:}/bin
+ o here documents, var = <<EOF
+ o try use lib "dir"; instead of unshift(@INC,"dir")
+ o reconsider "perlinc", use "perl5lib" instead
+ o consider checking the PERL5LIB variable under -T
1.2 +82 -40 p5ee/App-BEGIN/lib/App/BEGIN.pm
Index: BEGIN.pm
===================================================================
RCS file: /cvs/public/p5ee/App-BEGIN/lib/App/BEGIN.pm,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -w -r1.1 -r1.2
--- BEGIN.pm 9 May 2003 16:59:42 -0000 1.1
+++ BEGIN.pm 20 Jun 2003 17:17:46 -0000 1.2
@@ -1,6 +1,6 @@
#############################################################################
-## $Id: BEGIN.pm,v 1.1 2003/05/09 16:59:42 spadkins Exp $
+## $Id: BEGIN.pm,v 1.2 2003/06/20 17:17:46 spadkins Exp $
#############################################################################
package App::BEGIN;
@@ -67,9 +67,50 @@
App::BEGIN->load_conf();
}
+The load_conf() method reads the command line args (@ARGV),
+finds a configuration file, and loads it, all in a way which
+can be done in a BEGIN block. This is important to be able
+to modify the @INC array so that normal "use" and "require"
+statements will work with the configured @INC path.
+
+The load_conf() method stores command line options and configuration
+file values all in the global %App::conf hash.
+The special keys to this hash are as follows.
+
+ conf - specifies the exact file name of the config file
+ useful for command line usage (i.e. "app --conf=/path/to/app.conf")
+ "conf" is automatically set with the config file that it found
+ if it is not supplied at the outset as an argument.
+
+ conf_tag - specifies the tag that will be used when searching for
+ a config file. (i.e. "app --conf_tag=myapp" will search for "myapp.conf"
+ before it searches for "app.conf")
+ "conf_tag" is automatically set with the stem of the program file that
+ was run (or the first part of PATH_INFO) if it is not supplied at
+ the outset as an argument.
+
+ app_path_info - this is the remainder of PATH_INFO after the first
+ part is taken off to make the conf_tag.
+
+ prefix - This represents the base directory of the software
+ installation (i.e. "/usr/myproduct/1.3.12"). If it is not
+ set explicitly ...
+
+ perlinc
+
+ debugconf
+
+ import
+
+... I am too busy to write documentation right now.
+Take a look at the code. It's easy to figure out.
+
=cut
-sub load_conf () {
+sub load_conf {
+ shift if ($#_ > -1 && $_[0] eq "App::BEGIN");
+ my $conf = ($#_ > -1 && ref($_[0]) eq "HASH") ? $_[0] : \%App::conf;
+
#################################################################
# we do all this within a BEGIN block because we want to get a
# configuration file and update @INC so that it will be used
@@ -88,14 +129,14 @@
# (anything starting with one or two dashes is a config var, not a CGI var)
# i.e. --debugmode=record -debugmode=replay
# an option without an "=" (i.e. --help) acts as --help=1
- # Put the var/value pairs in %App::conf
+ # Put the var/value pairs in %$conf
#################################################################
my ($var, $value);
while ($#ARGV >= 0 && $ARGV[0] =~ /^--?([^=-][^=]*)(=?)(.*)/) {
$var = $1;
$value = ($2 eq "") ? 1 : $3;
shift @ARGV;
- $App::conf{$var} = $value;
+ $conf->{$var} = $value;
}
#################################################################
@@ -104,7 +145,7 @@
# in a web application, this is overridden by any existing
# first part of the PATH_INFO
#################################################################
- my $conf_tag = $App::conf{conf_tag};
+ my $conf_tag = $conf->{conf_tag};
my $app_path_info = $ENV{PATH_INFO} || "";
if (!$conf_tag) {
if ($app_path_info && $app_path_info =~ s!^/([^/]+)!!) {
@@ -115,9 +156,9 @@
$conf_tag =~ s!.*/!!; # strip off leading path
$conf_tag =~ s/\.[^\.]+$//; # strip off trailing file type (i.e.
".pl")
}
- $App::conf{conf_tag} = $conf_tag;
+ $conf->{conf_tag} = $conf_tag;
}
- $App::conf{app_path_info} = $app_path_info;
+ $conf->{app_path_info} = $app_path_info;
#################################################################
# 3. find the directory the program was run from.
@@ -140,7 +181,7 @@
# software installation. The program is usually in
# $prefix/bin or $prefix/cgi-bin.
#################################################################
- my $prefix = $App::conf{prefix}; # possibly set on command line
+ my $prefix = $conf->{prefix}; # possibly set on command line
$prefix = $ENV{PREFIX} if (!$prefix && $ENV{PREFIX});
$prefix = $ENV{DOCUMENT_ROOT} if (!$prefix && $ENV{DOCUMENT_ROOT});
if (!$prefix) {
@@ -151,19 +192,19 @@
$prefix = "." if (!$prefix);
#################################################################
- # 5. Define the standard places to look for a deployconf file
+ # 5. Define the standard places to look for a conf file
#################################################################
- my @deployconf = ();
- if ($App::conf{deployconf}) {
- @deployconf = ( $App::conf{deployconf} );
+ my @conf = ();
+ if ($conf->{conf}) {
+ @conf = ( $conf->{conf} );
}
else {
- push(@deployconf, "$ENV{HOME}/.app/$conf_tag.conf") if ($ENV{HOME});
- push(@deployconf, "$ENV{HOME}/.app/app.conf") if ($ENV{HOME});
- push(@deployconf, "$prefix/etc/app/$conf_tag.conf");
- push(@deployconf, "$prefix/etc/app/app.conf");
- push(@deployconf, "$prog_dir/$conf_tag.conf");
- push(@deployconf, "$prog_dir/app.conf");
+ push(@conf, "$ENV{HOME}/.app/$conf_tag.conf") if ($ENV{HOME} && $conf_tag
ne "app");
+ push(@conf, "$ENV{HOME}/.app/app.conf") if ($ENV{HOME});
+ push(@conf, "$prog_dir/$conf_tag.conf") if ($conf_tag ne "app");
+ push(@conf, "$prog_dir/app.conf");
+ push(@conf, "$prefix/etc/app/$conf_tag.conf") if ($conf_tag ne "app");
+ push(@conf, "$prefix/etc/app/app.conf");
}
#################################################################
@@ -174,14 +215,14 @@
#################################################################
local(*App::FILE);
- my ($deployconf, $firstonly, $regexp);
+ my ($conf_file, $firstonly, $regexp);
$firstonly = 1;
- while ($#deployconf > -1) {
- $deployconf = shift(@deployconf);
- print STDERR "Looking for deployconf [$deployconf]\n" if
($App::conf{debugconf});
- if (open(App::FILE, "< $deployconf")) {
- print STDERR "Found deployconf [$deployconf]\n" if
($App::conf{debugconf});
- @deployconf = () if ($firstonly); # throw out other files to look for
+ while ($#conf > -1) {
+ $conf_file = shift(@conf);
+ print STDERR "Looking for conf [$conf_file]\n" if ($conf->{debugconf});
+ if (open(App::FILE, "< $conf_file")) {
+ print STDERR "Found conf [$conf_file]\n" if ($conf->{debugconf});
+ @conf = () if ($firstonly); # throw out other files to look for
$firstonly = 0; # but keep others which get added (via "import")
while (<App::FILE>) {
chomp;
@@ -201,18 +242,19 @@
$var = $1;
$value = $2;
# TODO: quoting, var = " hello world "
- # TODO: variable substitutions, var = {:prefix=/usr:}/bin
# TODO: here documents, var = <<EOF
# only add values which have never been defined before
- if (!defined $App::conf{$var}) {
- $App::conf{$var} = $value; # save all in %App::conf
+ if (!defined $conf->{$var}) {
+ # do variable substitutions, var = {:prefix=/usr:}/bin
+ $value =~
s/\{:([a-zA-Z0-9_\.-]+)(=?)([^:\{\}]*):\}/(defined $conf->{$1} ? $conf->{$1} : ($2 ?
$3 : $1))/eg;
+ $conf->{$var} = $value; # save all in %App::conf
}
}
}
close(App::FILE);
- if ($App::conf{import}) {
- push(@deployconf, split(/[,:; ]+/, $App::conf{import}));
- delete $App::conf{import};
+ if ($conf->{import}) {
+ push(@conf, split(/[,:; ]+/, $conf->{import}));
+ delete $conf->{import};
}
}
}
@@ -220,11 +262,11 @@
#################################################################
# 7. establish the definitive (not inferred) $prefix
#################################################################
- if ($App::conf{prefix}) {
- $prefix = $App::conf{prefix};
+ if ($conf->{prefix}) {
+ $prefix = $conf->{prefix};
}
else {
- $App::conf{prefix} = $prefix;
+ $conf->{prefix} = $prefix;
}
#################################################################
@@ -234,8 +276,8 @@
# i.e. /usr/mycompany/lib/5.6.1 and /usr/mycompany/lib/site_perl/5.6.1
#################################################################
- if (defined $App::conf{perlinc}) { # add perlinc entries
- unshift(@INC, split(/[,:; ]+/,$App::conf{perlinc}));
+ if (defined $conf->{perlinc}) { # add perlinc entries
+ unshift(@INC, split(/[,:; ]+/,$conf->{perlinc}));
}
else {
my $libdir = "$prefix/lib";
@@ -259,10 +301,10 @@
# 9. print stuff out for configuration debugging
#################################################################
- if ($App::conf{debugconf}) {
- print STDERR "%App::conf =\n";
- foreach $var (sort keys %App::conf) {
- print STDERR " $var = [$App::conf{$var}]\n";
+ if ($conf->{debugconf}) {
+ print STDERR "%App::conf (or other) =\n";
+ foreach $var (sort keys %$conf) {
+ print STDERR " $var = [$conf->{$var}]\n";
}
print STDERR "[EMAIL PROTECTED] =\n";
foreach $var (@INC) {
1.2 +5 -0 p5ee/App-BEGIN/t/app.conf
Index: app.conf
===================================================================
RCS file: /cvs/public/p5ee/App-BEGIN/t/app.conf,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -w -r1.1 -r1.2
--- app.conf 9 May 2003 16:59:42 -0000 1.1
+++ app.conf 20 Jun 2003 17:17:46 -0000 1.2
@@ -1,3 +1,8 @@
var = value
#!/main/ var1 = pattern match
#![main] var2 = old pattern match
+dir = /usr/local
+htdocs_dir = {:dir:}/htdocs
+template_dir = {:dir=/usr/bad:}/template
+cgibin_dir = {:dir2=/usr/local:}/cgi-bin
+greeting = {:Hello:}
1.2 +14 -1 p5ee/App-BEGIN/t/main.t
Index: main.t
===================================================================
RCS file: /cvs/public/p5ee/App-BEGIN/t/main.t,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -w -r1.1 -r1.2
--- main.t 9 May 2003 16:59:42 -0000 1.1
+++ main.t 20 Jun 2003 17:17:46 -0000 1.2
@@ -22,9 +22,13 @@
is($App::conf{var}, "value", "var = value");
is($App::conf{var1}, "pattern match", "pattern match");
is($App::conf{var2}, "old pattern match", "old pattern match");
+is($App::conf{htdocs_dir}, "/usr/local/htdocs", "variable substitution");
+is($App::conf{cgibin_dir}, "/usr/local/cgi-bin", "variable substitution (default
used)");
+is($App::conf{template_dir}, "/usr/local/template", "variable substitution (default
supplied but not used)");
+is($App::conf{greeting}, "Hello", "variable substitution (var name used since var
not defined)");
%App::conf = (
- deployconf => "$dir/app.conf",
+ conf => "$dir/app.conf",
prefix => "/usr/local",
perlinc => "/usr/mycompany/2.1.7"
);
@@ -37,6 +41,15 @@
is($App::conf{var1}, "pattern match", "pattern match");
is($App::conf{var2}, "old pattern match", "old pattern match");
is($INC[0], "/usr/mycompany/2.1.7", "[EMAIL PROTECTED] affected by perlinc");
+
+App::BEGIN->load_conf(\%App::otherconf);
+#print "CONF:\n ", join("\n ",%App::otherconf), "\n";
+ok(%App::otherconf, "put something in %App::otherconf");
+is($App::otherconf{prefix}, $dir, "prefix = $dir");
+is($App::otherconf{conf_tag}, "main", "conf_tag = main");
+is($App::otherconf{var}, "value", "var = value");
+is($App::otherconf{var1}, "pattern match", "pattern match");
+is($App::otherconf{var2}, "old pattern match", "old pattern match");
exit 0;