Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^3: How do I train myself to write more Perl-ish Perl, rather than C-ish Perl?

by KurtSchwind (Chaplain)
on Dec 13, 2007 at 19:33 UTC ( [id://656890]=note: print w/replies, xml ) Need Help??


in reply to Re^2: 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?

    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.
  • Comment on Re^3: How do I train myself to write more Perl-ish Perl, rather than C-ish Perl?

Replies are listed 'Best First'.
Re^4: How do I train myself to write more Perl-ish Perl, rather than C-ish Perl?
by chromatic (Archbishop) on Dec 13, 2007 at 20:57 UTC

    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.

Re^4: How do I train myself to write more Perl-ish Perl, rather than C-ish Perl?
by GrandFather (Saint) on Dec 14, 2007 at 01:01 UTC

    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

      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.

        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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://656890]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2024-04-19 13:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found