Re: RFC 120 (v5) Implicit counter in for statements, possibly $#.
[email protected] (Piers Cawley) 29 Sep 2000 09:24:06 +0100
| Newsgroups | perl.perl6.language.flow |
|---|---|
| Message-ID | <[email protected]> |
Perl6 RFC Librarian <[email protected]> writes: > This and other RFCs are available on the web at > http://dev.perl.org/rfc/ > > =head1 TITLE > > Implicit counter in for statements, possibly $#. > > =head1 VERSION > > Maintainer: John McNamara <[email protected]> > Date: 16 Aug 2000 > Last Modified: 27 Sep 2000 > Mailing List: [email protected] > Number: 120 > Version: 5 > Status: Frozen > Frozen since: v3 > > =head1 ABSTRACT > > The syntax of the Perl style C<for> statement could be augmented by the > introduction of an implicit counter variable. The deprecated variable > C<$#> could be used for this purpose due to its mnemonic association > with C<$#array>. I'm not sure about the syntax but I like the idea. I've been messing with the Template Toolkit and I keep finding loop.number and loop.index to be startlingly useful. Especially the fact that I get both ordinal and index style counting without having to remember to add/substract 1. Going off on a brainstorm somewhat, it might be nice to have some dynamically scoped 'loop' object (name to be decided) that provides methods like (lifting directly from the template toolkit) first # first time through the loop? last # last time through the loop (where this makes sense) number # 1 based count index # 0 based count size # number of elements in the thing being iterated through max # Max index of thing being iterated through label # The loop label if there is one (just here for # completeness, and it *might* be useful to functions called # from inside the loop. But that way madness lies. Note that size, max and last only make sense where we know beforehand how long the loop is. They should return undef when we're in a a while loop for instance. What we call this special object (array) is, um, ambiguous. $# would do I guess. Note too that, because we could well be inside a loop inside a loop (inside a ...) there may be a case for allowing access to the loop state stuff further up the stack of loops, but this would mean making our special variable into a function (a la caller) and I'm not sure I'm too keen on that; If you need the functionality you can always do C<my $loop_state = $#> and access it further down the loop stack. -- Piers