http://www.perlmonks.org?node_id=238294


in reply to Re: Re^3: RFC: Class::Proxy::MethodChain
in thread RFC: Class::Proxy::MethodChain

I know it wasn't exactly a case of "once and only once", but that's the closest I could get to describe it. What I mean is that I found the previous code hard to read because of all the redundancy. There is a lot of repetition, but no so much that you could scan for patterns; reading the original-style code is downright painful.

And no, while my immediate concern is Gtk, the thoughts behind this run far further.

Ah, how I long for the beauty of saying something like

given(Gtk::FileDialog->new()) { .ok_button.label("Load"); .cancel_button.label("Exit"); .connect_signal(destroy => sub { ... }); # ... }

:-) That is really what I wanted to get out of this..

Wait.. I feel an idea coming on.. The problem with the for approach is that you need to weave calls; which is the case because you can't access the topic past the block. If for where an expression that could return something, it would work. One way to get that done would be to abuse map:

my $window = map { $_->signal_connect(delete => sub { Gtk->exit(0) }); $_->set_title("Test"); $_->border_width(15); $_->add(map { $_->signal_connect(clicked => sub { Gtk->exit(0) }); $_->show; $_ } Gtk::Button->new("Quit"); $_->show; } Gtk::Window->new("toplevel");
Now I'm not really sure if I should think of this as really clever or really evil.. *grin*

Makeshifts last the longest.