Perl tutorial for beginning programmers.

Given the sheer number of useful Perl scripts available for Linux-based systems, knowing the basics of the language is a definite plus for anyone running their own server. I use Perl for tasks ranging from system scripting to web development and database stored procedures (MySQL and Postgresql are cool like that).

While it's not intended to serve as more than a true beginner's guide, some may find my tutorial useful: ~~[http://www.classhelper.org/articles/perltutorial/learnperlintroduction.shtml" target="blank">](http://www.classhelper.org/articles/per … tion.shtml">http://www.classhelper.org/articles/perltutorial/learnperl_introduction.shtml](

It assumes no prior knowledge of programming, and provides real-world(ish) examples for each chapter. Hope you find it useful :).

10 Replies

I spent many years with Perl, but now PHP is my server-side language of choice. These days I think many folks would actually be learning JavaScript or another client-side language first! What I've always liked about PHP is its much easier syntax for complex data structures (and arrays in general). I don't have to keep referring to the Perl cookbook :)

I've also been writing quite a few bash scripts, mostly to provision and customize linodes. Most recently, as part of switching to Ubuntu from CentOS, I learned more about bashisms (because I had to write some init scripts, which are run with dash).

Looking very briefly at your tutorial, I noticed a few small things. Sorry my Perl is a bit rusty, though–

1. Love the -w and use strict!

2. You build an array with newlines appended to each part, which seemed a little odd.:````
@grocerylist = ("apples\n", "oranges\n", "milk\n", "bread\n"); print @grocerylist;

If you omitted the newlines, you could do things like:

print "@grocery list";
print join("\n", @grocery_list) . "\n";

````
3. The first loop you show is with for, I think foreach is simpler for most applications (since this is a beginners guide). You could even refer back to your grocery list to show how you could introduce some formatting.

4. You simply must introduce sanitizing user input when you get into the "dynamic content" section (you do mention it briefly). For example, If you expect numbers for input, remove everything else. We don't need more CGIs with injection and cross-site scripting problems.

This is a good time to talk about -T (taint) to identify such unsafe inputs. I wouldn't write any Perl-based Web interface without it.

I would also link people to a page like http://www.owasp.org/index.php/Unvalidated_Input for details.

8)

With respect to #2, I debated this for awhile. Wanting to keep the syntax as straightforward as possible for someone who's never seen code before, I opted out of "join" in that section. However, there's no reason I can't introduce it as an alternative.

I'll do the same with the "for" versus "foreach" loop structure, which is another good demonstration of the "more than one way to do it" philosophy. I started with "for" more out of deference to the first language I learned as a kid, which was C. The point about introducing some different formatting is a great one.

You're right that I should introduce taint mode and more rigorous input validation. While my aim is to make things as easy as possible for a raw beginner, there is definitely such a thing as knowing just enough to be dangerous. A couple of explanatory paragraphs with some practical demonstrations of these concepts might save someone from a nasty situation. While the security chapter does contain some links to pages that cover taint mode, I'll be adding your link to the list.

Thanks again for the great feedback :).

PHP is very nice but I'm starting to learn Ruby.

Jeff

I love PHP and Perl both. I think they have their own strengths and weaknesses. I use a lot of Perl at work to do stuff (along with Bash) since I do a lot of SA type work. But, for fun, I like to write PHP code (http://github.com/ultramookie).

I have a Ruby book and need to start reading up on Ruby. I hear it is quite nice.

@ultramookie:

I love PHP and Perl both. I think they have their own strengths and weaknesses. I use a lot of Perl at work to do stuff (along with Bash) since I do a lot of SA type work. But, for fun, I like to write PHP code (http://github.com/ultramookie).

I have a Ruby book and need to start reading up on Ruby. I hear it is quite nice.

I haven't done anything with Ruby, and I really ought to. I've heard it supports Lisp-y language features, which brought back memories on a friend's dad explaining SmallTalk programming to be as a kid.

@ClassHelper:

With respect to #2, I debated this for awhile. Wanting to keep the syntax as straightforward as possible for someone who's never seen code before, I opted out of "join" in that section. However, there's no reason I can't introduce it as an alternative.

I think you should hold off on arrays at that point. You've introduced a scalar, so now you could introduce the use of scalars in a foreach loop, such as

my $loopvar;
foreach $loopvar (1..10)
{
  print "We are $loopvar times around the loop\n";
}

THEN you can introduce arrays, and show the printout as "foreach $loopvar (@array)". This maintains consistency with the \n in each print statement. And then you can show the join() syntax as a shortcut.

Your "print @array" is actually quite a powerful feature (shows implicit type conversion) and not something to introduce as a throw-away.

Yet another item on my list of stuff to add to the tutorial :). God, I'm gonna wind up with something that looks like an O'Reilly book within a matter of months :).

All these Perl flashbacks! 8)

I see 1..10 (as a beginner) and wonder, what is that?

I think someone is more likely to want to iterate over an array since that seems logical, or print the value of $i++ in any loop, than to use sweh's foreach/range example.

What do you think the first built-in function is that someone would use? chomp? chop? split? join?

Perhaps I missed it, but CPAN could deserve a mention in the resources section.

Ruby is where it's at, it banishes those awful memories of writing PHP, yuck.

What kind of scripting are we talking about here?

Web site scripting? or system administration tasks? or…?

Perl, PHP, Python, Ruby, etc, all have varying purposes.

Perl is what I mostly use for quick sys admin type tasks.

PHP is what I use when I need a quick web script to run something.

My larger websites are built using Python and Django.

Bash is shell scripting… very useful for certain tasks.

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct