in reply to Re^3: Get data from an XML file heading
in thread Get data from an XML file heading

Thanks again.

One more thing. Not sure if I understand what this line of code is doing. It looks like it's taking the first element of the array called 'nodes' and converting it into a hash called attrs. Is that correct?

my %attrs = %{ $nodes[0] };

Replies are listed 'Best First'.
Re^5: Get data from an XML file heading
by haukex (Archbishop) on Feb 20, 2020 at 17:47 UTC
    It looks like it's taking the first element of the array called 'nodes' and converting it into a hash called attrs. Is that correct? my %attrs = %{ $nodes[0] };

    Almost right, it's not exactly converting, it's doing a hash dereference, see perlreftut and perlref. Basically:

    my %hash = ( foo => "bar" ); my $hashref = \%hash; # take a reference to the hash print $hashref->{foo},"\n"; # acccess a value via the ref my %attrs = %{ $hashref }; # makes a shallow copy

    (In this case, there's a slightly more complicated thing going on in the background, that you don't necessarily need to care about. $nodes[0] is an object, which provides a special behavior when you use it like a hash reference: it returns the attributes of the element as a hash. That's why I can use this object as if it were a hash reference and dereference it, even though it's a much more complex object than just the attributes in a hash - I can call methods on it, etc. It's documented in XML::LibXML::Element under "Overloading".)