Subject: | Allow passing of Config::IniFiles-object instead of filename |
Hello,
I use FastCGI for persistent perl processes.
I open the config file once in the stub script outside the FastCGI loop
and pass it on to CGI::Application as a parameter in the constructor.
It would be useful if I could pass the Config::Inifiles-Object on to
your plugin so the config file isn't opened & parsed for every request.
I have attached a patch that provides this functionality. Thanks.
Subject: | plugin-0.02.patch |
diff -ru CGI-Application-Plugin-Config-IniFiles-0.01/Changes CGI-Application-Plugin-Config-IniFiles-0.02/Changes
--- CGI-Application-Plugin-Config-IniFiles-0.01/Changes 2007-04-04 08:47:55.000000000 +0200
+++ CGI-Application-Plugin-Config-IniFiles-0.02/Changes 2007-10-22 15:03:41.000000000 +0200
@@ -1,5 +1,9 @@
Revision history for Perl extension CGI::Application::Plugin::Config::IniFiles.
+0.02 Mon Oct 22 15:02:33 CEST 2007
+ - add support for passing a Config::IniFiles object instead of
+ a filename (Sven Neuhaus)
+
0.01 Wed Apr 4 09:47:55 2007
- original version; created by h2xs 1.23 with options
-n CGI::Application::Plugin::Config::IniFiles -AXc
diff -ru CGI-Application-Plugin-Config-IniFiles-0.01/README CGI-Application-Plugin-Config-IniFiles-0.02/README
--- CGI-Application-Plugin-Config-IniFiles-0.01/README 2007-04-04 08:47:55.000000000 +0200
+++ CGI-Application-Plugin-Config-IniFiles-0.02/README 2007-10-22 15:14:35.000000000 +0200
@@ -1,16 +1,11 @@
-CGI-Application-Plugin-Config-IniFiles version 0.01
+CGI-Application-Plugin-Config-IniFiles version 0.02
===================================================
-The README is used to introduce the module and provide instructions on
-how to install the module, any machine dependencies it may have (for
-example C compilers and installed libraries) and any other information
-that should be provided before the module is installed.
-
-A README file is required for CPAN modules since CPAN extracts the
-README file from a module distribution so that people browsing the
-archive can use it get an idea of the modules uses. It is usually a
-good idea to provide version information here so that people can
-decide whether fixes for the module are worth downloading.
+This little plugin makes it a little more comfortable to use
+Config::IniFiles in your scripts using CGI::Application.
+
+Version 0.02 supports passing Config::IniFiles objects instead
+of filenames.
INSTALLATION
@@ -25,12 +20,11 @@
This module requires these other modules and libraries:
- blah blah blah
+ CGI::Application
+ Config::IniFiles
COPYRIGHT AND LICENCE
-Put the correct copyright and licence information here.
-
Copyright (C) 2007 by Artur Penttinen
This library is free software; you can redistribute it and/or modify
diff -ru CGI-Application-Plugin-Config-IniFiles-0.01/lib/CGI/Application/Plugin/Config/IniFiles.pm CGI-Application-Plugin-Config-IniFiles-0.02/lib/CGI/Application/Plugin/Config/IniFiles.pm
--- CGI-Application-Plugin-Config-IniFiles-0.01/lib/CGI/Application/Plugin/Config/IniFiles.pm 2007-04-04 13:52:32.000000000 +0200
+++ CGI-Application-Plugin-Config-IniFiles-0.02/lib/CGI/Application/Plugin/Config/IniFiles.pm 2007-10-22 15:20:01.000000000 +0200
@@ -15,15 +15,20 @@
our @EXPORT = qw( config_file config );
our $VERSION = (qw$Revision: $)[1];
-sub config_file ($$;%) {
- my ($self,$file,%opt) = @_;
- $self->{'__CONFIG_INIFILES'}->{'__FILE_NAME'} = $file;
- $self->{'__CONFIG_INIFILES'}->{'__CONFIG'} =
- new Config::IniFiles ('-file' => $file,%opt);
+sub config_file {
+ my($self,$file,%opt) = @_;
+ if ( ref($file) eq 'Config::IniFiles' ) {
+ # it's not a file after all, it's a Config::IniFiles object
+ # useful for persistent environments like FastCGI
+ $self->{'__CONFIG_INIFILES'}->{'__CONFIG'} = $file;
+ } else {
+ $self->{'__CONFIG_INIFILES'}->{'__FILE_NAME'} = $file;
+ $self->{'__CONFIG_INIFILES'}->{'__CONFIG'} = Config::IniFiles->new('-file' => $file,%opt);
+ }
return $file;
}
-sub config ($) {
+sub config {
return $_[0]->{'__CONFIG_INIFILES'}->{'__CONFIG'};
}
@@ -38,16 +43,16 @@
=head1 SYNOPSIS
use CGI::Application::Plugin::Config::IniFiles;
- sub cgiapp_init ($) {
- my ($self) = @_;
- $self->config_file ("app.conf");
- my $opt = $self->config->val ("main","option");
+ sub cgiapp_init {
+ my($self) = @_;
+ $self->config_file("app.conf");
+ my $opt = $self->config->val("main","option");
...
}
- sub run_mode ($) {
- my ($self) = @_;
- my $opt = $self->config->val ("main","option");
+ sub run_mode {
+ my($self) = @_;
+ my $opt = $self->config->val("main","option");
...
}
@@ -66,6 +71,8 @@
This method reads file I<$file> and create L<Config::IniFiles> object.
Optional arguments has same semantics as in L<Config::IniFiles/new>.
+You can also pass a L<Config::IniFiles> object instead of a filename
+which is useful for persistent environments like FastCGI.
=item C<config()>
diff -ru CGI-Application-Plugin-Config-IniFiles-0.01/t/CGI-Application-Plugin-Config-IniFiles.t CGI-Application-Plugin-Config-IniFiles-0.02/t/CGI-Application-Plugin-Config-IniFiles.t
--- CGI-Application-Plugin-Config-IniFiles-0.01/t/CGI-Application-Plugin-Config-IniFiles.t 2007-04-05 10:29:13.000000000 +0200
+++ CGI-Application-Plugin-Config-IniFiles-0.02/t/CGI-Application-Plugin-Config-IniFiles.t 2007-10-22 15:18:03.000000000 +0200
@@ -1,21 +1,39 @@
#! perl
-use Test::More tests => 5;
+use Test::More tests => 9;
+use Config::IniFiles;
use lib qw(. t);
-BEGIN { use_ok ("TestConfigIniFiles"); };
+BEGIN { use_ok("TestConfigIniFiles"); };
$ENV{'CGI_APP_RETURN_ONLY'} = 1;
-my $app = new TestConfigIniFiles;
-isa_ok ($app,"TestConfigIniFiles");
+my $app = TestConfigIniFiles->new();
+isa_ok($app,"TestConfigIniFiles");
my $out = $app->run;
-#diag ("\n" . $out);
+#diag("\n" . $out);
-ok ($out =~ m#^Content-Type:#,"header");
-ok ($out =~ m#^title="Main title"#m,"main title");
-ok ($out =~ m#^dbs=main,test#m,"db list");
+ok($out =~ m#^Content-Type:#,"header");
+ok($out =~ m#^title="Main title"#m,"main title");
+ok($out =~ m#^dbs=main,test#m,"db list");
+
+my $cfg = Config::IniFiles->new(
+ -file => -f "test.conf" ? "test.conf" : "../test.conf"
+);
+
+$app = TestConfigIniFiles->new(
+ PARAMS => { config_object => $cfg },
+);
+
+isa_ok($app,"TestConfigIniFiles");
+
+$out = $app->run;
+#diag("\n" . $out);
+
+ok($out =~ m#^Content-Type:#,"header");
+ok($out =~ m#^title="Main title"#m,"main title");
+ok($out =~ m#^dbs=main,test#m,"db list");
exit;
diff -ru CGI-Application-Plugin-Config-IniFiles-0.01/t/TestConfigIniFiles.pm CGI-Application-Plugin-Config-IniFiles-0.02/t/TestConfigIniFiles.pm
--- CGI-Application-Plugin-Config-IniFiles-0.01/t/TestConfigIniFiles.pm 2007-04-05 08:12:01.000000000 +0200
+++ CGI-Application-Plugin-Config-IniFiles-0.02/t/TestConfigIniFiles.pm 2007-10-22 15:18:33.000000000 +0200
@@ -5,28 +5,32 @@
use Data::Dumper;
$Data::Dumper::Indent = 1;
-sub cgiapp_init ($) {
- my ($self) = @_;
- my $file = -f "test.conf" ? "test.conf" : "../test.conf";
- $self->config_file ($file);
+sub cgiapp_init {
+ my($self) = @_;
+ if ( $self->param('config_object') ) { # for testing passing an object
+ $self->config_file( $self->param('config_object') );
+ } else { # for testing using filename
+ my $file = -f "test.conf" ? "test.conf" : "../test.conf";
+ $self->config_file($file);
+ }
return;
}
-sub setup ($) {
- my ($self) = @_;
- $self->run_modes ('start' => \&run_mode);
- $self->start_mode ("start");
+sub setup {
+ my($self) = @_;
+ $self->run_modes('start' => \&run_mode);
+ $self->start_mode("start");
return;
}
-sub run_mode ($) {
- my ($self) = @_;
+sub run_mode {
+ my($self) = @_;
my $out;
- my $title = $self->config->val ("main","title");
+ my $title = $self->config->val("main","title");
$out .= sprintf "title=\"%s\"\n",$title;
- my @db = $self->config->GroupMembers ("db");
+ my @db = $self->config->GroupMembers("db");
$out .= sprintf "dbs=%s\n",join ",",map { (split "\\s+",$_,2)[1] } @db;
return $out;