in reply to Re^2: How to improve introspection of an array of hashes
in thread How to improve introspection of an array of hashes

YAML is often my go-to module when I want formatted data but I'm too lazy to do it myself :). I was hoping for compacter data with YAML than Data::Dump though .But the latter has inline { SCALAR => 1 } where YAML puts it in a separate line.

Do you think this might be useful as a cpan module?
Maybe? It needs some tinkering though (or rewrite). Like a wrapper to hide the %output hash. And proper handling of objects: right now inspecting bless {}, 'Pony' would be indicated as 'PonyREF' and bless {}, 'ARRAY' would try to dereference the hashref as an arrayref. Oups.

I'd still be curious to see what others might have to say about the subject. I wouldn't be surprised if there is already a data traversing module that, rather than do what you want already, let's you do it in two to three lines.

Replies are listed 'Best First'.
Re^4: How to improve introspection of an array of hashes
by nysus (Parson) on Sep 13, 2018 at 16:33 UTC

    I'm sure someone has done something like this as well. Just couldn't find it.

    I took your code for a spin in the real world using Google Contacts API. Here's the output from a json response converted to a Perl data structure using Mojo::JSON::decode_json:

    HASH => { encoding => {}, feed => { HASH => { "author" => { ARRAY => { HASH => { email => { HASH => { "\\$t" => {} } }, name => { HASH => { "\\$t" => {} } }, }, }, }, "category" => { ARRAY => { HASH => { scheme => {}, term => {} +} } }, "entry" => { ARRAY => { HASH => { "app\\$edited" => { HASH => { "\\$t" => {}, "xmlns\\$app +" => {} } }, "category" => { ARRAY => { HASH => { scheme => {}, term +=> {} } } }, "content" => { HASH => { "\\$t" => {} } }, "gContact\\$birthday" => { HASH => { when => {} } }, "gContact\\$groupMembershipInfo" => { ARRAY => { HASH => + { deleted => {}, href => {} } } }, "gContact\\$nickname" => { HASH => { "\\$t" => {} } }, "gContact\\$relation" => { ARRAY => { HASH => { "\\$t" = +> {}, "rel" => {} } } }, "gContact\\$userDefinedField" => { ARRAY => { HASH => { +key => {}, value => {} } } }, "gContact\\$website" => { ARRAY => { HASH => { href => {}, label => {}, primary +=> {}, rel => {} } }, }, "gd\\$email" => { ARRAY => { HASH => { address => {}, label => {}, primary => {}, + rel => {} }, }, }, "gd\\$etag" => {}, "gd\\$extendedProperty" => { ARRAY => { HASH => { "\\$t" + => {}, "name" => {} } } }, "gd\\$im" => { ARRAY => { HASH => { address => {}, label => {}, primary => {}, + protocol => {}, rel => {} }, }, }, "gd\\$name" => { HASH => { "gd\\$additionalName" => { HASH => { "\\$t" => {} } +}, "gd\\$familyName" => { HASH => { "\\$t" => {}, " +yomi" => {} } }, "gd\\$fullName" => { HASH => { "\\$t" => {} } +}, "gd\\$givenName" => { HASH => { "\\$t" => {}, " +yomi" => {} } }, "gd\\$namePrefix" => { HASH => { "\\$t" => {} } +}, "gd\\$nameSuffix" => { HASH => { "\\$t" => {} } +}, }, }, "gd\\$organization" => { ARRAY => { HASH => { "gd\\$orgDepartment" => { HASH => { "\\$t" => {} } + }, "gd\\$orgName" => { HASH => { "\\$t" => {} } }, "gd\\$orgTitle" => { HASH => { "\\$t" => {} } }, "primary" => {}, "rel" => {}, }, }, }, "gd\\$phoneNumber" => { ARRAY => { HASH => { "\\$t" => {}, "label" => {}, "primary" => +{}, "rel" => {}, "uri" => {} }, }, }, "gd\\$structuredPostalAddress" => { ARRAY => { HASH => { "gd\\$city" => { HASH => { "\\$t" => { +} } }, "gd\\$country" => { HASH => { "\\$t" => { +}, "code" => {} } }, "gd\\$formattedAddress" => { HASH => { "\\$t" => { +} } }, "gd\\$postcode" => { HASH => { "\\$t" => { +} } }, "gd\\$region" => { HASH => { "\\$t" => { +} } }, "gd\\$street" => { HASH => { "\\$t" => { +} } }, "primary" => {}, "rel" => {}, }, }, }, "id" => { HASH => { "\\$t" => {} } }, "link" => { ARRAY => { HASH => { "gd\\$etag" => {}, "href" => {}, "rel" => +{}, "type" => {} }, }, }, "title" => { HASH => { "\\$t" => {} } }, "updated" => { HASH => { "\\$t" => {} } }, }, }, }, "gd\\$etag" => {}, "generator" => { HASH => { "\\$t" => {}, "uri" => {}, "version +" => {} } }, "id" => { HASH => { "\\$t" => {} } }, "link" => { ARRAY => { HASH => { href => {}, rel => {}, type = +> {} } } }, "openSearch\\$itemsPerPage" => { HASH => { "\\$t" => {} } }, "openSearch\\$startIndex" => { HASH => { "\\$t" => {} } }, "openSearch\\$totalResults" => { HASH => { "\\$t" => {} } }, "title" => { HASH => { "\\$t" => {} } }, "updated" => { HASH => { "\\$t" => {} } }, "xmlns" => {}, "xmlns\\$batch" => {}, "xmlns\\$gContact" => {}, "xmlns\\$gd" => {}, "xmlns\\$openSearch" => {}, }, }, version => {}, },

    I modified the code slightly to get rid of the "SCALAR" output.

    $PM = "Perl Monk's";
    $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest";
    $nysus = $PM . ' ' . $MCF;
    Click here if you love Perl Monks

      I modified the code slightly to get rid of the "SCALAR" output.
      I should have thought of that xD.

      What did you use for the output, I'm guessing it's not Data::Dump? "author" => {} would normally not have the quotes, and I don't understand how you can get something like "\\$t", where the backslash is escaped, so $t gets interpolated which would normally not happen with a either Data::Dumper or Data::Dump.

        Ah, sorry, yeah. That those double backslashes were an artifact of my logging script which uses Dumper. I'll have to look into replacing that with pp. Anyway, here is the raw output from pp:
        HASH => { encoding => {}, feed => { HASH => { "author" => { ARRAY => { HASH => { email => { HASH => { "\$t" => {} } }, name => { HASH => { "\$t" => {} } }, }, }, }, "category" => { ARRAY => { HASH => { scheme => {}, term => {} +} } }, "entry" => { ARRAY => { HASH => { "app\$edited" => { HASH => { "\$t" => {}, "xmlns\$app" = +> {} } }, "category" => { ARRAY => { HASH => { scheme => {}, term +=> {} } } }, "content" => { HASH => { "\$t" => {} } }, "gContact\$groupMembershipInfo" => { ARRAY => { HASH => +{ deleted => {}, href => {} } } }, "gContact\$userDefinedField" => { ARRAY => { HASH => { k +ey => {}, value => {} } } }, "gd\$email" => { ARRAY => { HASH => { address => {}, primary => {}, rel + => {} } }, }, "gd\$etag" => {}, "gd\$extendedProperty" => { ARRAY => { HASH => { "\$t" = +> {}, "name" => {} } } }, "gd\$name" => { HASH => { "gd\$familyName" => { HASH => { "\$t" => {} } }, "gd\$fullName" => { HASH => { "\$t" => {} } }, "gd\$givenName" => { HASH => { "\$t" => {} } }, }, }, "gd\$phoneNumber" => { ARRAY => { HASH => { "\$t" => {}, "primary" => {}, "rel" => {}, + "uri" => {} }, }, }, "id" => { HASH => { "\$t" => {} } }, "link" => { ARRAY => { HASH => { "gd\$etag" => {}, "href" => {}, "rel" => { +}, "type" => {} }, }, }, "title" => { HASH => { "\$t" => {} } }, "updated" => { HASH => { "\$t" => {} } }, }, }, }, "gd\$etag" => {}, "generator" => { HASH => { "\$t" => {}, "uri" => {}, "version" + => {} } }, "id" => { HASH => { "\$t" => {} } }, "link" => { ARRAY => { HASH => { href => {}, rel => {}, type = +> {} } } }, "openSearch\$itemsPerPage" => { HASH => { "\$t" => {} } }, "openSearch\$startIndex" => { HASH => { "\$t" => {} } }, "openSearch\$totalResults" => { HASH => { "\$t" => {} } }, "title" => { HASH => { "\$t" => {} } }, "updated" => { HASH => { "\$t" => {} } }, "xmlns" => {}, "xmlns\$batch" => {}, "xmlns\$gContact" => {}, "xmlns\$gd" => {}, "xmlns\$openSearch" => {}, }, }, version => {}, },

        $PM = "Perl Monk's";
        $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest";
        $nysus = $PM . ' ' . $MCF;
        Click here if you love Perl Monks