views:

718

answers:

9

I've started learning Perl but most of my former programming experience has been in languages that emphasize object oriented programming such as C# and Java. All the Perl examples I have found tend to be long single function programs and I find my self writing code the same way. Are there any resources or tutorials for writing maintainable well structured programs?

+10  A: 

If you're learning OO Perl you might as well move on to the next big thing and start learning Moose.

Moose is a postmodern object system for Perl 5 that takes the tedium out of writing object-oriented Perl. It borrows all the best features from Perl 6, CLOS (Lisp), Smalltalk, Java, BETA, OCaml, Ruby and more, while still keeping true to its Perl 5 roots.

Moose is 100% production ready and in heavy use in a number of systems and growing every day.

melaos
postmodern? I prefer something from the impressionist school.
Doug T.
+12  A: 

Perl Best Practices

Fhoxh
A: 

Write Perl the way you would write C, although you should take advantage of some things BEGIN blocks and modules. It should be formatted so that it looks more or less like old style K&R C code.

As for structure, skip the OO stuff and style it as ADTs instead:

http://en.wikipedia.org/wiki/Abstract_data_type

Basically you do this by using data-structures where you would have used objects in OO (and separating out the algorithms into their own functions).

Then, pick only one way to do things and stick to it, consistency is vital to readability.

If you manage that, the code can be both readable and easy to understand.

Paul.

Paul W Homer
No. No no no. Perl is not C and should not be written like C. C style for loops are a good example; just because you can does not mean that you should.
Mr. Muskrat
I know, it sounds horrible, but it works wonders. For me, the secret to building large maintainable systems in Perl was self-discipline: push a dense messy language onto a simpler structure, using only what you need, not what's available.
Paul W Homer
+20  A: 

Firstly, regardless of what sort of Perl programming you're doing, you'll probably find Perl::Critic to be invaluable. The command-line tool is by far the most convenient for getting feedback on your code, but you there is also a web interface where you can upload your Perl code and receive instant, automated feedback. Note that Perl::Critic isn't going to teach you good structure, but it will help improve your style in general, and steer you away from some common mistakes.

To go with Perl::Critic, I'd recommend getting a copy of Perl Best Practices (PBP). It contains a lot of detailed information upon which Perl::Critic was based. Even if you disagree with particular guidelines in the book, it makes you think about how you code, and that's very, very valuable. You don't have to shell out money for a book, but the two compliment each other very nicely, and there are lengthy discussions you'll find in PBP that you won't get from Perl::Critic.

If you've already worked with other OO languages and OO design, the you'll probably find Moose to be a comfortable transition. Moose is very stable, very well supported, and has a huge and active community (especially via IRC). Moose supersedes almost all the existing OO Perl advice out there, including my own. Object Oriented design is common for a reason; it makes sense for a lot of projects, and there's no reason not to use it in Perl.

Personally, I found a massive improvement in my own program structure when I moved to a test-driven development model. Under such a model, breaking a problem down into small, easily tested units is essential. Start with Test::Tutorial if you're new to testing in Perl and then look at some other testing resources or books if you want to learn more. Use a tool like Devel::Cover or Devel::NYTProf to see what your test cases are hitting and what they're not. Having code that's hard to test is often a sign of poor structure.

Having said all this, the best teacher by far is to get involved with an existing Perl project with experienced contributors. See how they do things, and when you make contributions, think about their advice. If you want a real application with awesome cool value that seems to have sucked up the very best and brightest of the Perl community, then I'd recommend getting involved with Padre, the Perl editor.

If (for whatever reason) you can't get involved with another project, then consider posting code samples to communities such as Stack Overflow, or PerlMonks. Better still, if you can make your code open source, then do so, and solicit feedback. All programming languages are better learnt with others who are already familiar with them, and Perl is no exception here.

May you do Good Magic with Perl,

Paul

pjf
+2  A: 

Personally, I didn't write good OO Perl until I started learning Java. I recommend you spend a small amount of time learning OO in a true OO language and apply what you learn to Perl.

For a place to be mentored, PerlMonks is GREAT.

Chris Nava
+6  A: 

Apart from the sources mentioned above I strongly recommend the book "Higher-Order Perl" by Mark Jason Dominus. If you can afford it buy it, otherwise the author has put the book on his website for free as in free beer, Free Higher Order Perl. For the PDF version: direct download URL (1.9 MB).

Edit:

I forgot to mention Perl Design Patterns. The PerlDesignPatterns site is a very good resource or reference for learning Perl design patterns.

systemsfault
Perl Design Patterns is a good recommendation, but Higher Order Perl (ie. functional programming in Perl) is probably more than the OP is looking for given his OO background and desire to learn about "maintainable well structured programs". While I think HOP is great, I wouldn't recommend it here
xdg
+6  A: 

If you feel you already are learning the basics and want to improve program structure, then I'd suggest these books, probably best to read in this order:

  • Intermediate Perl -- this is probably the place to start for what you're asking about; covers developing and using Perl modules (essential for good structure), references, complex data structures, OO, and more

  • Perl Medic: Transforming Legacy Code -- demonstrates well-structured Perl by showing how to fix poorly-structured Perl

  • Effective Perl Programming: Writing Better Programs with Perl -- a classic, but still useful; full of Perl idioms that will help you write more Perl-ish Perl; not much on OO, but covers more fundamental coding structures to make your code more succinct and yet more expressive. I think of this as the book that got me from Perl literacy to Perl fluency

All three are best once you've comfortable with basic Perl syntax, but none of them get too far into esoteric advanced concepts that you can't follow if you're still starting out in Perl but have reasonable programming experience in other languages already.

xdg
+5  A: 

If you already know how to write well-structured programs and you want to write well-structured Perl, but the only impediment to that is the abundance of ancient, crufty, spaghetti-code examples that are scattered around the net and refuse to die...

Ignore the examples. Or at least ignore their poor structure and only pay attention to the small syntactic bits that you need to learn from.

There is nothing in Perl that pushes you to write bad code. If you already know how to write good code in other languages, then you can apply the exact same principles and practices in Perl. It's merely a matter of discipline, just like in any other language.

Dave Sherohman