"temp" vs "my"
[email protected] (yary) Tue, 2 Oct 2018 23:10:12 -0700
| Newsgroups | perl.perl6.language |
|---|---|
| Message-ID | <CAG2CFAaZ8mvsGB1yBwVrYY+pvPyYMkSjvjMS2JHKA4BUhHXMCA@mail.gmail.com> |
--000000000000612c5e05774ce556
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Reading and playing with https://docs.perl6.org/routine/temp
There's an example showing how temp is "dynamic" - that any jump outside a
block restores the value. All well and good.
Then I thought, what if I want a lexical temporary value- then use "my"-
and this is all well and good:
my $v =3D "original";
{
my $v =3D "new one";
start {
say "[PROMISE] Value before block is left: `$v`";
sleep 1;
say "[PROMISE] Block was left while we slept; value is still `$v`";
}
sleep =C2=BD;
say "About to leave the block; value is `$v`";
}
say "Left the block; value is now `$v`";
sleep 2;
Then I thought, well, what if I want to initialize the inner $v with the
outer $v.
my $v =3D "original";
{
my $v =3D $v; # "SORRY! Cannot use variable $v in declaration to
initialize itself"
say "inner value is $v";
$v=3D "new one";
...
Gentle reader, how would you succinctly solve this contrived example?
Anything you like better than this?
my $v =3D "original";
given $v -> $v is copy {
say "inner value is $v"; # "original", good
$v=3D "new one";
....
-y
--000000000000612c5e05774ce556
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr"><div dir=3D"ltr"><div dir=3D"ltr"><div dir=3D"ltr"><div di=
r=3D"ltr"><div dir=3D"ltr">Reading and playing with=C2=A0<a href=3D"https:/=
/docs.perl6.org/routine/temp">https://docs.perl6.org/routine/temp</a></div>=
<div dir=3D"ltr"><br></div><div>There's an example showing how temp is =
"dynamic" - that any jump outside a block restores the value. All=
well and good.</div><div><br></div><div>Then I thought, what if I want a l=
exical temporary value- then use "my"- and this is all well and g=
ood:</div><div><br></div><div><div>my $v =3D "original";</div><di=
v>{</div><div>=C2=A0 =C2=A0 my $v =3D "new one";</div><div>=C2=A0=
=C2=A0 start {</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 say "[PROMISE] V=
alue before block is left: `$v`";</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=
=A0 sleep 1;</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 say "[PROMISE] Bloc=
k was left while we slept; value is still `$v`";</div><div>=C2=A0 =C2=
=A0 }</div><div>=C2=A0 =C2=A0 sleep =C2=BD;</div><div>=C2=A0 =C2=A0 say &qu=
ot;About to leave the block; value is `$v`";</div><div>}</div><div>say=
"Left the block; value is now `$v`";</div><div>sleep 2;</div></d=
iv><div><br></div><div>Then I thought, well, what if I want to initialize t=
he inner $v with the outer $v.</div><div><br></div><div><div>my $v =3D &quo=
t;original";</div><div>{</div><div>=C2=A0 =C2=A0 my $v =3D $v; # "=
;SORRY! Cannot use variable $v in declaration to initialize itself"</d=
iv><div>=C2=A0 =C2=A0 say "inner value is $v";</div><div>=C2=A0 =
=C2=A0 $v=3D "new one";</div></div><div>...</div><div><br></div><=
div>Gentle reader, how would you succinctly solve this contrived example? A=
nything you like better than this?</div><div><br></div><div><div>my $v =3D =
"original";</div><div>given $v -> $v is copy {</div><div>=C2=
=A0 =C2=A0 say "inner value is $v"; # "original", good<=
/div><div>=C2=A0 =C2=A0 $v=3D "new one";</div></div><div>....</di=
v><div><br></div><div dir=3D"ltr"><div><div class=3D"gmail_signature">-y<br=
></div></div>
</div></div></div></div></div></div>
--000000000000612c5e05774ce556--