On Sun, 7 Jan 2024 16:16:38 -0000
"Paul Webster" via perl5-porters <
perl5-...@perl.org> wrote:
> Here is the syntax that I’m proposing.
> ${^LOOPCOUNT} and ${^LOOPINDEX} (Naming is not final, willing
> to change if people would prefer something else}, available in every
> FOR & FOREACH.
(Having originally handwaved a suggestion on IRC for this) - I imagined
somewhat shorter names, maybe ${^INDEX} and ${^COUNT}
An idea for a usecase could be
foreach my $row (@rows) {
say "<table>" if ${^INDEX} == 0;
say "<tr>" . $row->as_html . "</tr>";
say "</table>" if ${^INDEX} == ${^COUNT}-1;
}
This way, if there are no rows we don't print an empty table container.
It's a bit shorter than the alternative
if(@rows) {
say "<table>";
foreach my $row (@rows) { ... }
say "</table>";
}
> Here are the potential problems.
> Minor overhead added to all loops, though perhaps this could
> be a triggerable feature requiring a “use feature ‘infoloop’” or such.
I was thinking about this too. I think if you said they had dynamic
scope, then it would indeed add a performance hit everywhere.
*But* if they are lexically scoped, only taking effect inside a lexical
foreach loop, then at compiletime we know which loops need them and can
account for them. It means the cost isn't paid anywhere else.
But overall - yes I would like to see a PPC document on this at least
so we can discuss it in further detail.
--
Paul "LeoNerd" Evans
leo...@leonerd.org.uk |
https://metacpan.org/author/PEVANS
http://www.leonerd.org.uk/ |
https://www.tindie.com/stores/leonerd/