http://qs321.pair.com?node_id=969044

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

Hi Monks,

I enjoyed reading "Object Oriented Perl" written by D. Conway. Insensitive::Hash interested me specially. Is it possible to create an object blessing tied hash? If possible, would you tell me which CPAN module bless() tied hash?

Sincerely,
Ryo

Replies are listed 'Best First'.
Re: Blessing tied hash
by tobyink (Canon) on May 05, 2012 at 14:23 UTC

    You can't bless hashes (tied or otherwise). You can only bless references. But yes, you can bless a reference to a tied hash.

    JSON::JOM::Object is a blessed reference to a tied hash. And JSON::JOM::Array is a blessed reference to a tied array.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

      Though bless indeed requires a reference as argument it actually tells the thingy referenced by REF that it is now an object, so it blesses the thingy, and not the reference:

      use 5.010; use strict; use warnings; my %hash; say ref \%hash; bless \%hash, "Whatever"; say ref \%hash; __END__ HASH Whatever

      PS of course you can bless reference too:

      use 5.010; use strict; use warnings; my %hash; my $ref = \%hash; bless $ref, 'Array'; bless \$ref, 'Whatever'; say ref \%hash; say ref \$ref; __END__ Array Whatever
        Thanks for your suggestion. Though I'm sure your code works so, not sure how to relate your code with what you mentioned. Does your code prove what you mentioned? 'thingy' sounds unclear for me :(
        Anyway, thanks :)
      I agree we can't bless() a hash. We bless() a reference to create an object. Sorry for misleading you.
Re: Blessing tied hash
by davido (Cardinal) on May 05, 2012 at 15:36 UTC

    As has been mentioned, a hash itself cannot be blessed, but a hashref (including a tied one) can. However, I wanted to add that there is another abstraction already available when a hash gets tied.

    Let's tie %hash:

    tie my %hash, MyClass; # boring

    tie has a return value...

    my $tied_object = tie my %hash, MyClass; # fun! $object->frobcinate(42);

    Now what you're proposing is that the reference to %hash also be blessed:

    my $tied_object = tie my %hash, MyClass; my $instance = bless \%hash, MyOtherClass; $tied_object->frobcinate(42); $instance->bedazzle( with => 'evil' );

    These are two different objects, tied to two very different classes. $tied_object could, for example, explicitly call FETCH(), and may call any other methods built into MyClass. On the other hand, $instance can call any methods from MyOtherClass. Neither one of them has any direct access to the other; they're two separate objects.

    As authors (which I'm not) often say, "The implementation and application to real world problems is left as an exercise for the reader."

    Now according to perltie, the object reference returned by tie doesn't even have to be a reference to an object of type MyClass (the tied class). Thus it's possible that $tied_object be blessed into some other class entirely. Update:...of the same type as the variable being tied. Thus, you might tie a hash, but return a reference to a blessed filehandle, or coderef or something like that. More evil. (and a little different from what I originally suggested)


    Dave

      I agree $tied_object and $instance are two separate objects. Though the following:
      bless \%hash, MyOtherClass;
      makes \%hash blessed into MyOtherClass, I don't think $tied_object blessed into the same class at the same time. In fact,
      ref $tied_object
      returns 'MyClass'.
      I misunderstood. In your last paragraph, you maybe mean we can:
      bless $tied_object, 'Foo';
      but we shouldn't do so.
Re: Blessing tied hash
by JavaFan (Canon) on May 05, 2012 at 14:43 UTC
    Is it possible to create an object blessing tied hash?
    What happened when you tried? It only takes a few lines to set up a tied hash, and a single line to bless it.
      Nothing unusual happened. Most of documents about Perl OOP says "bless() a reference to a hash (or an array) to create an object", and so I was under the impression referents shouldn't be tied. But they don't say "Don't bless() a reference to a tied hash". I wanted to reconfirm we can do so here PerlMonks.org :)