Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^4: SDLx handlers and Moose

by Ransom (Beadle)
on May 25, 2012 at 14:16 UTC ( #972437=note: print w/ replies, xml ) Need Help??


in reply to Re^3: SDLx handlers and Moose
in thread SDLx handlers and Moose

Because of... everything? Awesome, I'll just read the two tutorials about lexical scoping and clusures and... wait? What? No idea what I'm supposed to be learning here.

In the hopes to have learned something from reading, the methods I created are outside of the closures of my main file?

I'm having trouble relating how these tutorials affect A. My having to use the sub keyword... it IS a sub already and B. Why I must pass @_ to the sub when it was implicit before in this example:

$app->add_event_handler( \&quit_event ); $app->add_move_handler ( \&calculate_laser ); $app->add_show_handler ( \&render_laser ); sub quit_event { my $event = shift; my $controller = shift; $controller->stop if $event->type == SDL_QUIT; } sub calculate_laser { # The step is the difference in Time calculated for the next jump my ( $step, $app, $t ) = @_; $laser += $velocity * $step; $phaser += $velocity * $step; $velocity = -$velocity if $laser > $app->w; $velocity = -$velocity if $laser < -20; } sub render_laser { my ($delta, $app) = @_; #draw bg $app->draw_rect( [0, 0, $app->w, $app->h ], 0); $app->draw_rect([$phaser, $app->h/4, 20, 4], [100, 100, 255, 255]) +; $app->draw_rect( [ $laser, $app->h / 2, 20, 4 ], [255, 8, 8, 255] +); $app->update(); }

I'm fully aware that I may just be missing some major points on closures and lexical scoping, but after reading I'm just more confused as to what's going on rather than anything becoming clearer

Ransom


Comment on Re^4: SDLx handlers and Moose
Download Code
Re^5: SDLx handlers and Moose
by chromatic (Archbishop) on May 25, 2012 at 17:12 UTC
    Why I must pass @_ to the sub when it was implicit before in this example:

    The callbacks are function references. They're not methods; they have no invocants. SDL will invoke those references and pass the parameters it wants to them. SDL doesn't know you're using methods. SDL doesn't know which objects you want to use.

    Because you want to call methods on objects, you have to give SDL callbacks which are closures which then invoke methods on your objects.

    Because you're using that layer of indirection, you have to pass the arguments to your methods directly.

      Ok so with this post and the post above, basically I'm declaring an anonymous sub with the closure (for the callback), and inside the closure I'm saying this is the object and method you need to use/pass args to.

      Hoping I've grasped this little bit of wisdom.

      Thanks for your help,

      Ransom

        Exactly. The callback won't remember the object unless you wrap the object up in a closure.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2014-12-25 09:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (159 votes), past polls