Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl 6 function argument syntax in the interpreter (REPL)

There seems to be some inconsistencies in the argument syntax in the interpreter. I am using the latest Rakudo. Please see the following terminal output:

$ perl6
To exit type 'exit' or '^D'
> say: "foo"
foo
> say("foo")
foo
> say "foo"
===SORRY!=== Error while compiling:
Two terms in a row
------> say⏏ "foo"
    expecting any of:
        infix
        infix stopper
        statement end
        statement modifier
        statement modifier loop
> 
$ perl6
To exit type 'exit' or '^D'
> say "foo"
foo
> say("foo")
foo
> say: "foo"
foo
> say "foo"
===SORRY!=== Error while compiling:
Two terms in a row
------> say⏏ "foo"
    expecting any of:
        infix
        infix stopper
        statement end
        statement modifier
        statement modifier loop
> 
$ 

It seems that after you have used the ":" or "()" to supply the arguments, you cannot go back to use "", i.e., space, to supply arguments.

Or did I miss something?

Thanks !!!

lisprog

like image 628
lisprogtor Avatar asked Jan 03 '23 17:01

lisprogtor


1 Answers

say: "foo"

That line does not call the say subroutine.

Instead, it declares a statement label with the name say, and then executes the statement "foo" (which does nothing).

The only reason it printed "foo" in your case, is because you entered it into the REPL, which automatically prints the value of the last statement of each line.

If you had used it in a normal program, it would have actually thrown the warning Useless use of constant string "foo" in sink context.

say "foo" ===SORRY!=== Error while compiling: Two terms in a row ------> say⏏ "foo" expecting any of: infix infix stopper statement end statement modifier statement modifier loop

After you've declared the label, the symbol say in this scope no longer refers to the built-in subroutine with that name but rather to your custom label, and it is a syntax error to use a label like that.

The error message should ideally explain that, though. I've submitted a Rakudo ticket for that.

like image 156
smls Avatar answered Jan 12 '23 15:01

smls