How to teach Perl to children!?

How would you go about teaching Perl (or any language) to children?

Here is my plan for my own offspring. The main points of this plan are: taking it slow, and: taking baby steps, and trying to show results at each step giving positive feedback to the child..

Before I get into the plan, I want to point out that this intended for the teacher to already be a programmer, or at least be able to learn on their own, since a lot of the teaching will be individual in this case.

  1. Output.

    Start with printing simple Hello world. If you cannot see what the program did, then it isn't any fun at all to do.

  2. \Variables.

    Start simple, Teach how to store a number into $x, then how to store a string, and printing them back to the screen. Explain that when Perl 5 sees a '$' it expects 'something it can print' (a scalar). This will help when later working with arrays and hashes.

    This first part of this step should include arithmetic so that the child can actually have something useful with his or her program, even if it is a big complicated calculator.

    Move along to hashes. explain it as a bucket where you say something is something else like John is tall. Point out that if you say that John is skinny, that you lose the tall, because John can only be one thing at time.

    Then do arrays. I saved this one  for last, because of the possible confusions with the zero based arrays in Perl and most other languages. I like to describe the index, as "move X spots from the beginning"

    For each type of variable make sure that the child can put data in, and get it back out, and print it to the screen again.

    This might be a good time to point out the difference between array context and scalar context, or you might want to wait till later.

  3. Now you might want to teach about use strict;
    my favorite example is
    $ZipCode = '12345';
    print $ZIPCode;
    If you want to separate it with a lot of code inbetween that will help make the point.
    then turn on strict, and show how the compiler complains immediately, making it a lot easier.
  4. Loops.

    Start with a foreach loop, since it is the simplest construct. I suggest starting with a local variable to avoid $_: (you should always avoid $_)
    foreach my $item (@array){

    next cover a while loop, since it has simple syntax, and move on to the for loop

  5. Special variables

    since they are going to need to know, teach about $_,@ARGV @ENV and %ENV.
    Show them where to learn about the other special variables and what they do.
  6. Branching constructs

    teach about if/else blocks. do not encourage use of the postfix notations:
    $x = 3 if $x==2;
    rant: Damien Conway is correct about the postfix notation being a bad idea. The idea of a thousand lines of code not being executed because of the last line, seems like a bad way to go. /rant

    Instead of the postfix if construct, teach $x = $x==2 ? $x=3 : $x;
    It does the same thing, and avoids some potentially bad behavior.

  7. subroutines.
    create a subroutine, take in some inputs, use @_ as well as shift;
    do something with the data, and then return something.

    This might be a good time to point out the difference between array context and scalar context, by returning an array from the function.
  8. loading libraries, using someone elses code.

    Show off why Perl is so great! up till now they haven't learned anything that another language couldn't have offered. Show the the CPAN. Show the perl core libraries. show how to use perldoc, show how to really start having fun with Perl.
Advanced topics that I will cover at a later date

  1. create a cgi script
  2. using databases
  3. creating a package of your own
  4. hashes with several layers of internals (leading towards objects)
  5. use bless to make an oo object.
I will start to teach my nine year old daughter Perl using these steps. Here's hoping she is interested and that I can keep her interested... I will post my progress as it goes along


I had a similar question some time ago, and got some good advice here:

(but my plan was not to actually teach one kid programming, but to introduce a group of kids to basic programming concepts (which is "computers are dumb!")


Oh, and your first project should definitly be some kind of game. Either text-based, but I guess something with graphics is cooler. And it's not that hard with SDL (, see

My son who is now 11 has just written his first perl 6 program using my favorite IDE :-) but I don't think he will continue doing so as he finds out that it is not that visually attractive as he might expect.

My understanding is that because of the tons of visual content and the games they see on the next at that age they still need some very strong visual feedback.

Earlier he has been using Scratch and in a discussion Su-Shee just pointed me at Teach a Kid to Program that has an interesting list of tools with nice visual effects that can help learning programming in a safer environment than Perl or any other programming language we use.

At some point pretty early on you might want to introduce interaction with a program. Have them input their name and have the program greet them by name. Have the program display a menu of choices and let the user enter their choice.

And of course, if your daughter thinks of something fun she'd like to do, try to go through the steps of designing it, and you can introduce any additional components as they're required.

One of the reasons I really got into programming way back when was having fun creating interactive programs that did al sorts of ridiculous things my 10 year old brain could think of :)

I would change the order of that personally, but just because I think that basics need to be taught before relatively complex things like data structures.

I'd do scalars, then basic control flow (if else and some basic looping), then some data structures, and then subroutines.

I'd teach in a problem solving style. Like once you get ridiculously complex stuff because they don't know a better way (imagine a game with if elses that needs subroutines and loops) and then show them how.

But that may just be how I learn well, not others.

(hm, I posted this yesterday, but it seems to got lost..)

Some years ago I had a similar question and got quite good feedback here:

Additionally, I find this site very interesting:

Frankly, your description sounds even less interesting than Piano lessons.

There's a talk from Nate on his experiences teaching children that you might find interesting.

About this Entry

This page contains a single entry by leonard published on August 14, 2009 8:39 AM.

Why I like being a programmer! was the previous entry in this blog.

don't be a n00b is the next entry in this blog.

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