Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Perl Config::Simple with %hash or @array

by thanos1983 (Parson)
on Apr 30, 2014 at 01:27 UTC ( [id://1084418]=perlquestion: print w/replies, xml ) Need Help??

thanos1983 has asked for the wisdom of the Perl Monks concerning the following question:

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.

Replies are listed 'Best First'.
Re: Perl Config::Simple with %hash or @array
by tangent (Parson) on Apr 30, 2014 at 02:27 UTC
    is it possible to combine Blocks with hashes or arrays?
    It is possible to combine blocks with arrays but not with hashes, however it is easy to convert an array to a hash.

    For an array you just need to separate the values with a comma:

    [requirements] array=bar,baz hash=bar,1,baz,2
    You can then process that:
    my $ary = $config{'requirements.array'}; my $hary = $config{'requirements.hash'}; # convert array reference to hash my %hash = @$hary; print "ARRAY:\n"; for my $item (@$ary) { print "$item\n"; } print "HASH:\n"; for my $key (keys %hash) { print "$key => $hash{$key}\n"; }
    Output:
    ARRAY: bar baz HASH: bar => 1 baz => 2

      To: tangent,

      Perfect!!!! It is excactly what I needed. Really nice explanation and easy to understand.

      I was about to update my question, I figure out that it was impossible to use the sign '%' or '@' on the config.ini file because PHP will not be able to understand this symbols.

      Thank you for your time and effort.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1084418]
Approved by LanX
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2024-04-23 19:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found