Re: "temp" vs "my"

[email protected] (Timo Paulssen) Wed, 3 Oct 2018 14:51:33 +0200
Newsgroups perl.perl6.language
Message-ID <[email protected]>
--------------8D45E6C85DC7C2D35B80A192
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable

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=C2=A0https://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";
> {
> =C2=A0 =C2=A0 my $v =3D "new one";
> =C2=A0 =C2=A0 start {
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 say "[PROMISE] Value before block is left: =
`$v`";
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 sleep 1;
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 say "[PROMISE] Block was left while we slep=
t; value is still
> `$v`";
> =C2=A0 =C2=A0 }
> =C2=A0 =C2=A0 sleep =C2=BD;
> =C2=A0 =C2=A0 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";
> {
> =C2=A0 =C2=A0 my $v =3D $v; # "SORRY! Cannot use variable $v in declara=
tion to
> initialize itself"
> =C2=A0 =C2=A0 say "inner value is $v";
> =C2=A0 =C2=A0 $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 {
> =C2=A0 =C2=A0 say "inner value is $v"; # "original", good
> =C2=A0 =C2=A0 $v=3D "new one";
> ....
>
> -y

--------------8D45E6C85DC7C2D35B80A192
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable

<html>
  <head>
    <meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3DUTF=
-8">
  </head>
  <body text=3D"#000000" bgcolor=3D"#FFFFFF">
    <p>you can refer to the outer $v as OUTER::('$v'), that ought to
      help :)<br>
    </p>
    <div class=3D"moz-cite-prefix">On 03/10/2018 08:10, yary wrote:<br>
    </div>
    <blockquote type=3D"cite"
cite=3D"mid:[email protected]=
l.com">
      <meta http-equiv=3D"content-type" content=3D"text/html; charset=3DU=
TF-8">
      <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"
                    moz-do-not-send=3D"true">https://docs.perl6.org/routi=
ne/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 lexical temporary
                  value- then use "my"- and this is all well and good:</d=
iv>
                <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] Value b=
efore 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] Block w=
as 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; value=
 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 use v=
ariable $v
                    in declaration to initialize itself"</div>
                  <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? Anything you like better than this?<=
/div>
                <div><br>
                </div>
                <div>
                  <div>my $v =3D "original";</div>
                  <div>given $v -&gt; $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>....</div>
                <div><br>
                </div>
                <div dir=3D"ltr">
                  <div>
                    <div class=3D"gmail_signature">-y<br>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </blockquote>
  </body>
</html>

--------------8D45E6C85DC7C2D35B80A192--