Re: "temp" vs "my"

[email protected] (Brad Gilbert) Wed, 3 Oct 2018 19:19:04 -0500
Newsgroups perl.perl6.language
Message-ID <CAD2L-T3H+-Ct8aLn7CX4v0KPNtCaZr4BCSmB6Dr5cQ0X=DhEpg@mail.gmail.com>
Note that OUTER::<$v> only goes up one level.

So to go up two levels OUTER::OUTER::<$v>

There is also OUTERS::<$v> which will go up as many levels as it needs
to find the variable

    {
       my $a =3D 1;
       my $b =3D 2;
       {
           my $a =3D 3;
           {
               say OUTER::<$a>; # 3
               say OUTER::OUTER::<$a>; # 1

               say OUTERS::<$a>; # 3  # only one level
               say OUTERS::<$b>; # 2  # two levels
           }
       }
    }
On Wed, Oct 3, 2018 at 10:31 AM yary <[email protected]> wrote:
>
> Thanks! Knew I'd seen the concept of OUTER but couldn't remember the keyw=
ord.
>
> -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 ini=
tialize itself"
>>     say "inner value is $v";
>>     $v=3D "new one";
>> ...
>>
>> Gentle reader, how would you succinctly solve this contrived example? An=
ything 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
>
>