Brethren,

I have a HoH structure—actually a mix of hashes and arrays. It's created by reading an XML file, using XML::Simple. If I bless the interior hashes, am I going to mess up:

In more detail:
So a typical InspectionData structure read in from the XML looks something like
HASH(0xb82b64) 'InspectionRecord' => ARRAY(0xc207f0) 0 HASH(0xb82be8) 'FileRecord' => ARRAY(0xc20790) 0 HASH(0xb82d08) 'filename' => 'yelloJello.pl' 'Inspector' => ARRAY(0xc3cfc8) 0 HASH(0xb82c84) 'user' => 'smith_j' 'date' => '12-AUG-2007' 'fdnum' => 'D0000'
If I bless the interior hashes that represent InspectionData, FileRecord and Inspector into their own classes, would/should that give indigestion to the XMLout routine? Would it represent a sin against Object Oriented design or otherwise be a venial sin?

XML::Simple stores attributes as hash references and child entities as array references (if you've used the recommended 'ForceArray' option, which I have.) There are a couple places in my code where I walk the key/value pairs of each HoH, testing to see if the value is an arrayref or a hashref:

my $type = ref $hohref; if(not $type){ ....} elsif($type eq 'ARRAY'){ ....} elsif($type eq 'HASH'){ ....} else{ ....}
If I understand rightly, once I've blessed the InspectionRecord etc. $type isn't going to get set to HASH anymore - it's going to come back as InspectionRecord. The code that does this test isn't specific to the Inspection project; it's supposed to be general to anything read in by XML::Simple. So I'm reluctant to added tests there to see if $type is a InspectionRecord, FileRecord, Inspector...

What I want to test for is like Is the underlying implementation of this blessed thing a hashref? This seems contrary to OO principals, but I need to do it to keep the existing code running—and, I suspect, to dump back out to XML.

Am I going about this the right way?

In summary, Randall Schwartz said

The bless operation puts a little post-it note on the hash data structure (not the reference!) that says ``I belong to Widget''.
How do I keep from choking on the post-it note?

throop


In reply to Blessing interior hashes by throop

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.