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

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

  • Python match

    Exploring the new Python match statement, Python's implementation of switch/case.

  • 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.

comments powered by Disqus
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.

Learn More

News