Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re: Blessing tied hash

by tobyink (Abbot)
on May 05, 2012 at 14:23 UTC ( #969048=note: print w/replies, xml ) Need Help??

in reply to Blessing tied hash

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'

Replies are listed 'Best First'.
Re^2: Blessing tied hash
by zwon (Abbot) on May 05, 2012 at 16:13 UTC

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

        thingy is what the reference points to, it may be hash, array, scalar, subroutine. What I'm saying, and what code demonstrates is that you can bless a hash, not just the reference as tobyink said.

Re^2: Blessing tied hash
by anazawa (Scribe) on May 05, 2012 at 15:05 UTC
    I agree we can't bless() a hash. We bless() a reference to create an object. Sorry for misleading you.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://969048]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2018-06-21 05:23 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (117 votes). Check out past polls.