The start method creates a promise containing a block of code. There is an alternative way to create a promise by calling Promise.start via the start keyword. my $p = start { Β Β Β 42 } (Note that in Perl 6, a semicolon is assumed after a closing brace at the end of a line.) The … Continue reading “π The start keyword in Perl 6 promises”
Category: Raku
π The then method in Perl 6 promises
The then method, when called on an already existing promise, creates another promise, whose code will be called after the βparentβ promise is either kept or broken. my $p = Promise.in(2); my $t = $p.then({say “OK”}); # Prints this in two seconds say “promised”; # Prints immediately sleep 3; say “done”; The code above produces … Continue reading “π The then method in Perl 6 promises”
π An interpreter with Perl 6 grammars
So far, the grammar sees the structure of the programme and can tell if it is grammatically correct, but it does not execute any instructions contained in the programme. In this section, we will extend the parser so that it can actually execute the programme. Our sample language uses variables and integer values. The values … Continue reading “π An interpreter with Perl 6 grammars”
π Unicode in Perl 6
The strings in Perl 6 are internally handled in the format called NFG (Normalization Form Grapheme). From a practical point of view, that means that, for any symbol, you can get its NFC, NFD, NFKC and KFKD forms. I will refer you to read about the details of these formats to the Unicode standard. In … Continue reading “π Unicode in Perl 6”
π Working with files and directories in Perl 6
To get the content of a file, use the slurp built-in function, which reads the whole file and returns a string. say slurp “file.txt”; The function that does the opposite is called spurt, it writes the string to a file. Let us implement the Unixβs cp command in Perl 6. my ($source, $dest) = @*ARGS;Β … Continue reading “π Working with files and directories in Perl 6”
π Database access in Perl 6
Install the DBIish module to get a powerful tool for working with databases*: $ panda install DBIish You also will need the database driver; for example, libmysqlclient for working with MySQL. Check the documentation of the DBIish module on modules.perl6.org if you want to work with a different database engine. The module provides an interface … Continue reading “π Database access in Perl 6”
π Conclusion
Thatβs all, folks. We discussed a lot of things and how they work in PerlΒ 6. I hope that this has been a good introduction to the language for you and that you will be able to use it in your new projects. There are still many topics left. Your starting point for the new bits … Continue reading “π Conclusion”
π The anyof and allof methods in Perl 6 promises
Another pair of factory methods, Promise.anyof and Promise.allof, creates new promises, which will be only kept when at least one of the promises (in the case of anyof) is kept or, in the case of allof, all of the promises listed at the moment of creation are kept. One of the useful examples found in … Continue reading “π The anyof and allof methods in Perl 6 promises”
π An example of using promises: Sleep sort in Perl 6
Finally, a funny example of how promises can be used for implementing the sleep sort algorithm. In sleep sort, every integer number, consumed from the input, creates a delay proportional to its value. As the sleep is over, the number is printed out. Promises are exactly the things that will execute the code and tell … Continue reading “π An example of using promises: Sleep sort in Perl 6”
π Chapter 6. Regexes and Grammars
Grammars in Perl 6 are the βnext levelβ of the well-known regular expressions. Grammars let you create much more sophisticated text parsers. A new domain-specific language (DSL), language translator, or interpreter can be created without any external help, using only the facilities that Perl 6 offers with grammars.
π Regexes (regular expressions) in Perl 6
In fact, Perl 6 just calls regular expressions regexes. The basic syntax is a bit different from Perl 5, but most elements (such as quantifiers * or +) still look familiar. The regex keyword is used to build a regex. Let us create a regex for the short names of weekdays. my regex weekday Β Β Β … Continue reading “π Regexes (regular expressions) in Perl 6”
π The match $/ object in Perl 6
As we have just seen, the smartmatch operator comparing a string with a regex returns an object of the Match type. This object is stored in the $/ variable. It also contains all the matching substrings. To keep (catch) the substring a pair of parentheses is used. The first match is indexed as 0, and … Continue reading “π The match $/ object in Perl 6”
π A simple parser in Perl 6 grammars
The first example of the grammar application is on grammar for tiny language that defines an assignment operation and contains the printing instruction. Here is an example of a programme in this language. x = 42; y = x; print x; print y; print 7; Letβs start writing the grammar for the language. First, we … Continue reading “π A simple parser in Perl 6 grammars”
π Appendix
π Creating a calculator with Perl 6 grammars
When considering language parsers, implementing a calculator is like writing a βHello, World!β programme. In this section, we will create a grammar for the calculator that can handle the four arithmetical operations and parentheses. The hidden advantage of the calculator example is that you have to teach it to follow the operations priority and nested … Continue reading “π Creating a calculator with Perl 6 grammars”
π AST (abstract syntax tree) and attributes in Perl 6 grammars
Now, we are ready to simplify the grammar again after we split the assignment and printout rules into two alternatives each. The difficulty was that without the split, it was not possible to understand which branch had been triggered. You either needed to read the value from the value token or get the name of … Continue reading “π AST (abstract syntax tree) and attributes in Perl 6 grammars”
π Actions in Perl 6 grammars
The grammars in Perl 6 allow actions in response to the rule or token matching. Actions are code blocks that are executed when the corresponding rule or token is found in the parsed text. Actions receive an object $/, where you can see the details of the match. For example, the value of $<identifier> will … Continue reading “π Actions in Perl 6 grammars”
π Foreword
Perl 6 is a programming language that emerged in 2000. In December 2015, the stable version 6.c of the language specification was released. This book is the first one based on the stable version. It is intended to allow a quick dive into Perl 6 and is dedicated to those readers who are already familiar … Continue reading “π Foreword”
π¬ 75. my $x = $x in Perl 6
What happens if youβll try to create a new variable and immediately initialise it by itself, as shown in the following test code: my $x = $x; This does not work (which is expected), but Perl 6 is so kind to the userΒ that it gives an error message prepared especially for this case: ===SORRY!=== … Continue reading “π¬ 75. my $x = $x in Perl 6”
π¦ 74. Typed hashes in Perl 6
In Perl 6, you can restrict the content of a variable container by specifying its type, for example: my Int $i; There is only one value in a scalar variable. You can extend the concept to arrays and let its element to keep only integers, as it is done in the next example: > my … Continue reading “π¦ 74. Typed hashes in Perl 6”