On Fri, 12 Jan 2024 11:32:05 +0100 (CET)
"Smylers " via perl5-porters <
perl5-...@perl.org> wrote:
> One stupid question that I couldn't see the answer to in your email or
> the PPC: what does the t stand for? It isn't an obvious mnemonic for
> ‘expression’ or ‘code block’ or ‘braces’.
I don't quite recall where it came from but I have a vague memory it
might be "quoted template".
> Raku evaluates {...} blocks in double-quoted strings, and goes for
> interpolation interpretation 2 — this works:
>
> say qq[Right square bracket is Unicode {ord(']')}.];
>
> Try it out:
https://glot.io/snippets/gsdx63bezo
>
> I think that would be the behaviour of least surprise for users:
>
> • Somebody who hasn't thought about the issue would just write code
> like the above not noticing they have a potentially problematic
> embedded closing delimiter.
>
> • Somebody who realizes the potential issue would avoid it by picking
> a different delimiter — which would still work fine.
Yes it sound from descriptions that Ruby and JavaScript take similar
rules there, so we'd be in good company.
> Michael Conrad writes:
>
> > I would also agree that #2 is better for users of perl, but would
> > be a significant burden to the implementors of syntax highlighting.
> >
>
> I don't feel that should massively be taken into account. Syntax-
> highlighting Perl is already awkward, and if I'm writing something
> particularly esoterically nested, I don't necessarily expect syntax
> highlighters to get it right.
Plus I wouldn't be surprised if half of the existing highlighters fail
to correctly implement the rules on existing q/qq/etc.. anyway. ;) It
took us quite some effort in tree-sitter-perl to get the correct set of
behaviours, and that's from knowing a lot of the cornercase traps.
> Philippe Bruhat (BooK) writes:
>
> > I definitely prefer 1) because I think the wording of the PPC
> > implies that qt-strings are parsed just like the other quoted
> > constructs (look for the end, skip escaped delimiters). It's also
> > consistent with how parsing of quoted constructs has been
> > documented for the past 25+ years
> > (75e14d17912ce8a35d5c2b04c0c6e30b903ab97f in June 1998).
>
> Conversely, when adding a new feature, it's good to remedy any
> shortcomings in existing features. ‘You can embed a code block as an
> expression’ is simpler both to teach and market as a feature than ‘You
> can embed a code block as an expression, unless it happens to include
> the closing character for the surrounding string, in which case it
> might not work.’.
>
> Also, qt is distinct from existing quoting mechanisms in that it's the
> first one whose whole raison d'être is to interpolate a block with
> beginning and ending markers. As such, users may reasonably have
> expectations of what can be in it that isn't particularly influenced
> by what can be done inside other types of quotes.
Yes I'm inclined to agree. The entire point is to embed more complex
code structures and I think this kind of thing would come up often
enough to make people think about it more, as compared the case in q/qq
strings where it's very rarely of interest. I think about the only time
I'm ever aware of it is if I try to interpolate elements of a hash that
need quoting around the key names; e.g. this won't work:
say "My user name is $data->{"user-name"}";
Whereas my suggested interpretation 2, would permit
say qt"My user name is {$data->{"user-name"}}";
> > Thinking about how qt should be parsed, I think ... that anything
> > outside of {} should be treated as single-quoted strings
>
> I think that is indeed the intent of PPC 0019, which says in the
> Rationale section: “The proposed qt operator only looks for one
> special token in a string literal: {.‟
>
> However, I see the equivalence in the Specification section implies
> that the constant text A and B would be subject to qq interpretation.
> That contradiction in the PPC should probably be resolved before
> implementation starts.
Oooh, yes - another fine question that I hadn't spotted first time
around.
> (Technically it's only a contradiction if you read A and B as being
> placeholders for arbitrary strings. If you read them as simply being
> the exact strings 'A ' and ' B' then whether they are treated as q or
> qq strings is irrelevant, since they're the same in both.)
>
> But ... I'm concerned that not having \n interpreted as a line-break
> in qt strings could make them less useful.
...
> It also makes it harder to refactor old code to ‘upgrade’ from qq
> strings to qt strings.
...
Yes, a lot of interesting thoughts there that basically come down to
another set of choices, on how to handle the non-{} parts of the qt
string contents.
I think there's three options here, in order of size
1) Treat the characters exactly like q()
2) Treat the characters like the \X-aware parts of qq() but without
$... and @... interpolations
3) Treat the characters exactly like qq(), including variable
interpolations
Clearly the PPC doesn't intend for option 3 - it shouldn't support full
variable interpolations like qq(). There aren't any examples in the PPC
to distinguish options 1 or 2, but I think from the intention of the
part you quote where it says "equivalent to" an example using qq()
instead, suggests we probably should still support those escapes.
That is, an example like:
print qt(My name is { $self->name }\n);
would emit an actual newline sequence, rather than a literal
backslash-n combination.
While this does create yet another kind of weird quoting context that
has its own unique rules, that was already the case for qt() the moment
we picked the rules for {}. That brings the count up to at least four
that I can see - q(), qq(), m() and qt().