Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

comment on

( [id://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

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 lurking in the Monastery: (7)
As of 2024-04-19 15:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found