4

The Perl6 standard grammar is relatively large. Although this facilitates expression once mastered, it creates a barrier to mastery. For instance, core constructs often have multiple forms supporting different programming paradigms. A basic example is the variety of syntaxes for creating Pairs:

Pair.new('key', 'value'); # The canonical way 
'key' => 'value';         # this... 
:key<value>;              # ...means the same as this 
:key<value1 value2>;      # But this is  key => <value1 value2> 
:foo(127);                # short for  foo => 127 
:127foo;                  # the same   foo => 127

Note, in particular, the comment on the first form: "The canonical way".

Another example is the documentation for method make:

This is just a little sugar for $/.made = $ast which is a very common operation in actions.

Is there a canonical form that one may output for a Perl6 program so that, having mastered the canonical sub-grammar, one may inspect any Perl6 program in that form to comprehend it?

user3673
  • 665
  • 5
  • 21

1 Answers1

8

I'd say that the Perl6 grammar (in particular the roast) is the canon, so all those forms are sort of 'canonical'. That comment refers to what is actually happening when any of the other forms are compiled/executed. The .new() method for the Pair class gets called to create the new Pair object. That happens, behind the scenes, so to speak, regardless of which of the options you use. The other syntaxes are just easier ways to express the same thing.

You might find the .perl() method helpful. It will describe the way any variable can be expressed in Perl:

> Pair.new('key', 'value').perl
:key("value")
> ('key' => 'value').perl
:key("value")
> (:key<value>).perl
:key("value")
Curt Tilmes
  • 3,035
  • 1
  • 12
  • 24