Caleb Cushing's Blog

Teaching Perl - Week 1 - ( Part2 )

Padre - Perl IDE

Now let’s install Padre the Perl IDE to give the students a nice development environment. There are instructions on the website, however, cpanp -i Padre should get the trick done since it is on CPAN In the future Strawberry will ship with Padre but that future isn’t quite here.

First Program

Ok so we want to make sure that we have Perl installed correctly and since you’re taking my advice I’m assuming that you’ve given your students access to 5.10.1 and may have 5.8 as well… So I’m gonna do this one twice. Have them create a text file hello_world.pl and add the following lines

#!/usr/bin/env perl

print “Hello, World\n”;

Since I’m sure this is not your students first programming class this will be familliar to them. Explain the shebang (#!) line and note that it has no effect on windows. Now let’s modernize it, using the say feature from perl 5.10.

#!/usr/bin/env perl
use feature ‘say’;

say “Hello, World”;
note the removal of the \n and that use feature is a pragma that allows you to enable features that are newer in perl that weren’t available in the original perl 5. Perl is a bit backwards about this, IMO. We should be using the newest version and features unless we specify otherwise, IMO, but that’s not how it works, ATM. If 5.10 isn’t available to you and your students just use print in future examples. I will be excluding the shebang line from future examples too.

Now let’s expand hello world a bit. Have them Make it print

Hello, programming!
Here we go!
First thing is to make use of concatenation

use feature ‘say’;

say “Hello, programming”
. “Here we go”;
This example is inspired by Programming: Principles and Practice Using C++ ( This is an excellent book and should be the textbook for BOTH C++ classes at Baker. I recommend all novice to intermediate programmers have a copy whether you like C++, or not, this book is excellent ). Please note that the . operator is in the front. This draws a programmers attention to it and notes that it’s a continuation. See Perl Best Practices for more details on this.

Your output will be.

Hello, programming Here we go

That’s not what we want! obviously say appends a newline to the end but doesn’t help with long strings.

use feature ‘say’;

say “Hello, programming\n”
. “Here we go”;
Much Better.

Simple Game

Now let’s up the ante a bit. Again not there first programming class, the following example is rewritten in Perl from Head First Programming: A Learner's Guide to Programming Using the Python Language which arguably should be the textbook for Intro to Programming. This is the first example that book gives in chapter 1 (but it gives it in python).
note: you can use < STDIN > but blogger screws it up because of the brackets. This code works exactly the same.

So now you can discuss: scalars, if then else statements,and readline. There are also two additional pragma’s strict and warnings that you should tell your students to always use unless they have a ‘good’ reason to disable them (these have been there since the beginning).

So usually on the first night of class you’re lucky to get past hello world. I think this is all doable. In 1 small program I just took you all the way to Chapter 6 in Perl by Example (3rd Edition) the chapter on control structures. Remember, this is not your students first class… you don’t need to hold their hand through all this. Race through it so they can improve on it as the term goes on. Instead of spending 4 weeks (assuming a chapter a week) on if then statements you can now spend 10

Homework

Start a blog using whatever site you prefer (preexisting blogs acceptable) and add it to Iron Man. Do a first post that talks a little about you (blog can be anonymous). Include a copy of the simple game and what you think would be better about it. Also mention anything you’d like to do with, or know about, Perl.


Share

comments powered by Disqus