There's nothing really wrong with it, but I find it's wordier than it needs to be. Look at what the Perl6 idiom looks like; much quieter visually. Trying to retrofit the construct for this use can lead to some awkward choices: f.ex, I understand why you put the assingment in the for list rather than on the my line before it, but isn't that really the wrong way around? There's no reason to pick one over the other in terms of effective length, but of course putting the assignment on the my line makes the for line stick out like a sore thumb…
If I really wanted to use $_ as “it”, I'd probably use a do block:
my $window = do {
local $_ = Gtk2::Window->new( "toplevel" );
$_->signal_connect( delete_event => sub { Gtk2->main_quit } );
$_->set_title( "Test" );
$_->set_border_width( 15 );
$_->add( do {
local $_ = Gtk2::Button->new( "Quit" );
$_->signal_connect( clicked => sub { Gtk2->main_quit } );
$_;
} );
$_->show_all();
$_;
};
That mirrors the structure I achieve with my snippet most closely. But it needs those ugly $_; strewn in there and I'm still mentioning $_ a million times — a blanket of noise covering the code.
Make no mistake, I'm not 100% satisfied with what this snippet allows, either. But until we have Perl6 in our hands, it won't be possible to do much better than it.
Makeshifts last the longest. |