develooper Front page | perl.perl5.porters | Postings from June 2022

Re: pre-RFC: template strings

Thread Previous | Thread Next
From:
Graham Knop
Date:
June 12, 2022 21:37
Subject:
Re: pre-RFC: template strings
Message ID:
CAM=m89E0Re9-Yz=v712UKHw984zAwLXXtne26JJu4K3CEJVxig@mail.gmail.com
On Sun, Jun 12, 2022 at 8:47 PM Ricardo Signes
<perl.p5p@rjbs.manxome.org> wrote:
>
> Porters,
>
> I like the current proposal for a ?-> operator, but qq{Foo $Bar?->[0]} would become weird.  Either it would optionally dereference or it would not match the normal expression when interpolating.  (Methods not working this way already confuse people.)  The same problem already exists for postfix dereferencing like qq{$Foo->@*} without the postderef_qq feature.
>
> I would like to propose we blithely steal template literals from JavaScript, with some modifications.  We add a new quote-like operator, "qt", which does not interpolate like qq, but has its own method for interpolation.  We ignore the "tagged templates" feature of JavaScript.
>
> In a qt string, there are two special forms:  ${ EXPR } and @{ EXPR }.  These evaluate the expression in them in scalar and list contexts, respectively, and then interpolate the result.  The list form joins the list elements with $".
>
> This eliminates the need for the @{[ … ]} form in qq strings.  It means you can easily interpolate method calls and simple expressions like $a+$b.
>
> I'm not 100% happy with the propose spelling of the special forms, and we could get by eliminating the list one (because "join" exists).  I think the feature's worth adding, anyway.  Objections / endorsements before I write something more formal?
>

I like the general idea. I don't really like the spelling though, as
${ } already has meaning in the language and in interpolated strings.
I'd say that the @{} form is not needed. Eliminating it could allow a
better spelling for the ${ } form.

Prior art: https://metacpan.org/pod/Quote::Code

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About