in reply to Re^4: How do I train myself to write more Perl-ish Perl, rather than C-ish Perl?
in thread How do I train myself to write more Perl-ish Perl, rather than C-ish Perl?

I don't want to get hung up on loops as that is just one C-style construct under discussion. Also, I don't want to be in the position of having to defend C. Especially on a site called "PerlMonks".

That said, I agree most of what you said above. Although, I think that people who natively think in C don't have any problems reading

for ($i = 0; $i < $BLAH; ++$i) { ...}
. For a native C thinker to make a mistake with that construct is pretty rare.

But when you have the luxury of perl and you aren't forced to use C constructs, why not use

for my $foo (@bar) { . . . }
I agree completely. It's the better solution. But my point is I wouldn't wring my hands over using the C construct if you don't normally make mistakes with it.

--
I used to drive a Heisenbergmobile, but every time I looked at the speedometer, I got lost.

Replies are listed 'Best First'.
Re^6: How do I train myself to write more Perl-ish Perl, rather than C-ish Perl?
by GrandFather (Saint) on Dec 14, 2007 at 02:44 UTC

    Yay to be someone who doesn't make misteakes! Personally, I've been programming C/C++ for somewhere near 20 years and Perl for a little over 2 - I still occasionally screw up C for loops from time to time, not least because you can put all sorts of disparate stuff in the loop header.

    If I really need to use a loop to index into arrays in Perl, then for my $i (0 .. $#array) is much safer (and more readable IMO) than the C variant.

    Actually there's not much more than the C for loop that either isn't already Perl syntax, is completely different (subs, pointers, structs) or unavailable (switch). Perhaps the other major areas where C programmers tend to show their colors are in variable scoping and data structures. If your experience is really C++ rather than C, then even those differences ought to be much less (although C++ doesn't come anywhere near Perl's facility with data structures).


    Perl is environmentally friendly - it saves trees