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


In reply to Re: Blessing tied hash by davido
in thread Blessing tied hash by anazawa

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.