pass by reference

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 );
0
doomvox
12/8/2019 7:18:42 PM
perl.perl6.users 1384 articles. 0 followers. Follow

2 Replies
46 Views

Similar Articles

[PageSpeed] 15

--000000000000a778d1059937a45c
Content-Type: text/plain; charset="UTF-8"

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 );
>

--000000000000a778d1059937a45c
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">Either use `rw` or `raw`<div><br></div><div>Use `rw` if yo=
u need it to be mutable.</div><div><br></div><div>Use `raw` if you just wan=
t to make sure you are getting the actual variable.</div><div><br></div><di=
v>That really only applies to `$` parameters though.</div><div><br></div><d=
iv>---</div><div><br></div><div>`@` and `%` parameters are `raw` already.</=
div><div><div><br></div><div>=C2=A0 =C2=A0 sub pop-random(=C2=A0@_ ) {</div=
><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0=C2=A0@_.splice( (0..@_.elems).pick, 1 )</=
div><div>=C2=A0 =C2=A0 }</div></div><div><br></div><div>=C2=A0 =C2=A0 my=C2=
=A0@deck =3D 1..10;</div><div>=C2=A0 =C2=A0 say pop-random=C2=A0@deck; # 7<=
/div><div>=C2=A0 =C2=A0 say=C2=A0@deck; # [1 2 3 4 5 6 8 9 10]</div><div>=
=C2=A0 =C2=A0=C2=A0</div></div><br><div class=3D"gmail_quote"><div dir=3D"l=
tr" class=3D"gmail_attr">On Sun, Dec 8, 2019 at 1:26 PM Joseph Brenner &lt;=
<a href=3D"mailto:doomvox@gmail.com">doomvox@gmail.com</a>&gt; wrote:<br></=
div><blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;bor=
der-left:1px solid rgb(204,204,204);padding-left:1ex">What&#39;s the sub si=
gnature incantation to<br>
pass-by-reference do you can act directly<br>
on the structure passed in without juggling<br>
an alias yourself?<br>
<br>
=C2=A0 # deal from middle<br>
=C2=A0 my $card =3D pop_random( @deck );<br>
</blockquote></div>

--000000000000a778d1059937a45c--
0
b2gills
12/8/2019 9:05:37 PM
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 );
>>
>
0
doomvox
12/8/2019 9:16:47 PM
Reply: