Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Here's the scenario: You have a class implemented in the hash-based tradition. Code using it is welcome to access certain public attributes directly (e.g., $object->{foo} = 'new value of foo' is supported). Now you want to change that class to be an inside-out object, but you don't want to break existing code.

One answer is to use overload to make your new inside-out object appear to be a hash-based object. The code below is a proof of concept.

package Hash::Pretender; use overload '%{}' => \&get_hashref; use Class::Std; { my %attr1_of :ATTR( :get<attr1> :set<attr1> ); my %attr2_of :ATTR; sub get_hashref { my $self = shift; tie my %h, ref $self, $self; return \%h; } sub get_attr2 { return ++$attr2_of{ ident shift }; } sub TIEHASH { my ( $package, $self ) = @_; return $self; } sub STORE { my ($self, $key, $value) = @_; my $setter = "set_$key"; $self->$setter( $value ); } sub FETCH { my ($self, $key) = @_; my $getter = "get_$key"; return $self->$getter(); } } package main; use Test::More 'tests' => 11; my $o = Hash::Pretender->new(); isa_ok( $o, 'Hash::Pretender' ); $o->set_attr1( 'foo' ); is( $o->get_attr1(), 'foo', 'attr1 is "foo" via get_attr1' ); is( $o->{attr1}, 'foo', 'attr1 is "foo" via hash dereference' ); SKIP: { skip 'Not implemented', 1 unless ( $o->can('FIRSTKEY') && $o->can('NEXTKEY') ); %h = %{$o}; is( $h{attr1}, 'foo', 'attr1 is "foo" via hash copy' ); }; $o->{attr1} = 'bar'; is( $o->get_attr1(), 'bar', 'attr1 is "bar" via get_attr1' ); is( $o->{attr1}, 'bar', 'attr1 is "bar" via hash dereference' ); SKIP: { skip 'Not implemented', 1 unless ( $o->can('FIRSTKEY') && $o->can('NEXTKEY') ); %h = %{$o}; is( $h{attr1}, 'bar', 'attr1 is "bar" via hash copy' ); }; is( $o->get_attr2(), 1, 'attr2 is 1' ); is( $o->{attr2}, 2, 'attr2 is 2' ); ok( ! eval { $o->{attr2} = 3; 1 }, 'Set attr2 fails' ); my $err = $@; my $msg = q{Can't locate object method "set_attr2" via package "Hash::Pret +ender"}; is( substr( $err, 0, length $msg), $msg, 'Set attr2 fails for the expected reason' );

Now, code that used the old interface will continue to work. If you want to migrate it, you can throw out warnings in the methods that tie depends on to track down the offending code. You can also continue using the old interface and gain the advantage that the new code will throw an exception when you typo an attribute name.

In reply to Make an inside-out object look hash-based using overload. by kyle

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, 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, 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.
  • Log In?

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

    How do I use this? | Other CB clients
    Other Users?
    Others avoiding work at the Monastery: (4)
    As of 2020-07-14 16:47 GMT
    Find Nodes?
      Voting Booth?

      No recent polls found