Skip to content

Commit 553d4bc

Browse files
Further iteration with corrections and additions
1 parent aa3fe26 commit 553d4bc

File tree

1 file changed

+51
-34
lines changed

1 file changed

+51
-34
lines changed

doc/Language/regexes.pod6

Lines changed: 51 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1598,19 +1598,24 @@ pattern, which may be summarized as follows:
15981598
15991599
Syntax | Description
16001600
===============+===========================================================
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.
16071607
16081608
=end table
16091609
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.
16141619
16151620
my $string = 'Is this a regex or a string: 123\w+False ?';
16161621
my $pattern1 = 'string';
@@ -1642,41 +1647,53 @@ C<「Is」>. Statement C<[4]> shows how the stringified number is used as a matc
16421647
pattern.
16431648
16441649
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_.>,
16461651
but given the regex context the compiler will parse it as the regex wildcard
16471652
L<.|/language/regexes#Wildcards> that matches any character. The apparent
16481653
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;
16671675
my sub f1 { return /s\w+/ };
16681676
my sub f2 (Str $x) { return /$x x/ };
1677+
my sub f3 { return Q[$x] };
16691678
16701679
say $string.match: / <$pattern1> /; # [1] OUTPUT: 「Is」
16711680
say $string.match: / <$number> /; # [2] OUTPUT: 「123」
16721681
say $string.match: / <{ f1 }> /; # [3] OUTPUT: 「string」
1682+
16731683
my $x = "rege";
16741684
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.
16801697
16811698
Note: it may be desired to run arbitrary code from within the regex I<without>
16821699
making use of its return value inside the regex. This may, for instance, come in

0 commit comments

Comments
 (0)