Dear Monks,
I found out that I can have a common configuration file for Perl and PHP (e.g. conf.ini).
While I was reading the CPAN Config::Simple Documentation I found that the user can create a conf file with Blocks and Keys.
[BLOCK1] KEY1=VALUE1 KEY2=VALUE2 [BLOCK2] KEY1=VALUE1 KEY2=VALUE2
My question, is it possible to combine Blocks with hashes or arrays?
I can not find relative information on-line. The closest example that I found Perl Config::Simple, sample of working code sligthly modified is provided under:
#!/usr/bin/perl use warnings; use strict; use Config::Simple; use Data::Dumper; my %config; Config::Simple->import_from('test.ini', \%config) or die Config::Simple->error(); #print Dumper(\%config); my @data = qw< foo bar baz quux >; foreach ( @data ) { # Verify warn( "Parameter '$_' is missing from INI!\n" ) unless exists $config{$_}; }
The test.ini file:
foo 'test' baz 'test-2'
Terminal output:
Parameter 'bar' is missing from INI! Parameter 'quux' is missing from INI!
I tried several different approaches of the code according to my needs trying to achieve the expected outcome, but none successful:
#!/usr/bin/perl use warnings; use strict; use Config::Simple; use Data::Dumper; my %config; Config::Simple->import_from('conf.ini', \%config) or die Config::Simple->error(); #print Dumper(\%config); my @data = qw< foo bar baz quux >; print "Requirements ".$config{'requirements.test_foo'}."\n"; print "Requirements Array ".$config{'requirements.@test'}."\n"; print "Requirements Hash ".$config{'requirements.%test-2'}."\n"; foreach ( @data ) { # Verify warn( "Parameter '$_' is missing from INI!\n" ) unless exists $config{$_}; } foreach ( @data ) { # Verify warn( "Parameter '$_' is missing from INI!\n" ) unless exists $config{'requirements.@test'}; } foreach ( @data ) { # Verify warn( "Parameter '$_' is missing from INI!\n" ) unless exists $config{'requirements.%test-2'}; }
The conf.ini file:
[requirements] @test=('bar' 'baz') %test-2=( bar => 1 , baz => 2 ) test_foo='foo' quux_test='quux'
The terminal output:
Requirements foo Requirements Array (bar baz) Requirements Hash ARRAY(0x13a06a8) Parameter 'foo' is missing from INI! Parameter 'bar' is missing from INI! Parameter 'baz' is missing from INI! Parameter 'quux' is missing from INI!
I assume that I am doing something wrong and there should be a way of implementing the correct output since the conf.ini file is not complaining for wrong syntax
Has anyone encounter similar problem/solution? Or I am thinking way too complicated and I should simplify the code by using several lines for the same result?
Thanks in advance for your time and effort.
In reply to Perl Config::Simple with %hash or @array by thanos1983
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |