Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: multiple method calls against the same object, revisited

by dragonchild (Archbishop)
on Dec 27, 2004 at 19:50 UTC ( #417618=note: print w/ replies, xml ) Need Help??


in reply to multiple method calls against the same object, revisited

What's wrong with

my $window; for ($window = Gtk2::Window->new( "toplevel" )) { $_->signal_connect( delete_event => sub { Gtk2->main_quit } ); $_->set_title( "Test" ); $_->set_border_width( 15 ); $_->add( do { my $button = Gtk2::Button->new( "Quit" ); $button->signal_connect( clicked => sub { Gtk2->main_quit } ); $button; } ); $_->show_all(); }

$_ is the "current topic" and for() acts as a topicalizer. Unless, of course, I'm missing something ...

Being right, does not endow the right to be rude; politeness costs nothing.
Being unknowing, is not the same as being stupid.
Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.


Comment on Re: multiple method calls against the same object, revisited
Download Code
Re^2: multiple method calls against the same object, revisited
by Aristotle (Chancellor) on Dec 28, 2004 at 09:32 UTC

    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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://417618]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2014-08-30 04:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (291 votes), past polls