Perl 6 in a hands-on test

Game Mode

Keyboard input from the command line usually requires the user to confirm each entry with the return key, but in game mode, you want the blackjack script to respond directly to key presses. The raw terminal mode is used for this on Unix systems; you will only want to enable this during the actual entry and reset to normal cooked mode immediately after; otherwise, the terminal no longer responds to shell commands typed after the program has exited.

This is why the prompt-char() function defined in line 56 (Perl 6 allows dashes in function names) uses the stty utility to run a shell command that enables raw mode and disables it again on leaving the routine. Perl 6 provides the keywords ENTER and LEAVE to perform actions when entering and exiting a function. From the $TTY file handle in the previously opened terminal, read(1) consumes a byte via the prompt-char() function, and the downstream decode() method converts the buffer byte to an ASCII character that the script can later compare with a letter such as "h" (hit) or "s" (stand).

Perl 6 has a switch statement, which goes by the name of given/when and is used in line 31 to compare the player's keyboard input with specified letters and initiate the corresponding game controls.

Perl 6 vs. Perl 5

The game flow in Listing 4 is pretty self-explanatory, and if you compare the lines of code required for the implementation, Perl 6 comes off slightly ahead. Also the dropped parentheses in the for and while loop heads have a positive effect on readability.

Will Perl 6 assert itself? It is probably too early to tell; production tests will decide the outcome. Larry Wall hinted that he was working on a Perl 6 book at the conference in Orlando but left open how long it will take to publish it. If you want to draw lessons from the past, then you might guess that a good chunk of time may pass between announcement and release in Larry's private empire.

Mike Schilli

Mike Schilli works as a software engineer in the San Francisco Bay Area. He can be contacted at mailto:mschilli@perlmeister.com. Mike's homepage can be found at http://perlmeister.com.

Infos

  1. Schrodinger's cat: https://en.wikipedia.org/wiki/Schrodinger%27s_cat
  2. "Quantum Casino" by Michael Schilli, Linux Magazine, January, 2004, pg. 61: http://perlmeister.com/lme/prod-0401.pdf
  3. Listings for this article: ftp://ftp.linux-magazine.com/pub/listings/magazine/191/Perl
  4. Blackjack soft 17 rule: http://www.readybetgo.com/blackjack/strategy/soft-17-rule-2496.html

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Perl: Automating Color Correction

    If you have grown tired of manually correcting color-casted images (as described in last month's Perl column), you might appreciate a script that automates this procedure.

  • Perl: Retouching Photos

    In many cases, whole series of digital images need the same kind of modifications, which forces the photo-grapher to repeat the same steps time and time again in GIMP. Have you ever considered retouching in Perl?

  • Calculating Probability

    To tackle mathematical problems with conditional probabilities, math buffs rely on Bayes' formula or discrete distributions, generated by short Perl scripts.

  • Perl: Call Waiting

    If you can’t get through to your home number, a script can check the line status and send a signal to you via the Web to tell you when the line is free.

  • Perl: PerlPanel

    One panel has a neat collection of applets and another has spectacular looks – but a combination of the two is rare. Now help draws nigh for the desktop: PerlPanel is extensible with do-it-yourself widgets.

comments powered by Disqus

Direct Download

Read full article as PDF:

Price $2.95

News