http://www.perlmonks.org?node_id=1045717

ksublondie has asked for the wisdom of the Perl Monks concerning the following question:

BACKGROUND: I'm using XML::Compile::Schema to read multiple xsd files and generate a hash of the contents with the template function.

my $xsd=XML::Compile::Schema->new(\@array); my $t= $xsd->template('TREE',$xsd->elements()); print LOG Dumper($t);
The resulting LOG shows:

$VAR1 = { 'attrs' => [], ... 'elems' => [ bless( { 'struct' => [...], 'elems' => [ { ... }, { 'attrs' => [], 'elems' => [ bless( { ' +struct' => [...], ' +elems' => [ + { + ... + }, + { + 'attrs' => [], + 'elems' => [ + bless( { + 'struct' => [ + ... + ], + 'elems' => [ + { + ... + }, + { + 'attrs' => [], + 'elems' => [ + bless +( { + + 'struct' => [ + + ..., + + 'elems' => [ + + { + + ... + + }, + + { + + ... + + }, + + { + + ... + + } + + ], + + 'tag' => 'sequence' + + }, 'BLOCK' ) + ], + ... + }, + { + 'attrs' => [], + 'elems' => [ + bless +( { + + ..., + + 'elems' => [ + + { + + 'attrs' => [], + + 'elems' => [ + + bless( { + + 'struct' => [...], + + 'elems' => [ + + {...}, + + {...} + + ], + + 'tag' => 'sequence' + + }, 'BLOCK' ) + + ], + + ... + + }, + + { + + ... + + } + + ], + + 'tag' => 'sequence' + + }, 'BLOCK' ) + ], + ... + }, + { + 'attrs' => [], + 'elems' => [ + bless +( { + + 'struct' => [ + + ... + + ], + + 'elems' => [ + + { + + ... + }, + + { + + ... + + }, + + { + + ... + + } + + ], + + 'tag' => 'sequence' + + }, 'BLOCK' ) + ], + ... + }, + { + 'attrs' => [], + 'elems' => [ + bless +( { + + 'struct' => [ + + ... + + ], + + 'elems' => [ + + { + + ... + + } + + ], + + 'tag' => 'sequence' + + }, 'BLOCK' ) + ], + ... + }, + { + ... + } + ], + 'tag' => 'sequence' + }, 'BLOCK' ) + ], + ... + } + ], ' +tag' => 'sequence' }, +'BLOCK' ) ], ... }, { 'attrs' => [], 'elems' => [ bless( { ' +struct' => [ + ... + ], ' +elems' => [ + { + 'attrs' => [], + 'elems' => [ + bless( { + 'struct' => [ + ... + ], + 'elems' => [ + { + ... + }, + { + ... + }, + { + ... + }, + { + 'attrs' => [], + 'elems' => [ + bless +( { + + 'struct' => [... ], + + 'elems' => [ + + { + + ... + + }, + + { + + ... + + }, + + { + + ... + + }, + + { + + ... + + }, + + { + + ... } + + ], + + 'tag' => 'sequence' + + }, 'BLOCK' ) + ], + + }, + { + 'attrs' => [], + 'elems' => [ + bless +( { + + 'struct' => [...], + + 'elems' => [ + + { ... + }, + + { + + 'attrs' => [], + + 'elems' => [ + + bless( { + + 'struct' => [... + + ], + + 'elems' => [ + + {...}, + + {...}, + + { + + ... + }, + + { + + ... + + }, + + { + + ... + + } + + ], + + 'tag' => 'sequence' + + }, 'BLOCK' ) + + ], + + ... + + } + + ], + + 'tag' => 'sequence' + + }, 'BLOCK' ) + ], + ... + }, + { + ... + }, + { + ... + }, + { + 'attrs' => [], + 'elems' => [ + bless +( { + + 'struct' => [...], + + 'elems' => [ + + { + + 'attrs' => [], + + 'elems' => [ + + bless( { + + 'struct' => [...], + + 'elems' => [ + + { ...}, + + {...}, + + {...} + + ], + + 'tag' => 'sequence' + + }, 'BLOCK' ) + + ], + + ... + } + + ], + + 'tag' => 'sequence' + + }, 'BLOCK' ) + ], + ... + }, + { + ... + }, + { + ... + } + ], + 'tag' => 'sequence' + }, 'BLOCK' ) + ], + ... + }, + { + 'attrs' => [], + 'elems' => [ + bless( { + 'struct' => [...], + 'elems' => [ + {...}, + {...}, + {...}, + {...}, + {...}, + {...}, + {...} + ], + 'tag' => 'sequence' + }, 'BLOCK' ) + ], + ... + }, + { + 'attrs' => [], + 'elems' => [ + bless( { + 'struct' => [...], + 'elems' => [ + {...}, + {...}, + {...}, + {...}, + { + 'attrs' => [], + 'elems' => [ + bless +( { + + 'struct' => [...], + + 'elems' => [ + + { + + 'attrs' => [], + + 'elems' => [ + + bless( { + + 'struct' => [...], + + 'elems' => [ + + {...}, + + {...} + + ], + + 'tag' => 'sequence' + + }, 'BLOCK' ) + + ], + + ... + + } + + ], + + 'tag' => 'sequence' + + }, 'BLOCK' ) + ], + ... + } + ], + 'tag' => 'sequence' + }, 'BLOCK' ) + ], + ... + } + ], ' +tag' => 'sequence' }, +'BLOCK' ) ], ... }, { 'attrs' => [], 'elems' => [ bless( { ' +struct' => [...], ' +elems' => [ + { + 'attrs' => [], + 'elems' => [ + bless( { + 'struct' => [...], + 'elems' => [ + {...}, + {...}, + {...}, + {...} + ], + 'tag' => 'sequence' + }, 'BLOCK' ) + ], + ... + } + ], ' +tag' => 'sequence' }, +'BLOCK' ) ], ... } ], 'tag' => 'sequence' }, 'BLOCK' ) ], ... };

QUESTION: I've never seen bless used in this context and definitely never nested like this. The concept of bless is still a little obscure to me. I guess I learn the best from example code. Since I haven't found any yet used in this way, I'm completely stumped as to why bless was used and what is the best way to access these elements? I'm able to drill down the structure just like it's a typical HOA, but why bless? I'm assuming bless is doing something special here, but I can't figure it out. Can someone shed some light for me?

UPDATED: original post was cut off for being too long. Abbreviated LOG output.

Replies are listed 'Best First'.
Re: Nested Bless within Hash of Arrays
by chromatic (Archbishop) on Jul 22, 2013 at 21:31 UTC
    I'm able to drill down the structure just like it's a typical HOA, but why bless?

    bless associates the name of a package with a reference. It's ultimately how you create new object instances in Perl 5 right now.

    You're seeing a textual serialization of your objects and how to thaw them.


    Improve your skills with Modern Perl: the free book.

      As in BLOCK = $t->{elems}->[0]? But then what is $t->{elems}->[0]->{elems}->[1]->{elems}->[0] since it's also blessed as BLOCK?

        It's another instance of BLOCK, as in my $something = BLOCK->new( { ... } );, but without the intermediary $something.

Re: Nested Bless within Hash of Arrays
by Loops (Curate) on Jul 22, 2013 at 20:46 UTC
    Was there a question in there somewhere?

    Update:

    Ah yeah, figured something like that happened. You might want to take a quick peek at bless and some of the documents it references.
      I'm sorry, but there are no documents referenced nor is there any useful information in that post. Perhaps you meant to post some other link???

      I've been reading as much as I can find regarding bless, but haven't found it used in this way or any examples WITHIN hashes.

      Updated OP.

      The post preview didn't give me any indication that it was too long and cut off. It let me create, view, and edit the post in entirety. Only when I went back into the post from another route did I see it was cut off.

      Thanks for fixing the link.

      I've read all that before, but since it doesn't talk about or give any examples of any blessings INSIDE a hash, I'm missing something.

Re: Nested Bless within Hash of Arrays
by hdb (Monsignor) on Jul 23, 2013 at 07:17 UTC

    In Data::Dumper's output, bless signifies a blessed reference. Have a look at this example:

    use strict; use warnings; use DateTime; use Data::Dumper; my $t = DateTime->now; print Dumper $t;
    which creates this output:
    $VAR1 = bless( { 'local_rd_secs' => 25639, 'local_rd_days' => 735072, 'rd_nanosecs' => 0, 'locale' => bless( { 'default_time_format_length' => +'medium', 'native_territory' => 'United St +ates', 'native_language' => 'English', 'native_complete_name' => 'Engli +sh United States', 'en_language' => 'English', 'id' => 'en_US', 'default_date_format_length' => +'medium', 'en_complete_name' => 'English U +nited States', 'en_territory' => 'United States +' }, 'DateTime::Locale::en_US' ), 'local_c' => { 'hour' => 7, 'second' => 19, 'month' => 7, 'quarter' => 3, 'day_of_year' => 204, 'day_of_quarter' => 23, 'minute' => 7, 'day' => 23, 'day_of_week' => 2, 'year' => 2013 }, 'utc_rd_secs' => 25639, 'formatter' => undef, 'tz' => bless( { 'name' => 'UTC' }, 'DateTime::TimeZone::UTC' ), 'utc_year' => 2014, 'utc_rd_days' => 735072, 'offset_modifier' => 0 }, 'DateTime' );

    This tells you that $t is a blessed hash reference with classname DateTime. The hash itself contains all the members of the class some of which again are blessed hash references (like locale and tz) of different types (classes).

    In the structure you have posted, it seems that the class BLOCK has elements that are blocks themselves, like BLOCKs of BLOCKs. In a way this is not much different from arrays of arrays.

      Ok, so the "blessing" is just a specification by Dumper that the element is a BLOCK object?

        It is to tell you that the reference provided to Dumper has been blessed as a BLOCK.