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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With