4

Why does the first expression interpret but not the second?
I understand them to be identical, that is, a string invoking an anonymous regex.

("foo").first: /foo/ andthen say "$_ bar";
> foo bar
"foo": /foo/ andthen say "$_ bar";
> Confused
> at /home/dmc7z/raku/foo.raku:2
> ------> "foo":⏏ /foo/ andthen say "$_ bar";
>     expecting any of:
>         colon pair
3
  • I’m guessing, but from the documentation of thefirst routine (under List), it appears it’s a function that takes a lost and a reflex and returns the first match, ie it’s a function with the concept of matching built in, taking (effectively) two parameters. Your second example, the one that’s failing, has no function or operator which applies a match. Maybe you want ~~ or something (though this won’t do the same thing, ie trim results down to the first match). Commented Jun 23, 2022 at 12:36
  • Raku's first command seems very similar to Raku's grep. Do you see similar issues/problems in syntax? Commented Jun 23, 2022 at 15:07
  • 1
    @jubilatious1j .first(...) is the same as .grep(...).head Commented Jun 25, 2022 at 12:18

1 Answer 1

6

This is a method call:

 ("foo").first: /foo/

It's the same as if you had written:

("foo").first( /foo/ )

Or just:

"foo".first( /foo/ )

(Note that I used : at the end of the three above descriptions in English. That's where the idea to use : to mean that whatever following it is part of the same expression comes from.)

In this case it doesn't make a whole lot of sense to use first. Instead I would use ~~.

"foo" ~~ /foo/ andthen say "$_ bar";

first is used to find the first item in a list that matches some description. If you use it on a single item it will either return the item, or return Nil. It always returns one value, and Nil is the most sensible single undefined value in this case.


The reason it says it's expecting a colon pair is that is the only use of : that could be valid in that location. Honestly I halfway expected it to complain that it was an invalid label.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. Your clarification that it's just a method call is what I needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.