Why aren't // and m// exactly synonymous?

Viewed 206

From the examples below, I see that / / and m/ / aren't exactly synonymous, contrary to what I expected. I thought that the only reason to use m/ / instead of / / was that it allows using different delimiters (e.g. m{ }). Why are they different and why would I want to use one versus the other?

I am searching for CSV files in a directory. At first I searched for files ending in csv, thus (all code shown as seen from the Perl 6 REPL):

> my @csv_files = dir( test => / csv $ /  );
["SampleSheet.csv".IO]

but recently a file ending in Csv showed up. So I tried matching case insensitively:

> my @csv_files = dir( test => m:i/ csv $ / );
Use of uninitialized value of type Any in string context.
Methods .^name, .perl, .gist, or .say can be used to stringify it to something meaningful.
  in block <unit> at <unknown file> line 1

I found that I could fix this by putting a block around the matching expression:

> my @csv_files = dir( test => { m:i/ csv $ / } );
["SampleSheet.csv".IO]

However, if I had used a block around the original expression it doesn't match with the bare / /, but it does with m/ /:

> my @csv_files = dir( test => { / csv $ / } );
[]
> my @csv_files = dir( test => { m/ csv $ / } );
["SampleSheet.csv".IO]

Then I found out that if I used the case-insensitive adverb inside / /, it does work:

> my @csv_files = dir( test => /:i csv $ / );
["SampleSheet.csv".IO]

Anyway, / / and m/ / are clearly behaving differently and it's not yet clear to me why.

1 Answers
Related