5

Barewords can be used at the left hand side of Pair declarations (this is not documented yet, I'm addressing this issue right now, but I want to get everything right). However, I have not found what is and what's not going to be considered a bareword key anywhere.

This seems to work

say (foo'bar-baz => 3); # OUTPUT: «foo'bar-baz => 3␤»

This does not

say (foo-3 => 3); # OUTPUT: «(exit code 1) ===SORRY!=== Error while compiling /tmp/jorTNuKH9V␤Undeclared routine:␤    foo used at line 1␤␤»

So it apparently follows the same syntax as the ordinary identifiers. Is that correct? Am I missing something here?

jjmerelo
  • 22,578
  • 8
  • 40
  • 86

1 Answers1

10

There are no barewords in Perl 6 in the sense that they exist in Perl 5, and the term isn't used in Perl 6 at all.

There are two cases that we might call a "bare identifier":

  1. An identifier immediately followed by zero or more horizontal whitespace characters (\h*), followed by the characters =>. This takes the identifier on the left as a pair key, and the term parsed after the => as a pair value. This is an entirely syntactic decision; the existence of, for example, a sub or type with that identifier will not have any influence.
  2. An identifier followed by whitespace (or some other statement separator or terminator). If there is already a type of that name, then it is compiled into a reference to the type object. Otherwise, it will always be taken as a sub call. If no sub declaration of that name exists yet, it will be considered a call to a post-declared sub, and an error produced at CHECK-time if a sub with that name isn't later declared.

These two cases are only related in the sense that they are both cases of terms in the Perl 6 grammar, and that they both look to parse an identifier, which follow the standard rules linked in the question. Which wins is determined by Longest Token Matching semantics; the restriction that there may only be horizontal whitespace between the identifier and => exists to make sure that the identifier, whitespace, and => will together be counted as the declarative prefix, and so case 1 will always win over case 2.

Jonathan Worthington
  • 29,104
  • 2
  • 97
  • 136
  • 1
    Would "identifier-like literal" be a better term for that? – jjmerelo Dec 30 '18 at 16:21
  • 2
    I took "bare identifier" from the S02 design docs. It's possible that "auto-quoted identifier" might be suitable, but that may diminish an important difference: pair forms that don't place the key in a quoting construct will be named parameters in the context of an argument list, while those with quotes will not be. So perhaps it's the whole construction - a fatarrow pair being one with a literal identifier to the left of the arrow - that we want to focus on. – Jonathan Worthington Dec 30 '18 at 19:20