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

I didn't think it looked like XML Because it didn't have an ending and it has multiple values for one, node, I guess you would call it.

It's known as an "empty-element" tag, <element /> is equivalent to <element></element>, and the values within the tag itself are attributes. See e.g. https://www.w3schools.com/xml/.

Replies are listed 'Best First'.
Re^4: Get data from an XML file heading
by nachtmsk (Acolyte) on Feb 20, 2020 at 17:10 UTC

    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] };
      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".)