September 2017
Beginner
402 pages
9h 52m
English
For the user-defined classes, it is possible to create a gist method that prepares the output as desired. Let us try this on the following example.
We create a class for storing chemical formulae. The goal is to allow creating a chemical formula in pure ASCII format and then print it so that numerical indices are displayed as subscripts.
class Chemical {
has $.formula;
method gist {
my $output = $!formula;
$output ~~ s:g/(<[0..9]>)/{(0x2080+$0).chr}/;
$output;
}
}
The Chemical class has a data member $.formula, which keeps the original ASCII formula as a string. The gist method converts it to a string with subscripts. We make a replacement using regular expressions. Regular expressions are covered in detail ...