drose2211 has asked for the wisdom of the Perl Monks concerning the following question:

Hello. I am a complete beginner with Perl and code in general. I was reading the SDL manual and going through the section on how to make Pong. I decided to write each section for myself and try to understand what was happening in each section I ran into a problem in the area where a subroutine is used to render the ball and paddles to the screen. I typed it as shown in the manual but am getting a syntax error at (> . Sorry if this is a stupid question

$app-add_show_handler (> sub { $app->draw_rect( [ 0, 0, $app->w, $app->h ], 0x000000FF ); $app->draw_rect( $ball->{rect}, 0xFF0000FF ); $app->draw_rect( $player1->{paddle}, 0xFF0000FF ); $app->draw_rect( $player2->{paddle}, 0xFF0000FF ); $app->update; } );

Replies are listed 'Best First'.
Re: Pong in SDL
by roboticus (Chancellor) on Nov 30, 2017 at 16:55 UTC

    drose2211:

    I've not used SDL, and you don't provide enough context for me to be sure. But looking at just the bit of code you presented, I'd guess one of:

    • Perhaps you meant $app_add_show_handler?, Or maybe
    • A previous line isn't complete and perl detected the syntax error here instead of a few lines earlier?

    I then took a look at the cpan page for SDL and noticed the link to the book. A quick search for add_show_handler finds that exact bit of code (sans comments) on page 52. I thought you were using the '>' to indicate the location of the syntax error, but in fact the book shows a '>' symbol there. I expect that's a syntax error right there.

    Looking around in the text, I'm thinking they made a transposition error and it should be:

    $app->add_show_handler(

    Where the '>' at the end of the line should be moved left. If so, then they've made the same mistake on page 56 (and possibly elsewhere).

    Update: Tweaked the first line to make it read better.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      That's probably because the pod
      C<$app->add_show_handler(>
      renders as
      $app-add_show_handler(>
      and should be written correctly as
      C<< $app->add_show_handler( >>
      ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

      You were correct. The '>' was causing the syntax error. It needed to be written as $app->add_show_handler instead and that fixed the problem. Thanks for the help!