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

Brace interpolation in strings creates a new scope?

Thread Next
From:
Sean McAfee
Date:
October 26, 2020 17:40
Subject:
Brace interpolation in strings creates a new scope?
Message ID:
CANan03aAk19nCTccOCW6m6R6CB_wPX8NoK48egWa2cL=-q1OyQ@mail.gmail.com
I'm putting together a Raku presentation for my co-workers and have a
section on state variables.  This is my example:

sub how-many {
    state $times;
    say "You called me {++$times} times";
}

> how-many
You called me 1 times
> how-many
You called me 2 times
> how-many
You called me 3 times

Then I thought I'd mention anonymous state variables by modifying my
example thusly:

sub how-many {
    say "You called me {++$} times";
}

But this gives:

> how-many
You called me 1 times
> how-many
You called me 1 times
> how-many
You called me 1 times

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.

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.

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