develooper Front page | perl.perl6.users | Postings from December 2019

Re: pass by reference

Thread Previous
From:
Joseph Brenner
Date:
December 8, 2019 21:17
Subject:
Re: pass by reference
Message ID:
CAFfgvXWU5DksVk5h0RM8PkxrYHDFr=4KgxD7E39ERHa_ERq1mQ@mail.gmail.com
Thanks, on both points.  Come to think of it, you hardly ever *want*
to pass an aggregate by value, so automatic aliasing ("raw" access)
makes sense as a default.



On 12/8/19, Brad Gilbert <b2gills@gmail.com> wrote:
> Either use `rw` or `raw`
>
> Use `rw` if you need it to be mutable.
>
> Use `raw` if you just want to make sure you are getting the actual
> variable.
>
> That really only applies to `$` parameters though.
>
> ---
>
> `@` and `%` parameters are `raw` already.
>
>     sub pop-random( @_ ) {
>         @_.splice( (0..@_.elems).pick, 1 )
>     }
>
>     my @deck = 1..10;
>     say pop-random @deck; # 7
>     say @deck; # [1 2 3 4 5 6 8 9 10]
>
>
> On Sun, Dec 8, 2019 at 1:26 PM Joseph Brenner <doomvox@gmail.com> wrote:
>
>> What's the sub signature incantation to
>> pass-by-reference do you can act directly
>> on the structure passed in without juggling
>> an alias yourself?
>>
>>   # deal from middle
>>   my $card = pop_random( @deck );
>>
>

Thread Previous


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