# Caveat Lector - this is untested! foreach $tag (@tags) { push @{$tagstack{$tag->name}}, { text => $tag->value, attributes => { split /[\s=]+/, $tag->attributes }, }; } #### foreach my $tag (@tags) { my ($name, $attributes, $value) = $tag =~ m#^<(\w+)\s+(\w+)?\s*>(.*?)$#; # Use $name, $attributes, and $value as per above }