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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.