Perl 6 in a hands-on test
Points with Quantum
Listing 3 implements the Blackjack::Hand class. The draw()
method in line 9 expects an object of the Blackjack:Card type and adds it to the player's hand. To determine the count value (or rather the superimposed counts) in the hand, the values()
method in line 20 counts numerical values of all the cards. The critical value of 21 is exceeded if none of the superimposed states shows 21 or less, and the method is_busted()
in line 28 returns a value true in this case.
Listing 3
Hand.pm
To display the player's hand, all you need to do is put a Blackjack::Hand class object in a string context (i.e., in double quotes). This tells Perl 6 to invoke the object's Str()
method and use its return value. To allow this to happen, the function in line 32 of Listing 3 uses the cards()
method to iterate through all the cards in the player's hand, puts each card object in a string context in line 36, and joins the results using dashes as separators.
The string returned in line 38 contains the name of the player, the cards in the hand, and the score determined by the highval()
method. The operator for joining two strings mutated from a dot (.
) in Perl 5 to a tilde (~
) in Perl 6. Line 36 thus uses ~ =
to append the string on the right to the string on the left.
Jackpot with Blackjack
If a player is holding a card with a value of 10 along with an ace, this counts as a blackjack. If the dealer does not have the same combination, the player is paid 1.5 times their bet as winnings. The is_blackjack()
method tests for this combination as of line 42, by ensuring that the hand consists of exactly two cards and that one of the superposed states scores exactly 21.
The winning points earned by the player are determined by the score()
method in line 47, which expects the dealer's hand object as an argument for the comparison. The return value is -1
if the player has gone bust, no matter what the dealer has. If the dealer goes bust, the return value is 1
, and the player is one point ahead. If both have a blackjack, the result is a tie and 0
is returned. Note that the ternary operator in Perl 5, using the syntax [...]?[...]:[...]
to return the first or second expression depending on the test, has mutated in Perl 6 to [...]??[...]!![...]
.
Command-Line Casino
The controlling Perl script of this command-line casino blackjack.p6
is shown in Listing 4; its first line contains a reference to the executing Perl 6 interpreter. But where do you find Perl 6? On perl6.org, you will find a tarball which you can build yourself while hundreds of warnings flash across the screen. The easiest approach for a modern Linux enthusiast is to run docker pull rakudo-star
to download a Docker image with Perl 6 Rakudo installed.
Listing 4
blackjack.p6
To call a Perl 6 script residing on the host in the Perl 6 interpreter within the Docker container, Listing 4 defines the path to the shell script in Listing 5 in its first line, or shebang. This starts the Docker container and defines a mount in the current directory on the host, which is then visible within the container below /perl6
. This is why line 4 of Listing 4 adds an additional search path for modules in the form of use lib '/perl6'
so that it also finds the blackjack modules from the other listings.
Listing 5
perl6-in-docker.sh
§§nunumber 1 #!/bin/sh 2 3 sudo docker run -v `pwd`:/perl6 -i \ 4 -t ready /usr/bin/perl6 /perl6/$*
Because the Linux kernel does not allow shell scripts as shebang programs for security reasons, Listing 4 uses the helper /usr/bin/env
as the executable that invokes the shell script; the Linux kernel has no trouble with this.
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
-
Endless OS 6 has Arrived
After more than a year since the last update, the latest release of Endless OS is now available for general usage.
-
Fedora Asahi 40 Remix Available for Macs with Apple Silicon
If you've been anticipating KDE's Plasma 6 for your Apple Silicon-powered Mac, then you're in luck.
-
Red Hat Adds New Deployment Option for Enterprise Linux Platforms
Red Hat has re-imagined enterprise Linux for an AI future with Image Mode.
-
OSJH and LPI Release 2024 Open Source Pros Job Survey Results
See what open source professionals look for in a new role.
-
Proton 9.0-1 Released to Improve Gaming with Steam
The latest release of Proton 9 adds several improvements and fixes an issue that has been problematic for Linux users.
-
So Long Neofetch and Thanks for the Info
Today is a day that every Linux user who enjoys bragging about their system(s) will mourn, as Neofetch has come to an end.
-
Ubuntu 24.04 Comes with a “Flaw"
If you're thinking you might want to upgrade from your current Ubuntu release to the latest, there's something you might want to consider before doing so.
-
Canonical Releases Ubuntu 24.04
After a brief pause because of the XZ vulnerability, Ubuntu 24.04 is now available for install.
-
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.