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


in reply to C-style for loop

Perl style
$_ = _clean_cgi_param($_) foreach @values;

"C-style" is bad because of useless $i variable

Replies are listed 'Best First'.
Re^2: C-style for loop
by LanX (Saint) on Nov 15, 2008 at 21:54 UTC
    @cnn: your solution is almost the shortest (well,you can abbreviate foreach with for ; ). And I prefere this one!

    But the OP wants to conform to the styleguides of PBP & Perl Critics, and postfix-loops are disadviced!

    If I was in this situation, I would prefere

    _clean_param($_) for @values;
    with the routine transforming $_[0] directly, (it's an alias)

    UPDATE: or

    _clean_params(@values); # sub takes arr_ref by prototype

    Cheers LanX

    - - - - - Which song???

      But the OP wants to conform to the styleguides of PBP & Perl Critics, and postfix-loops are disadviced

      PBP advises against a lot of useful things. That doesn't mean that you shouldn't use them. It means that you shouldn't use them without a little bit of thought. In the right place, a C-style for loop is Just Fine. In the right place, a "postfix-loop" is Just Fine.

        You missunderstood me, it's the OP who cares about the defaults of Perl::Critics, not me! 8 )

        Cheers LanX

        - - - - - Which song???

        UPDATE: Hmm, I think my position was already quite clear, did you read the whole thread or is PBP just your equivalent to Pawlows bell? (SCNR ; )
Re^2: C-style for loop
by GrandFather (Saint) on Nov 15, 2008 at 20:43 UTC

    No. You could write a C for loop as:

    for ($_ = 0; $_ < @values; ++$_) { ... }

    but that would be a particularly bad way to do it. A C for loop is generally a poorer solution that a Perl for loop because there are more parts to go wrong. A C style for loop is highly suceptible to off by one errors.


    Perl reduces RSI - it saves typing

      Yes, one can use $_ instead of $i, but I talked about the loop specified by OP. And if in theory we can't say what type of a loop is better but in specific case we can.

      Unfortunately OP gave not equivalent loops. The first one modifies original array and the second one creates new array with modified data. I rewrite in perl-style the first loop.

        I gave you a reason for preferring the Perl style for loop in general - fewer parts to go wrong. Eliminating a variable is not generally the important difference between two different for loop usages. Indeed:

        for my $index (0 .. $#array) { ... }

        Is generally a much better solution than:

        for (my $index = 0; $index < $#array; ++$index) { ... }

        because the range of the Perl loop is much easier to see and harder to get wrong.

        In general we can say what sort of for loop is better - Perl for loops. '"C-style" is bad because of useless $i variable' is not the reason however.


        Perl reduces RSI - it saves typing
        Unfortunately OP gave not equivalent loops. The first one modifies original array and the second one creates new array with modified data. I rewrite in perl-style the first loop.

        I personally believe, ccn, that I can understand your point. More precisely, you also dropped a /msg to me, which I'm reproducing hereafter - I hope it's not a problem:

        Re Re^2: C-style for loop OMG! Please look at OP question. He asks about specific "this" loop, not general. I answered about that loop, not general! May be I said not what I actually mean. Not $i but *index* is useless there, coz we can iterate without it

        But then please let me get straight and direct and stress what quite about everyone is trying to say here:

        • first of all, the OP's post is ambiguous at best, with "this" being something that can mean quite about everything both general or very specific: I know he gives an actual example, but it may well be an actual example of what (s)he thinks to be a generic case;
        • the two loops even do different things, as you point out yourself in the first place!

        In view of all this, you did not "rewrite in perl-style the first loop:" but as a statement modifier, which is something still slightly different, albeit certainly more Perl-stylish than the C-style form of C<for> loop.

        --
        If you can't understand the incipit, then please check the IPB Campaign.
Re^2: C-style for loop
by blazar (Canon) on Nov 16, 2008 at 10:28 UTC

    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.
      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.