develooper Front page | perl.perl5.porters | Postings from June 2021

Re: RFC: Multiple-alias syntax for for

Thread Previous | Thread Next
From:
Paul "LeoNerd" Evans
Date:
June 8, 2021 19:34
Subject:
Re: RFC: Multiple-alias syntax for for
Message ID:
20210608203436.5f93fa9c@shy.leonerd.org.uk
On Tue, 8 Jun 2021 14:23:26 -0500
David Nicol <davidnicol@gmail.com> wrote:

> Perl already has this feature.
> 
> 
> @triples % 3 and die "TRIPLES ARRAY NOT MULTIPLE OF 3 IN LENGTH";
> while (@triples){
>     my ($foo, $bar, $baz) = splice @triples, 0, 3;
>     ...
> }

We do, but it's not very nice for two reasons:

 1) DRY failure - you've written `@triples` once in the looping
    condition and then a second time in the `splice`

 2) It's destructive on the array

Combined, that means it's impossible to run it on any non-variable
expression, such as the result of a function call. Whereas, calling a
function is just as possible with the multivariabled foreach:

  foreach my ($red, $green, $blue) (get_palette()) {
    ...
  }

-- 
Paul "LeoNerd" Evans

leonerd@leonerd.org.uk      |  https://metacpan.org/author/PEVANS
http://www.leonerd.org.uk/  |  https://www.tindie.com/stores/leonerd/

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