Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (4)
As of 2024-03-19 05:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found