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
- Schrodinger's cat: https://en.wikipedia.org/wiki/Schrodinger%27s_cat
- "Quantum Casino" by Michael Schilli, Linux Magazine, January, 2004, pg. 61: http://perlmeister.com/lme/prod-0401.pdf
- Listings for this article: ftp://ftp.linux-magazine.com/pub/listings/magazine/191/Perl
- Blackjack soft 17 rule: http://www.readybetgo.com/blackjack/strategy/soft-17-rule-2496.html
« Previous 1 2 3 4 5
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Linux Servers Targeted by Akira Ransomware
A group of bad actors who have already extorted $42 million have their sights set on the Linux platform.
-
TUXEDO Computers Unveils Linux Laptop Featuring AMD Ryzen CPU
This latest release is the first laptop to include the new CPU from Ryzen and Linux preinstalled.
-
XZ Gets the All-Clear
The back door xz vulnerability has been officially reverted for Fedora 40 and versions 38 and 39 were never affected.
-
Canonical Collaborates with Qualcomm on New Venture
This new joint effort is geared toward bringing Ubuntu and Ubuntu Core to Qualcomm-powered devices.
-
Kodi 21.0 Open-Source Entertainment Hub Released
After a year of development, the award-winning Kodi cross-platform, media center software is now available with many new additions and improvements.
-
Linux Usage Increases in Two Key Areas
If market share is your thing, you'll be happy to know that Linux is on the rise in two areas that, if they keep climbing, could have serious meaning for Linux's future.
-
Vulnerability Discovered in xz Libraries
An urgent alert for Fedora 40 has been posted and users should pay attention.
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs