throop has asked for the wisdom of the Perl Monks concerning the following question:
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:
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?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'
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:
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...my $type = ref $hohref; if(not $type){ ....} elsif($type eq 'ARRAY'){ ....} elsif($type eq 'HASH'){ ....} else{ ....}
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Blessing interior hashes
by ikegami (Patriarch) on Oct 03, 2007 at 16:40 UTC | |
by lodin (Hermit) on Oct 03, 2007 at 19:38 UTC | |
by ikegami (Patriarch) on Oct 03, 2007 at 19:42 UTC | |
by throop (Chaplain) on Oct 03, 2007 at 17:41 UTC | |
by ikegami (Patriarch) on Oct 03, 2007 at 18:28 UTC | |
by lodin (Hermit) on Oct 03, 2007 at 19:07 UTC | |
by ikegami (Patriarch) on Oct 03, 2007 at 19:51 UTC | |
| |
|
Re: Blessing interior hashes
by kyle (Abbot) on Oct 03, 2007 at 16:50 UTC |