🔬9. Obsolete syntax error messages in Perl 6, part 1

Yesterday, we saw an error message about the improper syntax of the ternary operator. Let’s look at other similar things that the Rakudo designers has implemented for us to make the transition from Perl 5 smoother.

First of all, the Perl 6 grammar file (src/Perl6/Grammar.nqp) contains four different methods for reacting to obsolete syntax:

Three of these methods throw exceptions, the fourth one prints a warning. The final text of the error message is using the information from the arguments of the methods. For example, this is what we saw yesterday:

<.obs('? and : for the ternary conditional operator', '?? and !!')>

This part of the token regex is transformed to the following error message (the parts from the regex are highlighted):

Unsupported use of ? and : for the ternary conditional operator;
in Perl 6 please use ?? and !!

Obsolete syntax

Let us see what other messages we have in the current Rakudo Perl 6 compiler.

Negative indices

The first example is very likely one of the most common mistake that a Perl 5 programmer faces when programming in Perl 6.

$ perl6 -e'my @a; say @a[-1]'
===SORRY!=== Error while compiling -e
Unsupported use of a negative -1 subscript to index from the end;
in Perl 6 please use a function such as *-1
at -e:1
------> my @a; say @a[-1]⏏

To count from the end of the array, you should use a WhateverCode instead of negative integers. This is how the error message is encoded in the src/Perl6/Actions.nqp file (notice that this is an NQP module, not the Perl 6 one, while the syntax is very clear):

The $c variable is the current symbol in the syntax tree, and the $ix is a negative index taken from the square brackets (notice the position of the capturing parentheses inside the regex). If there is a negative index, an error message is generated for your pleasure.

The rest of the .obs calls happen in the src/Perl6/Grammar.nqp file.

Perl 6 loop, not C-style for

The for loop in Perl 6 is designed to work with lists or arrays, so using it in the C-style, which is allowed in Perl 5, is prohibited: