in reply to Re: Ironclad protection of instance data
in thread Ironclad protection of instance data

Interesting link, thanks. But I must admit I have no idea what's going on.

sub set_phrase { my $self = shift; my $phrase = shift; $phrase {$self} = $phrase; } sub get_phrase { my $self = shift; return $phrase {$self}; }
What does $phrase {$self} do?

Replies are listed 'Best First'.
Re^3: Ironclad protection of instance data
by revdiablo (Prior) on Jul 03, 2004 at 23:53 UTC
    What does $phrase {$self} do?

    It uses the stringified reference at $self as the key for a lexical hash. The hash is unreachable outside of the module, but is shared between all the instances of that object, so you have to key it on something unique. Using the stringified reference is a convenient way to do that.

    Update: just to clarify a bit what I mean by stringified reference:

    $ perl -le 'my $self = {}; print $self' HASH(0x814cc20)

    Something like that would be used as the hash key. Since that contains a memory location, it will be unique (unless something Very Bad happens).

      Ah ha! That's brilliant. I was confused by the space between $phrase and {$self}.

      I think I am going to use a variant of that technique. Thanks for the replies, everyone. (Even those that think it's a bad idea. :) ).