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

Re: pass by reference

Thread Previous | Thread Next
From:
Brad Gilbert
Date:
December 8, 2019 21:05
Subject:
Re: pass by reference
Message ID:
CAD2L-T0LY+YkTOKXb4kJxGOnpFffN9yu6VOF9kHKUSSAx+-7ww@mail.gmail.com
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 | 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