5 ms·
Perl 6 (Raku) takes it a little further with what's known as the Q lang[1]. In their most literal form possible, strings are created by using a Q followed by an
by xisukar 8y ago
Perl 6 (Raku) takes it a little further with what's known as the Q lang[1].
In their most literal form possible, strings are created by using a Q followed
by any pair delimiting surrounding the text.
Q[This is a literal string.] #=> This is a literal string.
Q!The most literal-est of all.! #=> The most literal-est of all.
Q<So I say 'Hello'> #=> So I say 'Hello'
From there, adverbs are used to sort of expand a literal string's behavior.
For example, `Q:q` (or its more verbose form, `Q:single`) means the string can
interpolate \\, \qq[...] and escaping the delimiter with \.
Q:q[A simple string] #=> A simple string
Q:q{Backslash: \} #=> Backslash: \
Q:q!Backslash: \\! #=> Backslash: \
Q:q{This is a \} so not too early.} #=> This is a } so not too early.
In the other hand, `Q:qq` (or `Q:double`) means the string can
interpolate scalars (`$`), arrays (`@`), hashes (`%`), function calls (`&`),
closure (`{...}`) and backslashes.
my $bool = True;
my @bools = True, False;
my %bools = False => 0, True => 1;
Q:qq[Scalar: $bool] #=> «Scalar: True»
Q:qq{Array: @bools[]} #=> «Array: True False»
Q:qq (Hash: %bools{}) #=> «Hash: False 0True 1»
Q:qq!Function: %bools.keys.join(' and ')! #=> «Function: False and True»
Q:qq<Expression: { 1.0 + 2.0 == 3.0 }> #=> «Expression: True»
In order to prevent unwanted interpolation such as in "username@email.xyz" or
"https://es.wikipedia.org/wiki/%C3%87" https://es.wikipedia.org/wiki/%C3%87", only scalar variables are interpolated
normally. As demonstrated, the array constructor `[]` is used to interpolate
a whole array , the hash constructor `{}` for a hash, etc.
Since both `Q:q` and `Q:qq` are so common in the language, they have more
usual and succinct forms: single quotes (`''`) for `Q:q` and double quotes(`""`)
for `Q:qq`. Furthermore the Q can be dropped so `q` or `qq` (followed by
a delimiter) can be used as well.
From here onwards, a small number of adverbs can be used on the quoting
constructs `q` or `qq` to further restrict/expand a string's behavior.
For example, for a string that only interpolates scalar ($) and closure ({...})
variables, the adverbs `:s` (or `:scalar`) and `:c` (or `:closure`) can be used.
This results in `qq:s:c` (or `qq:sc`, `qq:scalar:closure`). For example:
my $var = 25;
qq:s:c<$var: { 3**2 + 4**2 }> #=> «25: 25»
Another example is the use of `q` with the `:to` (or `:heredoc`) adverb
which seems to be the closest construct to Swift 5's custom string delimiters:
my $billTheCat = q:to/BILL/;
<------- ____
&&& / \ __ _____,
`-- | o \' ` &&/
`| | o },-'
\____( )__/
,' \' \
/~~~~~~|. | .}~~~\
,-----( . | .}--.
| . /\___/
`----^,\ \
\_/
BILL
[1] https://docs.perl6.org/language/quoting https://docs.perl6.org/language/quoting
- b2gills 8y agoThat is slightly wrong; `:qq` implies `:q:b:c:s:a:h`, so `qq:s:c` is the same as just `qq`. You probably wanted to use either `q:s:c` or `Q:s:c`. You can turn off features using the exclamation mark `qq:!a`
- xisukar 8y agoNice catch! It's quite easy to get them wrong since `q` is just a `q` short to be `qq`. ;)