Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

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

I've thought about this some more, and written a few more examples.

This example shows the translation of using aggregate object accessors to the appropriate C:P:MC syntax:

Before:

my $file_dialog = Gtk::FileSelection->new("File Selection Demo"); $file_dialog->ok_button->label("Load"); $file_dialog->ok_button->relief("half"); $file_dialog->ok_button->width(80); $file_dialog->ok_button->height(50); $file_dialog->ok_button->signal_connect(clicked => sub { print $file_dialog->get_filename(), "\n"; }); $file_dialog->cancel_button->label("Exit"); $file_dialog->cancel_button->relief("half"); $file_dialog->cancel_button->width(80); $file_dialog->cancel_button->height(50); $file_dialog->cancel_button->signal_connect(clicked => sub{Gtk->main_q +uit}); $file_dialog->set_filename("penguin.png"); $file_dialog->signal_connect(destroy => sub{Gtk->main_quit}); $file_dialog->show();
After:
my $file_dialog; $file_dialog = Gtk::FileSelection ->Class::Proxy::MethodChain::new("File Selection Demo") ->ok_button->ret_wrap(sub {shift ->label("Load") ->relief("half") ->width(80) ->height(50) ->signal_connect(clicked => sub { print $file_dialog->get_filename()->ret_val, "\n"; }); }) ->cancel_button->ret_wrap(sub {shift ->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 think it's easy to see that the latter syntax scales much better.

I had been thinking that the ->ret_val() syntax is just too bulky and was toying with the idea of having AUTOLOAD() extract pre(or suf)fixes from the called method's name. Eventually I decided against it - I don't think it's better as it'll only cover one case (albeit probably the most or second most common one - getting the return value of the last method call) and complicate AUTOLOAD() needlessly. A name change on C:P:MC's methods would do just as good a job without the added hassle.

I'm thinking of ->_ret() instead of ->ret_val() and ->_proxy() instead of ->ret_wrap(). ->ret_val() is horribly named to begin with, in retrospect - a function can return more than a single value, after all. I am leaning on ->_proxy() because ->_wrap() strikes me as indescriptive.

Now I know underscores are usually used for private methods and that's actually exactly the reason I'd like to use them here. It's unlikely that another module will expose public methods with such names, which minimizes the potential need to reach for the ->call() escape. For the same reasons I'm pondering renaming it to ->_call().

Of course all that means the ->wrap() constructor becomes ->proxy() as well for consistency and leads me to wondering if it's not maybe a good idea to make that one an exported function instead of a constructor. That way no C:P:MC method would start without an underscore, so collisions with wrapped object's public methods' names are far less likely.

Makeshifts last the longest.


In reply to Re: RFC: Class::Proxy::MethodChain by Aristotle
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 imbibing at the Monastery: (5)
As of 2024-03-29 00:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found