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
-
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
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.