Inverted syntax: Difference between revisions

Content added Content deleted
m (syntax highlighting fixup automation)
m (→‎{{header|Raku}}: fix a few old references to Perl 6)
Line 1,133: Line 1,133:
=={{header|Raku}}==
=={{header|Raku}}==
(formerly Perl 6)
(formerly Perl 6)

Like all Perls, Perl 6 has statement modifiers:
Raku has statement modifiers:
<syntaxhighlight lang="raku" line>if $guess == 6 { say "Wow! Lucky Guess!" } # Traditional
<syntaxhighlight lang="raku" line>if $guess == 6 { say "Wow! Lucky Guess!" } # Traditional
say 'Wow! Lucky Guess!' if $guess == 6; # Inverted
say 'Wow! Lucky Guess!' if $guess == 6; # Inverted
Line 1,139: Line 1,140:
say 'Huh! You Guessed Rong!' unless $guess == 6; # Inverted</syntaxhighlight>
say 'Huh! You Guessed Rong!' unless $guess == 6; # Inverted</syntaxhighlight>


Perl also inverts the syntax of loops:
Raku can also invert the syntax of loops:
<syntaxhighlight lang="raku" line>while $i { --$i }
<syntaxhighlight lang="raku" line>while $i { --$i }
--$i while $i;
--$i while $i;
Line 1,149: Line 1,150:
.say if $_ %% 2 for 1..10; # list comprehension</syntaxhighlight>
.say if $_ %% 2 for 1..10; # list comprehension</syntaxhighlight>


Perl 6 has a system of metaoperators that modify the characteristics of normal operators. Among these is the <tt>R</tt> metaoperator, which is able to reverse the arguments of most infix operators (including user-defined ones).
Raku has a system of metaoperators that modify the characteristics of normal operators. Among these is the <tt>R</tt> metaoperator, which is able to reverse the arguments of most infix operators (including user-defined ones).
So a reversed assignment is easy to write:
So a reversed assignment is easy to write:
<syntaxhighlight lang="raku" line>42 R= $_; say $_; # prints 42</syntaxhighlight>
<syntaxhighlight lang="raku" line>42 R= $_; say $_; # prints 42</syntaxhighlight>