My new question is why must I use the sub keyword when adding the methods as callbacks?
How else is Perl to know that you mean to pass a function reference? Remember that curly braces in Perl 5 can mean any of three things: a hash reference, a block, or an anonymous function passed to a prototyped function.
In effect, the code I posted:
$app->add_move_handler ( sub { $ship->move( @_ ) } ); $app->add_show_handler ( sub { $ship->draw( @_ ) } );
... is a shorter version of this code, using anonymous functions instead:
sub move_ship { $ship->draw( @_ ); } sub draw_ship { $ship->draw( @_ ); } $app->add_move_handler( \&move_ship ); $app->add_show_handler( \&draw_ship );
... or even:
my $move_ship = sub { $ship->move( @_ ) }; my $draw_ship = sub { $ship->draw( @_ ) }; $app->add_move_handler( $move_ship ); $app->add_show_handler( $draw_ship );
Do you follow so far?
In reply to Re^3: SDLx handlers and Moose
by chromatic
in thread SDLx handlers and Moose
by Ransom
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |