in reply to C-style for loop
$_ = _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 | |
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 with the routine transforming $_[0] directly, (it's an alias) UPDATE: or
Cheers LanX - - - - - Which song??? | [reply] [d/l] [select] |
by DrHyde (Prior) on Nov 17, 2008 at 10:15 UTC | |
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. | [reply] |
by LanX (Saint) on Nov 17, 2008 at 15:32 UTC | |
Cheers LanX - - - - - Which song??? | [reply] |
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:
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 | [reply] [d/l] |
by ccn (Vicar) on Nov 15, 2008 at 21:05 UTC | |
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. | [reply] [d/l] [select] |
by GrandFather (Saint) on Nov 15, 2008 at 21:26 UTC | |
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:
Is generally a much better solution than:
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 | [reply] [d/l] [select] |
by ccn (Vicar) on Nov 15, 2008 at 22:01 UTC | |
by GrandFather (Saint) on Nov 15, 2008 at 23:08 UTC | |
by ikegami (Patriarch) on Nov 16, 2008 at 01:42 UTC | |
by zwon (Abbot) on Nov 15, 2008 at 23:00 UTC | |
by blazar (Canon) on Nov 18, 2008 at 14:28 UTC | |
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: 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. | [reply] [d/l] [select] |
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:
"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. | [reply] [d/l] [select] |
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. | [reply] |
by blazar (Canon) on Nov 18, 2008 at 13:59 UTC | |
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. | [reply] [d/l] [select] |