in reply to How do I train myself to write more Perl-ish Perl, rather than C-ish Perl?

I guess I have a different take.

Why bother? If the C-style works for you then stick with it. I have the same "problem" in that I think in C and kind of do a translation. I reach for C loops all the time when I could be using a map or something. Maybe that's not ideal. But it certainly works and it's easy to maintain. It's also easy to read.

Part of what I like about Perl is that I can write in a C-style and then when I hit a bump (sort of like you have there with wanting to jump out of the outer loop from the inner one) THEN I can use some Perl nicities.

--
I used to drive a Heisenbergmobile, but every time I looked at the speedometer, I got lost.
  • Comment on Re: How do I train myself to write more Perl-ish Perl, rather than C-ish Perl?

Replies are listed 'Best First'.
Re^2: How do I train myself to write more Perl-ish Perl, rather than C-ish Perl?
by chromatic (Archbishop) on Dec 13, 2007 at 19:07 UTC
    Why bother? If the C-style works for you then stick with it.

    Fencepost errors, for one (or is that zero?).

          Fencepost errors, ...

      By definition, that pretty much violates the "works for you" part of my comment, doesn't it?

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

        That depends on who finds the bug and when. I know I can write a C-style for loop correctly, most of the time, but I don't trust myself to write it reliably all of the time, and so I try to avoid them whenever possible.

        There is really only one interpretation you can place on for my $element (@array) and very few ways it can go wrong whether you know Perl or not. On the other hand for ($i = 0; $I < $numOfElements; ++$i) {...} requires a lot of interpretation even if you know C and there are many places it can go wrong. One of the reasons Perl seems to work as expected first off more often than C (and I know this to be true from personal experience) is that you can avoid exactly this sort of issue the vast majority of the time in Perl.

        Some Perl idiom is just weird and non-obvious to the non-Perl programmer, but much of it is understandable without arcane Perl knowledge (and simply pulsates with succinct power).


        Perl is environmentally friendly - it saves trees
Re^2: How do I train myself to write more Perl-ish Perl, rather than C-ish Perl?
by mpeppler (Vicar) on Dec 13, 2007 at 16:09 UTC
    I'm with you on this. It is particularly true when you are writing perl in a shop where most people don't know it, or if perl is not your primary language. Using a C loop may not be as concise, and maybe not as fast, but almost anyone who has ever written a program will be able to understand it.

    Michael

Re^2: How do I train myself to write more Perl-ish Perl, rather than C-ish Perl?
by Cop (Initiate) on Dec 18, 2007 at 05:23 UTC

    Bless you!