🔬22. The infix nature of the ternary operator in Perl 6

The ternary operator ?? !! takes three operands, obviously. Although, it is said in the documentation that the operator is an infix. Let us figure out why. Here is the fragment from the Grammar that handles the ternary operator: token infix:sym<?? !!> { :my $*GOAL := ‘!!’; $<sym>=’??’ <.ws> <EXPR(‘i=’)> [ ‘!!’ || <?before ‘::’ <.-[=]>> … Continue reading “🔬22. The infix nature of the ternary operator in Perl 6”

🔬21. The tolerance operator in Perl 6

In Perl 6, there is a so-called approximately-equal operator =~=. It compares two numbers approximately. If both values are non-zero, the operator calculates their relative difference; the tolerance is defined by the $*TOLERANCE variable, which equals to 1E-15 by default. So, for two numbers $a and $b, the result (in pseudo-code) is: |$a – $b| … Continue reading “🔬21. The tolerance operator in Perl 6”

🔬20. What does nqp::getattr do in Perl 6?

In the previous posts, we saw many examples of calling NQP functions from the Perl 6 modules. One of the frequent calls was nqp::getattr. Let us see what that function does. Here are a couple of recent examples: nqp::isge_i($pos,0) && nqp::isconcrete(nqp::getattr(self,List,’$!reified’)) . . . nqp::if( nqp::iseq_i( nqp::getattr( nqp::getattr($self,Code,’$!signature’), Signature, ‘$!count’ ),1) When you first look at … Continue reading “🔬20. What does nqp::getattr do in Perl 6?”

🔬19. Digging into @a[*-1] in Perl 6

After a big work that we had yesterday to understand what’s going on when you try to subscript an array with a negative index, let’s see what actually happens behind the new syntax of getting the last element of an array as @a[*-1]. So, here’s a test program: my @a = <a b c>; say … Continue reading “🔬19. Digging into @a[*-1] in Perl 6”

🔬18. Implementing negative array subscripts in Perl 6

A few days ago, we saw how Perl 6 checks the syntax if you are trying to index an array with negative indices. Since then, I was thinking about implementing the support of @a[-1]. It was not that easy, that’s why I did not demonstrate this last time 🙂 Before going further, a small disclaimer. … Continue reading “🔬18. Implementing negative array subscripts in Perl 6”

🦋17. Parameterised roles in Perl 6

Today, a small excursus into the syntax. Did you know that roles in Perl 6 can have a parameter that makes them similar to generic templates in, say, C++? Here’s a small example: role R { has $.value; method add($b) { $.value + $b.value } method div($b) { $.value / $b.value } } The R … Continue reading “🦋17. Parameterised roles in Perl 6”

🔬16. Unifying the implementation of ‘say’ in Perl 6

For the last two days, the topic of this blog was the internals of the say routine in Rakudo Perl 6. (By the way, the term routine is a good choice if you need to talk about both subs and methods.) In src/core/io_operators.pm, other routines are also defined. The main focus of today is on the … Continue reading “🔬16. Unifying the implementation of ‘say’ in Perl 6”

🔬15. Variants of ‘say’ in Perl 6

Yesterday, we saw four different variants of the multi sub called say. Today, let’s look at them more precisely. The functions are located in the src/core/io_operators.pm file. Start with the first and the simplest one: multi sub say() { $*OUT.print-nl } It just prints the newline to the $*OUT stream. Probably, it would be wise … Continue reading “🔬15. Variants of ‘say’ in Perl 6”

🔬14. Tracking down the ‘say’ calls in Perl 6

Welcome back! Today, we’ll try to do a simple thing using some knowledge from the previous days. Compare the two lines: say ‘Hello, World’; ‘Hello, World’.say; Is there any difference between them? Well, of course. Although the result is the same in both cases, syntactically they differ a lot. In the first case, say is … Continue reading “🔬14. Tracking down the ‘say’ calls in Perl 6”

🔬13. Let 1 + 2 * 3 = 9

Is it easy to break the behaviour of Perl 6? Well, the answer probably depends on what exactly you want to break. Playing with operator precedence, I wanted to change the rules of arithmetical operators + and * so that they are executed in different order, namely, multiplication first, addition second. Sounds like an easy … Continue reading “🔬13. Let 1 + 2 * 3 = 9”

🔬12. The beginning of the Grammar of Perl 6

Yesterday, we talked about the stages of the compiling process of a Perl 6 program and saw the parse tree of a simple ‘Hello, World!’ program. Today, our journey begins at the starting point of the Grammar. So, here is the program: say ‘Hello, World!’ The grammar of Perl 6 is written in Not Quite Perl … Continue reading “🔬12. The beginning of the Grammar of Perl 6”