Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

I basically agree :-) There are certainly other useful styles for setters. For example, the idiom of returning the old value in a setter allows you to compact:

my $old = $foo->fribble $foo->fribble($new); ... do something ... $foo->fribble($old);

to

my $old = $foo->fribble($new) ... do something; $foo->fribble($old);

However, for me the extra clutter that C::P::MC adds takes away the advantages that chaining gives you :-)

That said - how about this as a way to get around the name clashing problems:

package Class::Chain; use strict; use warnings; sub AUTOLOAD { my $self = shift; our $AUTOLOAD; my ($method) = ($AUTOLOAD =~ m/^.*::(.*)$/); return bless \$self->$method(@_) unless ref($self); return $$self->$1(@_) if $method =~ m/^raw_(.*)$/; return bless \$$self->$1(@_) if $method =~ m/^wrap_(.*)$/; $$self->$method(@_); return($self); }; sub chain { my ($self, $sub) = @_; $sub->($self); $self; }; sub DESTROY {};

Which gives you:

# call the whatever method and return $wrapped $wrapped->whatever # call the whatever method and return any value(s) $wrapped->raw_whatever # call the underlying method and wrap return value $wrapped->wrap_whatever # run $wrapped->$coderef and return $wrapped $wrapped->chain($coderef)

So your second example would become something like (untested):

my $file_dialog = Gtk::FileSelection ->Class::Chain::new("File Selection Demo") ->chain(sub {shift->wrap_ok_button ->label("Load") ->relief("half") ->width(80) ->height(50) ->signal_connect(clicked => sub { print $file_dialog->get_filename()->ret_val, "\n"; }); }) ->chain(sub {shift->wrap_cancel_button ->label("Exit") ->relief("half") ->width(80) ->height(50) ->signal_connect(clicked => sub{Gtk->main_quit}); }) ->set_filename("penguin.png") ->signal_connect(destroy => sub{Gtk->main_quit}) ->show();

I still think it's ugly tho :-) :-)


In reply to Re^3: RFC: Class::Proxy::MethodChain by adrianh
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":



  • 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 goofing around in the Monastery: (4)
As of 2024-04-19 16:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found