I'm using rakudo, and the following code:
"foo" ~~ m/(foo)/;
say $0;
I thought the output would be:
foo
However, I get:
「foo」
(That's foo with some weird unicode-y quote marks around it.)
I cannot find anything about this in the documentation, and I can't seem to get rid of those quotes. What's happening here?
Edit: Doing
say "$0";
instead gets rid of the quote marks, and both of
print $0;
print "$0";
do too. So I guess the capture isn't actually a string, and putting double quotes around it somehow turns it into a string? (By the way, $0.gist produces 「foo」, not foo.) Can anyone point me to the part of the documentation where I can learn about this behavior? I'm coming from Perl, and thoroughly confused.
A capture returns a Match which stringifies to the matched string as you discovered.
Grouping and Capturing says
An unquantified capture produces a Match object.
BTW, You can see what type the variable actually holds with .WHAT
:
say $0.WHAT;
(Match)
The say
sub calls the .gist
method. By contrast, the print
sub calls the .Str
method. There's also a put
sub ("print using terminator"), which calls .Str
and then does a newline. That's probably what you want to be using instead of say
.
The .gist
and .Str
methods are two different ways to turn an object into a Str
. The .gist
method provides a human-friendly representation of the data that conveys its structure. If you .gist
a complex Match
whith a bunch of captures, it will show those (and use indentation to display the match tree). By contrast, .Str
doesn't try to reproduce structure; on a Match
object, it just gives the text that the Match
covers.
So, to summarize the differences between the Perl 5 and Perl 6 languages that you're running into:
Match
objects, not strings (which is why grammars can produce a parse tree)say
function in Perl 6 calls .gist
put
function in Perl 6 is mostly equivalent to the say
function in Perl 5Finally, the square quotes were picked because they are relatively rare, and thus unlikely to be in any user data, and therefore allow a presentation of the captured data that is very unlikely to need any escape sequences in it. That provides a more easily readable overview of the Match
in question, which is the aim of .gist
.
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