Re: "temp" vs "my"
[email protected] (Jonathan Scott Duff) Wed, 3 Oct 2018 07:13:35 -0500
| Newsgroups | perl.perl6.language |
|---|---|
| Message-ID | <CANauOzYg+aPz9yjVbFcoY_pvUCsnO95f2+iZ+HZJRrmvTOY3Zg@mail.gmail.com> |
--0000000000005bee06057751f800
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
What you want is OUTER ...
my $v =3D "original";
> {
> my $v =3D OUTER::<$v>;
> say $v;
> $v =3D "new one";
> say $v;
> }
> say $v;
It's how you access the outer scope from an inner scope.
-Scott
On Wed, Oct 3, 2018 at 1:10 AM yary <[email protected]> wrote:
> 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
>
--0000000000005bee06057751f800
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr"><div>What you want is OUTER ...</div><div><br></div><block=
quote style=3D"margin:0 0 0 40px;border:none;padding:0px"><div><blockquote =
class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-left:1px sol=
id rgb(204,204,204);padding-left:1ex">my $v=C2=A0 =3D "original";=
<br>{<br>=C2=A0 =C2=A0 my $v =3D OUTER::<$v>;<br>=C2=A0 =C2=A0 say $v=
;<br>=C2=A0 =C2=A0 $v =3D "new one";<br>=C2=A0 =C2=A0 say $v;<br>=
}<br>say $v;</blockquote></div></blockquote><div><br></div>=C2=A0<br><div d=
ir=3D"ltr"><div>It's how you access the outer scope from an inner scope=
.</div><div><br></div><div>-Scott</div></div></div><br><div class=3D"gmail_=
quote"><div dir=3D"ltr">On Wed, Oct 3, 2018 at 1:10 AM yary <<a href=3D"=
mailto:[email protected]">[email protected]</a>> wrote:<br></div><blockq=
uote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc =
solid;padding-left:1ex"><div dir=3D"ltr"><div dir=3D"ltr"><div dir=3D"ltr">=
<div dir=3D"ltr"><div dir=3D"ltr"><div dir=3D"ltr">Reading and playing with=
=C2=A0<a href=3D"https://docs.perl6.org/routine/temp" target=3D"_blank">htt=
ps://docs.perl6.org/routine/temp</a></div><div dir=3D"ltr"><br></div><div>T=
here's an example showing how temp is "dynamic" - that any ju=
mp outside a block restores the value. All well and good.</div><div><br></d=
iv><div>Then I thought, what if I want a lexical temporary value- then use =
"my"- and this is all well and good:</div><div><br></div><div><di=
v>my $v =3D "original";</div><div>{</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] Value before block is left: `$v`&q=
uot;;</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 sleep 1;</div><div>=C2=A0 =C2=
=A0 =C2=A0 =C2=A0 say "[PROMISE] Block was left while we slept; value =
is still `$v`";</div><div>=C2=A0 =C2=A0 }</div><div>=C2=A0 =C2=A0 slee=
p =C2=BD;</div><div>=C2=A0 =C2=A0 say "About to leave the block; value=
is `$v`";</div><div>}</div><div>say "Left the block; value is no=
w `$v`";</div><div>sleep 2;</div></div><div><br></div><div>Then I thou=
ght, well, what if I want to initialize the inner $v with the outer $v.</di=
v><div><br></div><div><div>my $v =3D "original";</div><div>{</div=
><div>=C2=A0 =C2=A0 my $v =3D $v; # "SORRY! Cannot use variable $v in =
declaration to initialize itself"</div><div>=C2=A0 =C2=A0 say "in=
ner 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 su=
ccinctly solve this contrived example? Anything you like better than this?<=
/div><div><br></div><div><div>my $v =3D "original";</div><div>giv=
en $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 "n=
ew one";</div></div><div>....</div><div><br></div><div dir=3D"ltr"><di=
v><div class=3D"m_-13163899487926216gmail_signature">-y<br></div></div>
</div></div></div></div></div></div>
</blockquote></div>
--0000000000005bee06057751f800--