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

Re: RFC: Multiple-alias syntax for for

Thread Previous | Thread Next
From:
Dan Book
Date:
June 8, 2021 19:50
Subject:
Re: RFC: Multiple-alias syntax for for
Message ID:
CABMkAVUDwAACiAX=3mT-qAstFbfjO-QBDfF-tiCsRid8SnFKQQ@mail.gmail.com
On Tue, Jun 8, 2021 at 3:35 PM Paul "LeoNerd" Evans <leonerd@leonerd.org.uk>
wrote:

> 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()) {
>     ...
>   }
>

Also it doesn't work on hashes.

-Dan

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