CGI-Application-Plugin-Config-Simple-1.01/0000775000076400007640000000000010351551743021362 5ustar mpetersmpeters00000000000000CGI-Application-Plugin-Config-Simple-1.01/t/0000775000076400007640000000000010351551743021625 5ustar mpetersmpeters00000000000000CGI-Application-Plugin-Config-Simple-1.01/t/test_unwriteable.ini0000666000076400007640000000023510351551743025710 0ustar mpetersmpeters00000000000000; Config::Simple 4.57 ; Mon Dec 19 10:34:23 2005 [block1] param12=value12 param11=value11 [block2] param22=value22 param21=value21 [default] param1=xyz CGI-Application-Plugin-Config-Simple-1.01/t/test.ini0000664000076400007640000000020710351551743023304 0ustar mpetersmpeters00000000000000; Config::Simple 4.57 ; Mon Dec 19 10:35:02 2005 [block2] param22=value22 param21=value21 [block1] param12=value12 param11=value11 CGI-Application-Plugin-Config-Simple-1.01/t/main.t0000664000076400007640000000653610351551743022750 0ustar mpetersmpeters00000000000000#!/usr/bin/perl use Test::More; plan(tests => 20); use strict; # 1 # test that we have the modules we need require_ok('CGI::Application::Plugin::Config::Simple'); #create an empty hash to represent a C::A::P::C::S object my $self = bless {}, 'CGI::Application::Plugin::Config::Simple'; # 2..11 # ok, set the environment variable to make sure we can get that { $ENV{CGIAPP_CONFIG_FILE} = 't/test.ini'; my $value = $self->config_file(); is($value, 't/test.ini', 'Config file name from %ENV'); # now let's get a few parameters $value = $self->config_param('block1.param11'); is($value, 'value11', 'Simple ini-file param 1'); $value = $self->config_param('block2.param22'); is($value, 'value22', 'Simple ini-file param 2'); # now let's set some parameters $value = $self->config_param('block1.param11' => 'testing11', 'block2.param22' => 'testing22'); ok($value, 'Set ini-file params 1'); # and test their values $value = $self->config_param('block1.param11'); is($value, 'testing11', 'Simple ini-file param 1'); $value = $self->config_param('block2.param22'); is($value, 'testing22', 'Simple ini-file param 2'); # now let's set them back $value = $self->config_param('block1.param11' => 'value11', 'block2.param22' => 'value22'); ok($value, 'Set ini-file params 2'); # now let's get all of the values in a hash $value = $self->config_param(); is(ref($value), 'HASH', 'Get all ini-file params'); # now let's test this hash's values is($value->{'block1.param11'}, 'value11', 'Testing individual elements of entire config hash 1'); is($value->{'block2.param22'}, 'value22', 'Testing individual elements of entire config hash 2'); } # 12..16 # now let's change config files { my $value = $self->config_file('t/test.conf'); is($value, 't/test.conf', 'Change config file'); # now let's test these values $value = $self->config_param('param1'); is($value, 'value1', 'Simple config param 1'); $value = $self->config_param('param2'); is($value, 'value2', 'Simple config param 2'); $value = $self->config_param('param3'); is($value, 'value3', 'Simple config param 3'); # now let's test the config() method to see if we get a Config::Simple object $value = $self->config(); is(ref($value), 'Config::Simple', 'config() returned object'); } # 17..20 # lets cause some errors SKIP: { # try to change it's permissions my $new_file = 't/test_unwriteable.ini'; chmod(0000, $new_file) || die "Could not chmod $new_file! $!"; # skip these tests if we can still read the file # cause we're probably running as root skip('user wont have permission issues', 4) if( -r $new_file ); # un readable file $self->config_file($new_file); eval { $self->config_param('param1') }; like($@, qr/Permission denied/i, 'un readable file'); # un writeable file chmod(0400, $new_file) || die "Could not chmod $new_file! $!"; my $value = $self->config_file($new_file); is($value, $new_file, 'new unwriteable file'); eval { $self->config_param(param1 => 'xyz') }; like($@, qr/Could not write/i, 'could not write'); # don't specify a config file $ENV{CGIAPP_CONFIG_FILE} = ''; $self->config_file(''); eval { $self->config_param('param1') }; like($@, qr/No config file/i, 'no file given'); } CGI-Application-Plugin-Config-Simple-1.01/t/test.conf0000664000076400007640000000005310351551743023451 0ustar mpetersmpeters00000000000000param1 value1 param2 value2 param3 value3 CGI-Application-Plugin-Config-Simple-1.01/Changes0000664000076400007640000000115210351551743022654 0ustar mpetersmpeters00000000000000Revision history for Perl extension CGI::Application::MailPage. 1.01 (Dec, 19 2005) - fixed failing permission tests if running as root 1.00 (Mar, 08 2005) - 100% test coverage 0.04 (Oct, 19 2004) - Removed CGI::Application::Plugin dependancy in tests - Fixed minor POD discrepancy 0.03 (Sep. 13 2004) - Changed from using CGI::Application::Plugin to Exporter - Changed name of internal store from {_PLUGINS}->{'Config::Simple'} to just {__CONFIG_SIMPLE} 0.02 (Aug. 16 2004) - Changed to support new CGI::Application::Plugin 0.01 - First version. CGI-Application-Plugin-Config-Simple-1.01/Makefile.PL0000664000076400007640000000117210351551743023335 0ustar mpetersmpeters00000000000000# Note: this file was auto-generated by Module::Build::Compat version 0.03 use ExtUtils::MakeMaker; WriteMakefile ( 'PL_FILES' => {}, 'INSTALLDIRS' => 'site', 'NAME' => 'CGI::Application::Plugin::Config::Simple', 'EXE_FILES' => [], 'VERSION_FROM' => 'lib/CGI/Application/Plugin/Config/Simple.pm', 'PM' => undef, 'PREREQ_PM' => { 'Test::More' => 0, 'Config::Simple' => 0, 'CGI::Application' => '3.21', 'Exporter' => 0 } ) ; CGI-Application-Plugin-Config-Simple-1.01/META.yml0000664000076400007640000000075110351551743022636 0ustar mpetersmpeters00000000000000--- name: CGI-Application-Plugin-Config-Simple version: 1.01 author: - Michael Peters abstract: Add Config::Simple support to CGI::Application license: perl resources: license: http://dev.perl.org/licenses/ requires: CGI::Application: 3.21 Config::Simple: 0 Exporter: 0 Test::More: 0 provides: CGI::Application::Plugin::Config::Simple: file: lib/CGI/Application/Plugin/Config/Simple.pm version: 1.01 generated_by: Module::Build version 0.2703 CGI-Application-Plugin-Config-Simple-1.01/README0000664000076400007640000001472510351551743022253 0ustar mpetersmpeters00000000000000NAME CGI::Application::Plugin::Config::Simple - Add Config::Simple support to CGI::Application SYNOPSIS in your CGI::Application based module use CGI::Application::Plugin::Config::Simple; sub cgiapp_init { my $self = shift; #set my config file $self->config_file('myapp.conf'); # #do other stuff # } #later on in a run mode sub run_mode1 { my $self = shift; #just get a single parameter from my config file my $value = $self->config_param('my_param'); #get a parameter in a block (if using ini style files) $value = $self->config_param('my_block.my_param'); #the entire config hash reference my $config_vars = $self->config_param(); #get my Config::Simple object for direct access my $config = $self->config; } DESCRIPTION This module acts as a plugin for Config::Simple to be easily used inside of a CGI::Application module. It does not provide every method available from Config::Simple but rather easy access to your configuration variables. It does however provide direct access to the underlying Config::General object created if you want to use it's full power. The module tries to make the getting and setting of configuration variables as easy as possible. Only three methods are exported into your CGI::Application module and they are described below. Before I wrote this module sometimes I would put my code that read in the configuration file into the cgiapp_init() or cgiapp_prerun() methods but then if I had a run mode that didn't need those config variables it was run anyway. This module helps to solve this is. The Config::Simple object is not created (and the config file is not read and parsed) until after your first call to config() or config_param() to either retrieve/set values, or get the Config::Simple object. This lazy loading idea came from Cees Hek's CGI::Application::Plugin::Session module. METHODS config_param() This method acts as an accessor/mutator for configuration variables coming from the configuration file. This method will behave in three different ways depending on how many parameters it is passed. If 0 parameters are passed then the entire config structure will be returned as a hash ref. If 1 parameters is passed then the value of that parameter in the config file will be returned. If more than 1 parameter is passed then it will treat them as name value pairs and will set the parameters in the config file accordingly. In this case, if we successfully set the parameters then a true value will be returned. #get the complete config hash my $config_hash = $self->config_param(); #just get one config value my $value = $self->config_param($parameter); #set multiple config values my $success = $self->config_param(param1 => $value1, param2 => $value2); This method uses Config::Simple so if you are using ini-files then you can set the values of variables inside blocks as well using the '.' notation. See Config::Simple; You must set the name of the configuration file either using the config_file() method or the CGIAPP_CONFIG_FILE environment variable before calling this method or it will 'die'. config() This method will return the underlying Config::Simple object for more direct use by your application. You must set the name of the configuration file either using the config_file() method or the CGIAPP_CONFIG_FILE environment variable before calling this method or it will 'die'. my $conf = $self->config(); config_file([$file_name]) This method acts as an accessor/mutator to either get the name of the current config file or to change/initialize it. This method must be called to initialize the name of the config file before any call can be made to either config() or config_param() unless the 'CGIAPP_CONFIG_FILE' environment variable has been set. If this environment variable is set it will be used as the initial value of the config file. This is useful if we are running in a mod_perl environment when can use a statement like this in your httpd.conf file: PerlSetEnv CGIAPP_CONFIG_FILE /path/to/my/conf It is typical to set the name of the config file in the cgiapp_init() phase of your application. If a value is passed as a parameter then the config file with that name is used. It will always return the name of the current config file. #get the value of the CGIAPP_CONFIG_FILE environment variable (if there is one) #since we haven't set the config file's name with config_file() yet. my $file_name = $self->config_file(); #set the config file's name $self->config_file('myapp.conf'); #get the name of the config file $file_name = $self->config_file(); CAVEATS The CGI::Application object is implemented as a hash and we store the variables used by this module's methods inside of it as a hash named __CONFIG_SIMPLE. If you use any other CGI::Application plugins there would be problems if they also used $self->{__CONFIG_SIMPLE} but in practice this should never actually happen. AUTHOR Michael Peters Thanks to Plus Three, LP (http://www.plusthree.com) for sponsoring my work on this module SEE ALSO * CGI::Application * Config::Simple LICENSE This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. CGI-Application-Plugin-Config-Simple-1.01/Build.PL0000664000076400007640000000067610351551743022667 0ustar mpetersmpeters00000000000000#!/usr/bin/perl -w use Module::Build; my $build = Module::Build->new( module_name => 'CGI::Application::Plugin::Config::Simple', license => 'perl', requires => { 'CGI::Application' => 3.21, 'Exporter' => 0, 'Config::Simple' => 0, 'Test::More' => 0, }, create_makefile_pl => 'traditional', create_readme => 1, ); $build->create_build_script(); CGI-Application-Plugin-Config-Simple-1.01/MANIFEST0000664000076400007640000000025610351551743022516 0ustar mpetersmpeters00000000000000Build.PL Changes lib/CGI/Application/Plugin/Config/Simple.pm Makefile.PL MANIFEST This list of files META.yml README t/main.t t/test.conf t/test.ini t/test_unwriteable.ini CGI-Application-Plugin-Config-Simple-1.01/lib/0000775000076400007640000000000010351551743022130 5ustar mpetersmpeters00000000000000CGI-Application-Plugin-Config-Simple-1.01/lib/CGI/0000775000076400007640000000000010351551743022532 5ustar mpetersmpeters00000000000000CGI-Application-Plugin-Config-Simple-1.01/lib/CGI/Application/0000775000076400007640000000000010351551743024775 5ustar mpetersmpeters00000000000000CGI-Application-Plugin-Config-Simple-1.01/lib/CGI/Application/Plugin/0000775000076400007640000000000010351551743026233 5ustar mpetersmpeters00000000000000CGI-Application-Plugin-Config-Simple-1.01/lib/CGI/Application/Plugin/Config/0000775000076400007640000000000010351551743027440 5ustar mpetersmpeters00000000000000CGI-Application-Plugin-Config-Simple-1.01/lib/CGI/Application/Plugin/Config/Simple.pm0000664000076400007640000002444210351551743031235 0ustar mpetersmpeters00000000000000package CGI::Application::Plugin::Config::Simple; use strict; use warnings; use base 'Exporter'; use CGI::Application; use Config::Simple; $CGI::Application::Plugin::Config::Simple::VERSION = '1.01'; use vars '@EXPORT'; @EXPORT = qw(config_file config_param config); =pod =head1 NAME CGI::Application::Plugin::Config::Simple - Add Config::Simple support to CGI::Application =head1 SYNOPSIS in your CGI::Application based module use CGI::Application::Plugin::Config::Simple; sub cgiapp_init { my $self = shift; #set my config file $self->config_file('myapp.conf'); # #do other stuff # } #later on in a run mode sub run_mode1 { my $self = shift; #just get a single parameter from my config file my $value = $self->config_param('my_param'); #get a parameter in a block (if using ini style files) $value = $self->config_param('my_block.my_param'); #the entire config hash reference my $config_vars = $self->config_param(); #get my Config::Simple object for direct access my $config = $self->config; } =head1 DESCRIPTION This module acts as a plugin for L to be easily used inside of a L module. It does not provide every method available from L but rather easy access to your configuration variables. It does however provide direct access to the underlying L object created if you want to use it's full power. The module tries to make the getting and setting of configuration variables as easy as possible. Only three methods are exported into your L module and they are described below. Before I wrote this module sometimes I would put my code that read in the configuration file into the cgiapp_init() or cgiapp_prerun() methods but then if I had a run mode that didn't need those config variables it was run anyway. This module helps to solve this is. The L object is not created (and the config file is not read and parsed) until after your first call to L or L to either retrieve/set values, or get the L object. This lazy loading idea came from Cees Hek's L module. =head1 METHODS =head2 config_param() This method acts as an accessor/mutator for configuration variables coming from the configuration file. This method will behave in three different ways depending on how many parameters it is passed. If 0 parameters are passed then the entire config structure will be returned as a hash ref. If 1 parameters is passed then the value of that parameter in the config file will be returned. If more than 1 parameter is passed then it will treat them as name value pairs and will set the parameters in the config file accordingly. In this case, if we successfully set the parameters then a true value will be returned. #get the complete config hash my $config_hash = $self->config_param(); #just get one config value my $value = $self->config_param($parameter); #set multiple config values my $success = $self->config_param(param1 => $value1, param2 => $value2); This method uses Config::Simple so if you are using ini-files then you can set the values of variables inside blocks as well using the '.' notation. See L; You must set the name of the configuration file either using the L method or the CGIAPP_CONFIG_FILE environment variable before calling this method or it will 'die'. =cut sub config_param { my $self = shift; my @params = @_; my $conf = $self->config(); #if there aren't any params then we want the entire config structure as a hash ref if(scalar(@params) == 0) { return scalar($conf->vars) } elsif(scalar(@params) == 1) { #if there is just one then we want just that value return $conf->param($params[0]) } #else we might be setting some values else { my %params = (@params); $conf->param($_ => $params{$_}) foreach (keys %params); if($conf->write) { return 1 } else { die "Config-Plugin: Could not write to config file (" . $self->config_file . ")! " . $conf->error() } } } =pod =head2 config() This method will return the underlying Config::Simple object for more direct use by your application. You must set the name of the configuration file either using the L method or the CGIAPP_CONFIG_FILE environment variable before calling this method or it will 'die'. my $conf = $self->config(); =cut sub config { my $self = shift; #if we don't already have a config object or if the file name has changed on us then create it my $create = !$self->{__CONFIG_SIMPLE}->{__CONFIG_OBJ} || $self->{__CONFIG_SIMPLE}->{__FILE_CHANGED}; if($create) { #get the file name from config_file() my $file_name = $self->config_file or die "No config file specified!"; my $conf = Config::Simple->new($file_name) or die "Could not create Config::Simple object for file $file_name! $!"; $self->{__CONFIG_SIMPLE}->{__CONFIG_OBJ} = $conf; $self->{__CONFIG_SIMPLE}->{__FILE_CHANGED} = 0; } return $self->{__CONFIG_SIMPLE}->{__CONFIG_OBJ}; } =pod =head2 config_file([$file_name]) This method acts as an accessor/mutator to either get the name of the current config file or to change/initialize it. This method must be called to initialize the name of the config file before any call can be made to either L or L unless the 'CGIAPP_CONFIG_FILE' environment variable has been set. If this environment variable is set it will be used as the initial value of the config file. This is useful if we are running in a mod_perl environment when can use a statement like this in your httpd.conf file: PerlSetEnv CGIAPP_CONFIG_FILE /path/to/my/conf It is typical to set the name of the config file in the cgiapp_init() phase of your application. If a value is passed as a parameter then the config file with that name is used. It will always return the name of the current config file. #get the value of the CGIAPP_CONFIG_FILE environment variable (if there is one) #since we haven't set the config file's name with config_file() yet. my $file_name = $self->config_file(); #set the config file's name $self->config_file('myapp.conf'); #get the name of the config file $file_name = $self->config_file(); =cut sub config_file { my ($self, $file_name) = @_; #if we have a file name to set if(defined $file_name) { $self->{__CONFIG_SIMPLE}->{__FILE_NAME} = $file_name; $self->{__CONFIG_SIMPLE}->{__FILE_CHANGED} = 1; } else { #else we are getting the filename $file_name = $self->{__CONFIG_SIMPLE}->{__FILE_NAME} } #if we don't have the file_name then get it from %ENV, but untaint it if(!$file_name) { $ENV{CGIAPP_CONFIG_FILE} =~ /(.*)/; $file_name = $1; } return $file_name; } 1; __END__ =pod =head1 CAVEATS The CGI::Application object is implemented as a hash and we store the variables used by this module's methods inside of it as a hash named __CONFIG_SIMPLE. If you use any other CGI::Application plugins there would be problems if they also used $self->{__CONFIG_SIMPLE} but in practice this should never actually happen. =head1 AUTHOR Michael Peters Thanks to Plus Three, LP (http://www.plusthree.com) for sponsoring my work on this module =head1 SEE ALSO =over 8 =item * L =item * L =back =head1 LICENSE This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =cut