Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Doing perl efficiently

by relientmark (Novice)
on May 18, 2011 at 11:18 UTC ( [id://905454]=perlquestion: print w/replies, xml ) Need Help??

relientmark has asked for the wisdom of the Perl Monks concerning the following question:

For the past 3 months I've been learning perl. I'm comfortable that I understand the basics of perl, I've written code to do what I need. Although I want to make sure, I'm writing efficient (I know I'm not) code, and doing things the "right" way. Has anyone got any pointers/tutorials/links or general advise? Cheers M

Replies are listed 'Best First'.
Re: Doing perl efficiently
by GrandFather (Saint) on May 18, 2011 at 11:31 UTC

    One thing you could do is post code you are unhappy with here and see what others have to offer.

    Another way you would likely improve your Perl quickly is to jump on the Seekers of Perl Wisdom band wagon as a peruser and contributor. Look at questions that are (or have been) asked and attempt to answer them yourself. Compare your answer with the other answers that are offered and see what can be learned that way. You needn't actually post your answer - just the exercise of writing the code before you look at other answers is likely to be valuable.

    True laziness is hard work
Re: Doing perl efficiently
by Anonymous Monk on May 18, 2011 at 11:41 UTC
Re: Doing perl efficiently
by LanX (Saint) on May 18, 2011 at 11:33 UTC
    General advice: Follow one thread of perlmonks per day to get an idea...

    There are no right way, just different groups with different coding stiles.

    But if you insist there this one way, you could certainly explain how wrong your code is... ;)

    Cheers Rolf

      That's so simple I would never have thought of it. My Perl has been stagnant for years. A damn good idea.
      Me
Re: Doing perl efficiently
by jpl (Monk) on May 18, 2011 at 12:14 UTC
    I'll second the recommendation for Devel::NYTProf. It is a fantastic profiler! That said, your efficiency and that of those that will have to read or support your code is at least as important as the efficiency of the code itself. Go first for correctness and clarity. Worry about code efficiency only if there is a problem. Moore's law will make your code run twice as fast in a few years, but it won't help at all with correctness or clarity.

      If only... Unless your code can make use of multiple cores, its not any faster than on a 2004ish machine :(.

      We ought to be up near 15Ghz by now, and where's my flying car?

        My fairly new machine is much faster than my 2004 rig, even running on one thread. More megaHz, more pipelining, 64-bit OS, and faster RAM. Not to mention faster HD, being a SSD, and more RAM so less swapping in the first place.

        Gotta be careful though. My first attempt melted down when I ran Prime95 at 4.2GHz. Lousy "open-box" deal... but that's why we test, right?

        I know per-thread it's faster than stuff put together in 2007 and 2008, which is my direct comparison. And that was a lot faster than what came before it.

Re: Doing perl efficiently
by AR (Friar) on May 18, 2011 at 11:33 UTC
    Are you asking about efficiency in your algorithms or choice of syntax? Do you want to write code the "right" way with respect to common idioms, brevity, easy maintainability? I found myself writing better code by just writing more and more code and comparing it to code written here and in the books.
Re: Doing perl efficiently
by osbosb (Monk) on May 18, 2011 at 15:55 UTC
    I'm in the same boat as you are. I've been learning how to write perl, properly for the past 5 months or so. I'm pretty happy with the progress I've been making, but I can always do better. One thing that has helped me is to keep reading and writing Perl. Once you learn how to solve a problem one way, try to figure out another unique way to solve the same problem. -osb
Re: Doing perl efficiently
by Gulliver (Monk) on May 18, 2011 at 16:14 UTC

    Have a look at the book "Perl Best Practices" and check out the module Perl::Critic which checks your code against the suggestions in the book.

    Perl::Critic let me know I was using a lot of "unless" statements for comparisons where "if" statements would work with the '<' and '>' swapped. It made my program easier for others to follow.

    The non-free version of ActiveState Perl comes with a Perl Critic graphical application or ebook that lists the rules and is good reading. But I haven't been able to find a free version of that.

Re: Doing perl efficiently
by dreadpiratepeter (Priest) on May 18, 2011 at 17:06 UTC

    Can't recommend 'Modern Perl' by chromatic enough. Well written and current. It should have you writing better code right away. And it's free electronically. But after you find it useful (and you will) buy a paper copy to thank and encourage him.

    see http://www.modernperlbooks.com/mt/index.html for more info



    -pete
    "Worry is like a rocking chair. It gives you something to do, but it doesn't get you anywhere."

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2024-04-20 03:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found