Re: "temp" vs "my"
[email protected] (yary) Wed, 3 Oct 2018 08:30:23 -0700
| Newsgroups | perl.perl6.language |
|---|---|
| Message-ID | <CAG2CFAZwLX7LM=Vi6hWs3a6HYLUi6vUM_x7NjmDjzcMBwOJ-Zg@mail.gmail.com> |
--000000000000c09ddb057754b81f Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Thanks! Knew I'd seen the concept of OUTER but couldn't remember the keyword. -y On Wed, Oct 3, 2018 at 5:51 AM, Timo Paulssen <[email protected]> wrote: > you can refer to the outer $v as OUTER::('$v'), that ought to help :) > On 03/10/2018 08:10, yary 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 > > --000000000000c09ddb057754b81f Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable <div dir=3D"ltr">Thanks! Knew I'd seen the concept of OUTER but couldn&= #39;t remember the keyword.</div><div class=3D"gmail_extra"><br clear=3D"al= l"><div><div class=3D"gmail_signature" data-smartmail=3D"gmail_signature">-= y<br></div></div> <br><div class=3D"gmail_quote">On Wed, Oct 3, 2018 at 5:51 AM, Timo Paulsse= n <span dir=3D"ltr"><<a href=3D"mailto:[email protected]" target=3D"_blan= k">[email protected]</a>></span> wrote:<br><blockquote class=3D"gmail_quo= te" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"= > =20 =20 =20 <div text=3D"#000000" bgcolor=3D"#FFFFFF"> <p>you can refer to the outer $v as OUTER::('$v'), that ought t= o help :)<br> </p><div><div class=3D"h5"> <div class=3D"m_4515692280833966869moz-cite-prefix">On 03/10/2018 08:10= , yary wrote:<br> </div> <blockquote type=3D"cite"> =20 <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"h= ttps://docs.perl6.org/routine/temp" target=3D"_blank">https://docs.perl6.or= g/<wbr>routine/temp</a></div> <div dir=3D"ltr"><br> </div> <div>There's an example showing how temp is "dynam= ic" - 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 lexical temporary value- then use "my"- and this is all well and = good:</div> <div><br> </div> <div> <div>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] Valu= e 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 "About to leave the block; va= lue is `$v`";</div> <div>}</div> <div>say "Left the block; value is now `$v`";</= div> <div>sleep 2;</div> </div> <div><br> </div> <div>Then I thought, well, what if I want to initialize the inner $v with the outer $v.</div> <div><br> </div> <div> <div>my $v =3D "original";</div> <div>{</div> <div>=C2=A0 =C2=A0 my $v =3D $v; # "SORRY! Cannot us= e variable $v in declaration to initialize itself"</div> <div>=C2=A0 =C2=A0 say "inner value is $v";</di= v> <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? Anything you like better than this?</d= iv> <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"; # &= quot;original", good</div> <div>=C2=A0 =C2=A0 $v=3D "new one";</div> </div> <div>....</div> <div><br> </div> <div dir=3D"ltr"> <div> <div class=3D"m_4515692280833966869gmail_signature">-y<= br> </div> </div> </div> </div> </div> </div> </div> </div> </blockquote> </div></div></div> </blockquote></div><br></div> --000000000000c09ddb057754b81f--