@@ -1598,19 +1598,24 @@ pattern, which may be summarized as follows:
1598
1598
1599
1599
Syntax | Description
1600
1600
===============+===========================================================
1601
- C « $variable » | Evaluates stringified contents of variable literally.
1602
- C « $(code) | Runs Perl6 code inside the regex, and evaluates the
1603
- | stringified result literally.
1604
- C « <$variable> » | Evaluates stringified contents of variable as a regex.
1605
- C « <{code}> » | Runs Perl6 code inside the regex, and evaluates the
1606
- | result as a regex.
1601
+ C « $variable » | Interpolates stringified contents of variable literally.
1602
+ C « $(code) » | Runs Perl6 code inside the regex, and interpolates the
1603
+ | stringified return value literally.
1604
+ C « <$variable> » | Interpolates stringified contents of variable as a regex.
1605
+ C « <{code}> » | Runs Perl6 code inside the regex, and interpolates the
1606
+ | return value as a regex.
1607
1607
1608
1608
= end table
1609
1609
1610
- Let's start with the first two syntactical forms. These forms will interpolate
1611
- the stringified value of the variable or the stringified return value of the
1612
- code literally, assuming the respective value isn't a Regex. If the value is a
1613
- Regex, it is evaluated as such.
1610
+ Let's start with the first two syntactical forms: C « $variable » and C « $(code) » .
1611
+ These forms will interpolate the stringified value of the variable or the
1612
+ stringified return value of the code literally, provided that the respective
1613
+ value isn't a L < Regex object|/type/Regex > . If the value is a Regex, it will not
1614
+ be stringified, but instead be interpolated as such. 'Literally' means
1615
+ I < strictly literally > , that is: as if the respective stringified value is quoted
1616
+ with a basic C < Q > string L < C < Q[...] > |/language/quoting#Literal_strings:_Q> .
1617
+ Consequently, the stringified value will not itself undergo any (second-level)
1618
+ interpolation.
1614
1619
1615
1620
my $string = 'Is this a regex or a string: 123\w+False ?';
1616
1621
my $pattern1 = 'string';
@@ -1642,41 +1647,53 @@ C<「Is」>. Statement C<[4]> shows how the stringified number is used as a matc
1642
1647
pattern.
1643
1648
1644
1649
Statement C < [5] > does not work as intended. To the human reader, the dot C < . >
1645
- may seem to represent the L < method call operator|/language/operators#methodop_. > ,
1650
+ may seem to represent the L < method call operator|/language/operators#methodop_. > ,
1646
1651
but given the regex context the compiler will parse it as the regex wildcard
1647
1652
L < .|/language/regexes#Wildcards > that matches any character. The apparent
1648
1653
ambiguity may be resolved in various ways, for instance through the use of
1649
- straightforward L < string interpolation|/language/quoting#Interpolation:_qq >
1650
- from the regex as in statement C < [6] > (note that the inclusion of the call
1651
- operator C < () > is key here), or by using the second syntax form from the above
1652
- table as in statement C < [7] > , in which case the match pattern 'string' first
1653
- emerges as the return value of the C < flip > method call. Since general Perl6
1654
- code may be run from within the parentheses of C < $( ) > , the same effect can
1655
- also be achieved with a bit more effort, like in statement C < [8] > . Statement
1656
- C < [9] > illustrates how the stringified version of the code's return value
1657
- (the boolean value C < False > ) is matched literally.
1658
-
1659
- Now consider the second two syntactical forms from the table above. These forms
1660
- will stringify the value of the variable or the return value of the code and
1661
- evaluate it as a regex. If the respective value is a Regex, it is evalued as
1662
- such.
1663
-
1664
- my $string = 'Is this a regex or a string: 123\w+ ?';
1665
- my $pattern1 = '\w+';
1666
- my $number = 123;
1654
+ straightforward L < string interpolation|/language/quoting#Interpolation:_qq > from
1655
+ the regex as in statement C < [6] > (note that the inclusion of the call operator
1656
+ C < () > is key here), or by using the second syntax form from the above table as
1657
+ in statement C < [7] > , in which case the match pattern C < 'string' > first emerges
1658
+ as the return value of the C < flip > method call. Since general Perl6 code may be
1659
+ run from within the parentheses of C < $( ) > , the same effect can also be achieved
1660
+ with a bit more effort, like in statement C < [8] > . Statement C < [9] > illustrates
1661
+ how the stringified version of the code's return value (the boolean value
1662
+ C < False > ) is matched literally.
1663
+
1664
+ Now consider the second two syntactical forms from the table above:
1665
+ C « <$variable> » and C « <${code}> » . These forms will stringify the value of the
1666
+ variable or the return value of the code and interpolate it as a regex. If the
1667
+ respective value is a Regex, it is interpolated as such. 'Interpolated as a
1668
+ regex' means interpolated/inserted into the target Regex without protective
1669
+ quoting. Consequently, the further evaluation of the target Regex may trigger
1670
+ the (second-level) interpolation of any variables it contains.
1671
+
1672
+ my $string = 'Is this a regex or a string: 123\w+$x ?';
1673
+ my $pattern1 = '\w+';
1674
+ my $number = 123;
1667
1675
my sub f1 { return /s\w+/ };
1668
1676
my sub f2 (Str $x) { return /$x x/ };
1677
+ my sub f3 { return Q[$x] };
1669
1678
1670
1679
say $string.match: / <$pattern1> /; # [1] OUTPUT: 「Is」
1671
1680
say $string.match: / <$number> /; # [2] OUTPUT: 「123」
1672
1681
say $string.match: / <{ f1 }> /; # [3] OUTPUT: 「string」
1682
+
1673
1683
my $x = "rege";
1674
1684
say $string.match: / <{ f2($x) }> /; # [4] OUTPUT: 「regex」
1675
-
1676
- In statement C < [4] > use if made of function C < f2 > , which acts as a (very simple)
1677
- "regex factory": you can pass it a string variable, and it will return a Regex
1678
- object into which the variable has been interpolated. In this case, C < f2 > appends
1679
- the letter 'x' to whatever string it is passed.
1685
+ say $string.match: / <{ f3 }> /; # [5] OUTPUT: 「rege」
1686
+
1687
+ In statement C < [4] > use is made of the function C < f2 > , which acts as a (very
1688
+ simple) "regex factory": you can pass it a string variable, and it will return a
1689
+ Regex object into which the variable has been interpolated. In this case, C < f2 >
1690
+ appends the letter 'x' to whatever string it is passed. The Regex that is
1691
+ returned by C < f2 > is in turn inserted into target Regex by the C < {...} >
1692
+ construct. Statement C < [5] > illustrates another case of two-fold regex
1693
+ interpolation. When the target Regex is constructed, the strictly literal string
1694
+ value C < $x > is interpolated into it by the C < {...} > construct. When the Regex is
1695
+ evaluated further, the unprotected variable C < $x > is interpolated, i.e. replaced
1696
+ by the string value C < rege > , which explains the match.
1680
1697
1681
1698
Note: it may be desired to run arbitrary code from within the regex I < without >
1682
1699
making use of its return value inside the regex. This may, for instance, come in
0 commit comments