in reply to Config file to tree format with id, parent, key, value

I'm assuming you do not need that exact format. Do you really need the id and parent to point to line numbers? As long as you can get and set the node values, it should not matter to know the id/parent. There are several formats that can express nested mapping/sequence and support comments, e.g. YAML and TOML. If you are willing to forgo comments, there's JSON. And there's always Perl itself (you specify "configuration" as Perl code).

Somewhat related formats include Apachish (Config::Apachish::Reader) and Config::General. And, you know, XML.

If you want to preserve comments after writing back to configuration file, there are Config::IniFiles or Config::IOD, but these are INI-like. (IOD can store hashes and arrays too, though).

  • Comment on Re: Config file to tree format with id, parent, key, value

Replies are listed 'Best First'.
Re^2: Config file to tree format with id, parent, key, value
by arthur_accioly (Initiate) on Nov 02, 2016 at 02:22 UTC
    Thanks for your answer. I don't want the comments, I actually want to get rid of them. I tested with Config::General and is interesting, but the problem is that when I have the sub-structures, the lib just put everything together and I can't split or find the parents of each key-value... That's my test-code:
    #!/usr/bin/perl use warnings; use strict; use Config::General; my $conf = Config::General->new("fh.cfg"); my %config = $conf->getall; my @myarray = (); while( my( $key, $value ) = each %config ){ print "$key: $value\n"; if (ref($value) eq 'ARRAY'){ @myarray = @$value; foreach (@myarray) { print "$_\n"; } } }
    And here is the output repeated (because "request_snapshot" appears many times in my config):
    (...) status_interval: 30; exchange_is_active_time_end: "16:00"; dropout_detection_initial_interval_secs: 600; request_snapshot: ARRAY(0xce97a8) false; false; false; false; (...)