Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Raku - String invoking anonymous regex

Tags:

raku

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
like image 635
dmc7z Avatar asked Oct 19 '25 07:10

dmc7z


1 Answers

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.

like image 152
Brad Gilbert Avatar answered Oct 22 '25 04:10

Brad Gilbert