http://qs321.pair.com?node_id=723911


in reply to Re: C-style for loop
in thread C-style for loop

I personally believe I may come out as an annoying little voice, but I have to join the chorus: that is a statement-modifier-for which happens to have the form of Perl-style C<for> acting on $_ - and saving you the need of parens. But by a (generic) Perl-style C<for> it is meant either:

for my $i (LIST) { ... } # or for (LIST) { ... }

"C-style" is in no way bad "because of useless $i variable" since indeed there may not be any $i variable at all: it's "just" a completely different kind of loop altoghether (a more generic one - and that is fundamentally the reason why it exists at all,) which bears the same name as Perl-style one due to the fact that it can easily be disambiguated from the latter by means of the syntax of what's in the parens. I would go as far as claiming that what's in the parens itself in that case is yet another minilanguage (very closely related to Perl itself: it consists of exactly three Perl statements!) that does not fit in other parts of Perl's syntax, and thus a very special case: of course Perl 6 knows better and gives it an entirely different name which IIRC is C<loop> and not by chance takes one letter more wrt "for" since that's for huffmanization and in fact it is thought to be on a much sparser basis to begin with.

--
If you can't understand the incipit, then please check the IPB Campaign.

Replies are listed 'Best First'.
Re^3: C-style for loop
by JavaFan (Canon) on Nov 17, 2008 at 17:41 UTC
    I would go as far as claiming that what's in the parens itself in that case is yet another minilanguage (very closely related to Perl itself: it consists of exactly three Perl statements!) that does not fit in other parts of Perl's syntax, and thus a very special case: of course Perl 6 knows better and gives it an entirely different name which IIRC is C<loop> and not by chance takes one letter more wrt "for" since that's for huffmanization and in fact it is thought to be on a much sparser basis to begin with.
    A couple of points.
    • I won't deny Perl 6 gives it a different name, but the Perl 5 parser already knows the 'loop' construct. perly.y has a token 'loop' under which all loop, including the bare block and 4 (!) 'for' constructs. (for my $v (), for $v (), for (), are all parsed separately).
    • The C-style for loop doesn't have three statements inside the parens, it has three expressions - each of which may be empty.
    • Each of the expressions is a different kind of expression (first introduces a scope, second is a boolean expression, third may introduce a scope), but they do fit in other parts of Perls syntax. Of course, you are right in the sense that 'for (;;)' is the only place that requires exactly three expressions.
    • I don't understand the huffmanization remark. Huffmanization means you're minimizing the length of the average program, using tokens that are never a prefix of another token. Making little used tokens longer makes sense, but only if you either the freed up token for something that's used more often, or if the freed token is a prefix of some other token that cannot be replaced. So, what keyword starting with 'for' is used in Perl6? Surely you aren't saying in Perl6 'format' is going to be used more often than 'loop'? ;-)
      I won't deny Perl 6 gives it a different name, but the Perl 5 parser already knows the 'loop' construct. perly.y has a token 'loop' under which all loop, including the bare block and 4 (!) 'for' constructs.

      I personally believe I lost you: do you mean that C<loop> is a Perl 5 reserved keyword with some semantics associated to it? It may well be, but I've never seen it in use. Can you give an actual example of usage shedding some light on the issue?

      I (also) personally believe that somehow your phrase is unfinished: "all loop" seems the subject of something... but there's a verb missing.

      Each of the expressions is a different kind of expression (first introduces a scope, second is a boolean expression, third may introduce a scope), but they do fit in other parts of Perls syntax. Of course, you are right in the sense that 'for (;;)' is the only place that requires exactly three expressions.

      Fair enough... both on the fact that they are expressions and that they do fit in other parts of Perl's syntax. Just as a minor point: is it true that the third may introduce a scope too? I knew that only variables declared with my in the initialization section were lexically scoped to the block following the parens...

      I don't understand the huffmanization remark. [ ...]

      I probably used the term improperly, then, based on what I heard: basically it all boils down to having a bare C<for> consisting of three characters for the supposedly more often used Perl-style (and only!) "for" loop and a C<loop> consisting of four characters for the supposedly less often used kind of loop that we now</c> call C-style "for" loop. Two beasts different enough from each other to warrant a totally different name altoghether in Perl 6.

      --
      If you can't understand the incipit, then please check the IPB Campaign.