Search code examples
perlxml-simplehashref

How to build a hashref with arrays in perl?


I am having trouble building what i think is a hashref (href) in perl with XML::Simple. I am new to this so not sure how to go about it and i cant find much to build this href with arrays. All the examples i have found are for normal href.

The code bellow outputs the right xml bit, but i am really struggling on how to add more to this href

Thanks Dario

use XML::Simple;
$test = {
book => [
    {
        'name' => ['createdDate'],
        'value' => [20141205]
    },
        {
        'name' => ['deletionDate'],
        'value' => [20111205]
    },

]

};
$test ->{book=> [{'name'=> ['name'],'value'=>['Lord of the rings']}]};
print XMLout($test,RootName=>'library');

Solution

  • To add a new hash to the arrary-ref 'books', you need to cast the array-ref to an array and then push on to it. @{ $test->{book} } casts the array-ref into an array.

    push @{ $test->{book} }, { name => ['name'], value => ['The Hobbit'] };