develooper Front page | perl.perl6.language | Postings from October 2018

Re: "temp" vs "my"

Thread Previous | Thread Next
From:
Jonathan Scott Duff
Date:
October 5, 2018 20:37
Subject:
Re: "temp" vs "my"
Message ID:
CANauOzYg+aPz9yjVbFcoY_pvUCsnO95f2+iZ+HZJRrmvTOY3Zg@mail.gmail.com
What you want is OUTER ...

my $v  = "original";
> {
>     my $v = OUTER::<$v>;
>     say $v;
>     $v = "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 <not.com@gmail.com> 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 = "original";
> {
>     my $v = "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 ½;
>     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 = "original";
> {
>     my $v = $v; # "SORRY! Cannot use variable $v in declaration to
> initialize itself"
>     say "inner value is $v";
>     $v= "new one";
> ...
>
> Gentle reader, how would you succinctly solve this contrived example?
> Anything you like better than this?
>
> my $v = "original";
> given $v -> $v is copy {
>     say "inner value is $v"; # "original", good
>     $v= "new one";
> ....
>
> -y
>

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About