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 :-) :-)
-
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.