How to Learn Perl

| 3 Comments

You've decided to learn Perl. You're excited. You have problems to solve, and you know that a little bit of learning and some hard work will help you convince your computer to do things for you. You've started an important process, one that will benefit you greatly if you stick with it.

Your good intentions are the first step. The fun begins now.

The Basics

If you already know a little bit about programming—if you understand the difference between values and variables, how if and else work, how to call functions and deal with their return values, and most important how to use a text editor and save a program and run it on your operating system of choice, you're way ahead of most people.

If you're not sure if you know all of that, or if the meaning of those words weren't immediately obvious to you, you need to learn those first. Fortunately, learn.perl.org has some great and modern tutorials which cover those basics. Eric Wilhelm's Learn Perl tutorials are also useful. The Perl Begin site has copious resources, from advocacy to practical Perl programming advice.

That'll get you set up to write and to run Perl 5 programs. Now you need something to help you figure out the absolute basics of programming. Beginning Perl is a free online book which explains those basics. It's a bit old, but the basics haven't changed. When you're comfortable understanding what variables and values and functions are, you're ready to move on.

Learning Perl, sixth edition costs money, but previous editions are cheaper. It also covers the basics of Perl 5 programming. If you've never programmed before, you're the target audience. If you've programmed a little bit in another language, it may be too basic for you.

Writing Good Perl

If you're comfortable with the basics of programming, Perl 5 or otherwise, my book Modern Perl is available for free download (though you can always buy Modern Perl online if you're so inclined). While the book does offer a review of basic programming concepts such as variables, functions, and control flow, I took pains to explain these ideas in terms of Perl 5's philosophy. By the end of the book, I plan for you to understand how Perl works so that you can take advantage of its strengths and avoid its weaknesses.

At this point, many people recommend reading Programming Perl, affectionately known as the Camel. I don't recommend it myself; the third edition is a decade old and Perl 5 has changed in some important ways since then. A new edition should come out in late 2011 and may be a better resource. (I've read both the second and third editions in their entirety, start to finish, and that helped me as a novice Perl programmer in 1999 and again in 2000, but the books serve better as reference material than tutorial).

The best way to become a good programmer is to write code and get feedback on that code.

Testing

One way of getting immediate feedback on your code is to develop the discipline of rigorous testing. It's not nearly as difficult as it sounds. It's actually even fun, if you do it right.

Modern Perl uses Test::More throughout its examples and includes a section on writing tests. You can also read Test::Tutorial for a thorough introduction to the ideas of testing, from the simplest point of view to the bread-and-butter approach most Perl 5 developers use.

Testing is as much art as is designing software, and as such only experience will help you find what works best. Even so, I like to follow a simple process that lets me build up a lot of good tests in a repeatable way:

  • Before you add a feature to a piece of code, as yourself "What should this do?" and "How will I know when it does it?"
  • Based on that, give this behavior a name. This is probably going to be a function or a method.
  • Figure out the one thing that named code should do and think about how you'll know if it does it.
  • Write a test for that one thing.
  • Ask yourself if anything can go wrong, and write tests for those things too.
  • Make sure the tests pass.
  • Clean up any duplication or confusing things.
  • Repeat.

That list doesn't dictate when you write the code to make the tests pass. I have a lot of success writing a test or two before I write code to make them pass. Other people swear by writing a line of code and then a test. Try both approaches.

The nice part about this process is that if you don't really know what to expect from a line of code or a Perl 5 construct, you can write a test about your expectations and see if you can make it pass. In other words, use this testing framework to teach yourself how Perl works.

Learning from Others

You'll have questions and you'll need answers.

I've been a member of PerlMonks from its first day as a public site. (I was the second person to sign up.) I've learned as much about Perl programming from the site as from anywhere else. If you join and read questions and answers and take the time to do your research before asking questions, you'll get a lot from the site.

The Perl Beginners mailing list is a community dedicated to helping novices learn Perl in a friendly and non-judgmental way. Again it's worth lurking for a bit to figure out the community guidelines before you post, but asking a good question on here will get you good answers.

If you prefer real-time interaction, the #perl-help channel on irc.freenode.net has friendly people who are willing to help novices willing to ask good questions and take advice well. Lurk first to see how the community works.

Perl Mongers user groups meet regularly to discuss Perl, computing, and technology, and they offer opportunities to meet and socialize with other people learning how to write great code. If there's a PM group in your area, go! If there's no PM group in your area, perhaps you can start one.

Writing Great Perl

Writing great Perl 5 means taking advantage of the CPAN. Even if you don't contribute to the world's largest library of free software components, structuring your code in the CPAN style offers tremendous benefits. Again, Eric Wilhelm's How to Use Perl Modules and CPAN and Perl Configuration Howto are great resources.

Yet writing great Perl means solving real problems. Here you need a project and some way of measuring the progress toward your goal. This is for you to discover.

My first Perl program slurped lottery data from a public site, analyzed it, and plotted the frequency of appearance of each possible number of the results over time. (I don't play the lottery, but I found the statistics interesting.) That program still runs today (automated, twice a week), even though I may go years without looking at it.

Find something that interests you. Find a way to automate it. Keep a list of changes or improvements or new techniques you might apply. Write down what you think about when you're commuting or walking or falling asleep or bathing. When you can't get it out of your head, break it into small pieces, test and experiment, and see what happens.

Programming well requires knowledge, certainly, but like anything else it requires passion to keep you practicing in a disciplined way. The resources I've mentioned here can give you knowledge and will help you develop your discipline. (They're not the only resources, but I believe they're great resources.) What's left is up to you.

You've taken an important step in starting to program Perl. It's rewarding and enjoyable, and you'll be able to do amazing things. Keep your focus, but keep your eye on your goals. You have a lot to be proud of, and you're just getting started.

3 Comments

There are many more resources on Perl-Begin - the Perl Beginners' Site or referred to from there. Many people and I think Perl-Begin is a great resource, but, unfortunately, it has been subjected to lack of acceptance by the core Perl leaders, and sometimes I even received some negative criticism of it.

You're right, Shlomi. I've updated the article to link to Perl-Begin. I apologize for the oversight.

Modern Perl: The Book

cover image for Modern Perl: the book

The best Perl Programmers read Modern Perl: The Book.

sponsored by the How to Make a Smoothie guide

Categories

Pages

About this Entry

This page contains a single entry by chromatic published on September 28, 2011 11:08 AM.

Always TDD, Except When You Shouldn't was the previous entry in this blog.

When Forking is Not an Act of Love is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.


Powered by the Perl programming language

what is programming?