7

I was trying to convert a match object to a string in perl6. The method Str on a match object is defined as:

method Str(Match:D: --> Str:D)

I would think I could use Str($match) to accomplish this. And it seems to convert it to a string, but I'm getting an error using the string with the following code:

my $searchme = "rudolph";
my $match = $searchme ~~ /.*dol.*/;
say $match.WHAT;
my $test1 = Str($match);
say $test1.WHAT;
say $test1;

With the output:

(Match)
(Str)

With the error:

Cannot find method 'gist': no method cache and no .^find_method in block at .code.tio line 6

However, if I run:

my $searchme = "rudolph";
my $match = $searchme ~~ /.*dol.*/;
say $match.WHAT;
my $test1 = $match.Str;
say $test1.WHAT;
say $test1;

I get no error and the result:

(Match)
(Str)
rudolph

Is this a bug or me misunderstanding how it works?

Thanks for reading.

James Jones
  • 3,850
  • 5
  • 25
  • 44
MorayJ
  • 537
  • 2
  • 8
  • 2
    It's a bug. Perhaps you just golfed [this](https://github.com/rakudo/rakudo/issues/1800). `dd $test1;` instead of `say $test1;` is helpful in that it displays `BOOTStr $test1 = (BOOTStr without .perl method)`. Based on that I [searched the rakudo repo for `BOOTStr`](https://github.com/rakudo/rakudo/search?q=BOOTStr) and that led to the above issue. I will try to golf it further but thought I'd post this comment in the meantime. – raiph Jul 28 '18 at 12:58
  • As an aside, I usually use `~` to coerce a match to a string. `my $text = ~$match` – donaldh Jul 31 '18 at 08:11

1 Answers1

8

I'm writing this up as an answer even though it's actually an incomplete discussion of a bug, so not at all normal SO fare. The alternative of lots of comments doesn't seem better.


It's a bug. Perhaps you just golfed this.

dd $test1; instead of say $test1; is helpful in that it displays BOOTStr $test1 = (BOOTStr without .perl method).

Based on that I searched the rakudo repo for BOOTStr and that led to the above issue.

Golfing it further leads to:

say $ = Str(Match.new);

Note that these are all fine:

say Str(Match.new);
say $ = Int(Match.new);
say $ = Str(Date.new: '2015-12-31');

It appears to be a combination of leaking some implementation details regarding how Rakudo/NQP/MoarVM bootstrap; Match being an NQP object; Str() on that being wonky; and assigning it to a Scalar container (the $ is an anonymous one) making that wonkiness visible.

I'll add more when/if I figure it out.

raiph
  • 31,607
  • 3
  • 62
  • 111