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

Hi I want to read a config file but should error out if there are more than 1 keys with the same name. Which file parsing method should I use? By default, YAML::LoadFile will take only the first one while Config::Any will take the last one.

Replies are listed 'Best First'.
Re: duplicate keys in YAML
by GrandFather (Saint) on Nov 28, 2014 at 06:06 UTC

    According to the 1.2 version of the YAML spec "The content of a mapping node is an unordered set of key: value node pairs, with the restriction that each of the keys is unique." so you have badly formed YAML and parsers are free to handle that as they see fit. Do you have any control over the generation of the config file?

    If the structures contained in the files are simple you could write a simple parser and handle the multiple entries in whatever fashion makes sense in your application. If you describe the bigger task you are working on we may be able to offer better advice.

    Perl is the programming world's equivalent of English
Re: duplicate keys in YAML
by vinoth.ree (Monsignor) on Nov 28, 2014 at 05:42 UTC

    Can you show us your sample confi file?

    I am also using YAML file config with same keys, I used to read all the keys as array of hashes.

    use strict; use warnings; use YAML::XS qw/LoadFile/; use Data::Dumper; my $config = LoadFile('./config.yaml'); print Dumper($config);
    Test.yaml
    --- - emailName: ree - emailAddresses: - test@test.com - user@domain.com - credential: username: user1 password: password - credential: username: vinothg password: password
    But in your case, you expect the module has to say error on duplicate keys?

    All is well
Re: duplicate keys in YAML
by SuicideJunkie (Vicar) on Nov 28, 2014 at 17:58 UTC

    If you don't have the more fanciful features of YAML in your input, try YAML::Tiny.

    Test.pl
    use strict; use warnings; use YAML::Tiny; my $yaml = YAML::Tiny->read('test.yaml');
    Test.yaml
    --- rootproperty: blah section: one: two three: four Foo: Bar one: again empty: ~
    Results:
    D:\>perl test.pl YAML::Tiny found a duplicate key 'one' in line 'again' at test.pl line + 5.
Re: duplicate keys in YAML
by the_dark_lord (Novice) on Nov 28, 2014 at 08:52 UTC
    Sorry, this is the config file:
    test1: abc: abc: - sdhfg test1: - def test1: ghi: - sdjfhgsdj - sdgfgsd ghi: - rty - weytr test2: - jkl
Re: duplicate keys in YAML
by the_dark_lord (Novice) on Nov 28, 2014 at 08:51 UTC
    This is the sample config file: test1: abc: abc: - sdhfg test1: - def test1: ghi: - sdjfhgsdj - sdgfgsd ghi: - rty - weytr test2: - jkl No i dont have control over the config file. Other users who use my software may have config files with duplicate keys. Such is the case which i want to catch and error out. The structure is very complex to define and there are lot of config files with lots of keys. So will have to form another hash key values and then loop over each task to find its duplicate. This extra processing can be avoided if YAML or any other config file reader gave error for duplicate keys
Re: duplicate keys in YAML
by the_dark_lord (Novice) on Mar 02, 2015 at 08:56 UTC
    hi is there any way I can also find out the line number of the file at which the error occurred? YAML::Tiny does error out but doesn't give the line number out. Any other module which solves this issue?