Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
I think you will find some agreement on accessors and mutators that mutators should return the mutated object, and to quote the referenced post "They shall not return the old value, if you wanted to know that you should have asked." (Disclaimer, these are not necessarily my opinions, just other views which may support Aristotle's proposal).

Personally, I think the chained method calls are more elegant, (e.g. ala File::Find::Rule), but I'll decide later when the need arises if its worthwhile enough to wrap objects in your C::P::MC module :-)

Update: Here's my idea of how a very simplified C::P::MC might work:

#!/usr/bin/perl package AccessMutate; # Silly package which returns old values # when attributes are mutated sub new { bless {}; } sub foo { my $self = shift; return $self->{foo} unless @_; my $old = $self->{foo}; $self->{foo} = shift; $old; } sub bar { my $self = shift; return $self->{bar} unless @_; my $old = $self->{bar}; $self->{bar} = shift; $old; } package C::P::MC; sub new { my $class = shift; my $obj = shift; bless \shift, $class; } # Call the object's method. If called # in scalar context, returns the proxy # object. If called in list context, returns # object, and return value. # (Update - drawback: assumes # that the method returns a scalar.., could # probably be fixed with some wantarray logic) sub AUTOLOAD { my $self = shift; my $obj = $$self; (my $method = $AUTOLOAD) =~ s/.*:://; my $ret = $obj->$method(@_); return $obj, $ret, $self; } sub DESTROY { 1 } package main; use strict; use warnings; # Return AM object my ($obj1) = C::P::MC ->new(AccessMutate->new) ->foo(3) ->bar(2); print $obj1->foo,"\n"; print $obj1->bar,"\n"; # Return CPMC object my $obj2 = C::P::MC ->new(AccessMutate->new) ->foo(3) ->bar(2); my (undef, $foo) = $obj2->foo; my (undef, $bar) = $obj2->bar; print "$foo $bar\n";

In reply to Re: RFC: Class::Proxy::MethodChain by runrig
in thread RFC: Class::Proxy::MethodChain by Aristotle

Title:
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!
  • 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
  • Outside of code tags, you may need to use entities for some characters:
            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?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others taking refuge in the Monastery: (8)
    As of 2014-08-30 08:30 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The best computer themed movie is:











      Results (291 votes), past polls