develooper Front page | perl.perl6.users | Postings from October 2020

Re: Brace interpolation in strings creates a new scope?

Thread Previous | Thread Next
From:
Elizabeth Mattijsen
Date:
October 26, 2020 19:04
Subject:
Re: Brace interpolation in strings creates a new scope?
Message ID:
D5CABEF0-A005-4EAE-965C-6FE102A0F8D4@dijkmat.nl
> On 26 Oct 2020, at 18:40, Sean McAfee <eefacm@gmail.com> wrote:
> Is this the intended behavior?  The doc page on quoting constructs just says that values can be interpolated with braces, but (at least to my eyes) doesn't suggest that this involves creating a new scope, or a new function, or however it is that this happens.

I guess we need to update the documentation.  But the braces inside a double quoted string *do* create a new scope, causing the behaviour of ++$ that you have seen.

> For my presentation I can just alter some lines to:
> 
>     say "You called me ", ++$times, " times";
> 
> and
> 
>     say "You called me ", ++$, " times";
> 
> ...but that seems a bit less elegant than what I had originally.

Alternately you could do:

   say "You called me " ~ ++$ ~ " times";

Personally, I would avoid the use of the anonymous state variable.  I think it is an example where Larry would nowadays apply rule #2.



Liz
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