Learn Perl in about 2 hours 30 minutes

A bunch of new people started at work and we use a lot of Perl in our department. So I put together some information about Perl and I thought it might be worth sharing so here it is.

This was originally dispensed in the form of a presentation and about a dozen Perl scripts and half a dozen modules, which I've consolidated into one fairly readable file. If you find something that has been omitted from the file, this is probably either because it isn't used very frequently in my department, because it's trivial to look up, or because it just wouldn't fit due to time constraints.

It turns out that two and a half hours is barely enough to rattle through the essentials of Perl. The biggest example of this is use, a critical core feature which requires knowledge of packages, modules (which are different from packages), BEGIN blocks and class methods (i.e. object-oriented Perl) in order to fully explain.

Discussion (83)

2011-07-31 05:39:05 by James:

Looks awesome. Found myself with alot of time on my hands and started learning Perl so this is really timely. Any objections to me printing it off? Personal use only, I just prefer to read off paper than screens. No worries if not, and thanks again for making this available.

2011-07-31 08:31:05 by Daniel:

This is quite a nice beginner's guide -- I wish I'd had it when I first learned this ridiculous, powerful language. The only piece of information I didn't see in there which took me a while to figure out is the $#array syntax returning the last populated index of @array. I rarely find this syntax directly useful, but it is an additional method for dealing with array length that I see used rather often. Considering that it's a difficult syntax to Google and does see use here and there, it probably bears mentioning in a guide fornew perl initiates.

2011-07-31 13:26:26 by qntm:

I have no problem with people printing this out for personal use.

2011-08-01 15:50:26 by pozorvlak:

Much better than most online Perl tutorials (an admittedly low bar...), but there are a few things which I think could be improved. Others are more knowledgeable than me: hopefully they'll correct *my* mistakes below :-) 1) You don't mention CPAN anywhere. The Comprehensive Perl Archive Network is a repository of easily-installable open-source Perl modules that can help you with any imaginable programming task, and one of the best things about Perl. There's a great search engine at https://metacpan.org/, and a nice lightweight CPAN-module installer at http://search.cpan.org/dist/App-cpanminus/lib/App/cpanminus.pm. Or you could use the default installer that came with your Perl distro: type "man cpan" at a command prompt for instructions. 2) In Perl 5.10 or above, you can use the "say" builtin, which is like "print" but it appends an extra newline. Add "use 5.010;" or "use feature say;" to the beginning of your script. 3) Perl does have a block comment syntax, sorta: it's called POD (Plain Old Documentation), and it's very widely used. 4) "Arrays and hashes are collectively known as lists." This is not my understanding, and indeed on p73 of the Camel Book I find the sentence "It bears repeating that a list value is different from an array". Lists are ephemeral constructs that can be *assigned* to arrays or hashes, but which can't themselves be reified. Usually you encounter them as literals. 5) As well as the built-in ones, there are some helpful array-manipulation functions in List::Util (part of your Perl distribution) and List::MoreUtils (available from CPAN). 6) Doing object-orientation by writing your own calls to bless and so on is tedious, error-prone and now strongly deprecated by the community. Use Moose instead (https://metacpan.org/module/Moose, great tutorial/whirlwind tour at http://rjbs.manxome.org/talks/moose/). 7) You can use lexical variables as filehandles, and doing so is encouraged because they have simpler scoping behaviour than bareword filehandles: open my $fh, "<", $filename or die "Can't open $filename: $!"; 8) You can automatically die if any open/system/close/etc call fails, using the "autodie" pragma: use autodie; open my $fh, "<", $filename; # script dies if this call fails Catch errors with Try::Tiny (https://metacpan.org/module/Try::Tiny) or TryCatch (https://metacpan.org/module/TryCatch). 9) If you prefer an object-oriented interface to file operations, have a look at IO::File. 10) Join your local Perl Mongers group! You can find the closest one at http://www.pm.org/. I don't know what I'd do without mine - as well as being great people, they're an incredibly useful technical resource. Some .pm groups (London.pm, for instance) welcome non-locals onto their mailing lists.

2011-08-01 15:53:05 by R:

$x ^ $x evaluates to true if $x is a string and false if $x is a number.

2011-08-01 16:10:02 by pozorvlak:

R: sure, but you probably don't actually care what representation is currently being used for your variable. You're more likely to care if it's "number-like": either a number or a string containing a number. In which case, Regexp::Common (https://metacpan.org/module/Regexp::Common) Is Your Friend.

2011-08-01 22:34:28 by Murray:

Thanks for taking the time do this -- it's a lot of work distilling something like this down. One little suggestion would be a "further reading" or "see also" or "next steps" section. Frankly, I cannot recommend "Modern Perl" enough (I have no connection, other than as a reader of the book) http://www.onyxneon.com/books/modern_perl/index.html It's free (as in beer, but you can also pay for it if you like it), is available DRM free in a variety of formats & well written by a well respected, long time Perl author/contributor. In short, recommended.

2011-08-02 05:14:06 by Jake:

Love the Tom Lehr reference in the list comprehensions section: http://www.youtube.com/watch?v=DYW50F42ss8

2011-08-02 14:34:11 by Yamil:

Hi Sam, you might want to add this to your CSS to make the guide a little more legible: html body p { font-family: 'helvetica neue', sans-serf; font-weight: 200; font-size: 18px; color: #333; line-height: 1.6em; } html, body { font-family: 'helvetica neue', sans-serf; } a { text-decoration: none; color: darkblue; }

2011-08-05 10:07:35 by Raymundo:

Thank you for such a gread document. May I point out just one mistake? --- An array expression evaluated in scalar context returns the length of the array: my $scalar = ("Alpha", "Beta", "Gamma", "Pie"); print $scalar; # "4" --- ("Alpha", "Beta", "Gamma", "Pie") is a list rather than an array. And $scalar comes to have the final element of the list, "Pie", not 4.

2011-08-06 16:33:59 by qntm:

I've read up a little on the difference between lists and arrays (yet another insane "feature" of Perl) and corrected all of the locations where I misused those terms. CPAN and the majority of external modules are not mentioned in the document because we use them quite rarely at work. For example, our Perl code is expected to run in some 60 different hardware/software environments and it is really quite surprising how many otherwise robust and popular Perl modules completely flip their lid on EBCDIC systems. In addition, we have to get legal approval to use external Perl modules and by default our legal department will answer "no" if there's the slightest possibility that we can write the code ourselves or do without the module entirely. The "say" function is not mentioned because I've never seen a single line of code which uses it, so our newcomers don't need to know about it. Ditto POD, autodie, and various other things. I had a very small amount of time to deliver my piece, so the non-essentials were cut. And just for the record: I don't consider myself to be a member of the Perl community, nor is this document aimed at people hoping to become members of the Perl community. This is not evangelism. This is for people who - like me - don't care about Perl beyond what's necessary to do their job, and just want cold, hard facts.

2011-08-08 14:10:33 by davorg:

"CPAN and the majority of external modules are not mentioned in the document because we use them quite rarely at work" You're going to get a really strange perception of Perl then. Modern Perl programming is often a case of plumbing together the right modules. Not taking advantage of that is really going to waste your time. If you're in an environment where you're expected to use Perl without CPAN then I strongly recommend that you upgrade your management, your sysadmins or your job (as appropriate).

2011-08-08 16:42:17 by qntm:

You'd have to be an idiot to quit your job just because you can't use the third-party module you need.

2011-08-15 04:13:39 by RossPresser:

I must not perl. Perl is the mind-killer. Perl is the little-death that brings total obliteration. I will face the perl. I will permit it to pass over me and through me. And when it has gone past I will turn the inner eye to see its path. Where the perl has gone there will be nothing. Only awk will remain. <i>"names can consist only of letters" is total bullshit. My <b>name</b> is Ross Presser, two words.</i>

2011-09-28 14:08:30 by nickj:

Good job. I've needed this sort of thing for so many languages..

2012-02-07 03:55:17 by Ian:

Thanks. A really useful guide.

2012-02-18 20:22:53 by wumpus:

Thank you very much Sam! This has been a very informative introduction to a decently powerful language. You've saved my ass in my undergraduate AI course :)

2012-02-28 05:39:45 by Heikki:

An empty list evaluates to false, too. Please amend.

2012-02-29 23:05:29 by qntm:

The doc currently states, 'A scalar in an if statement evaluates to boolean "false" if and only if it is one of the following: undef, number 0, string "", string "0"'. Later, it's explained what happens when you evaluate a list or an array in scalar context. And finally, the doc elaborates, "if, unless and ?: statements evaluate their conditions in scalar context". So basically, the doc is complete and correct. I'll consider explaining what happens when you put a list or an array in a conditional earlier, but I don't want to overload the reader by trying to explain everything simultaneously. At the time "booleans" are covered, all the reader knows about is scalar variables. Lists and arrays haven't been explained yet, let alone context. Everything needs to be explained in some sort of order, and I stand by the order that's currently there.

2012-03-20 21:51:52 by Jack:

Hey, just wanted to say thanks for such a well written, thorough and entertaining tutorial. Trying to learn Perl for an upcoming client, and this guide is a godsend. Thanks again! Jack

2012-04-11 19:17:36 by drs:

Fantastic tutorial. I have a Perl program I want to do some editing on so I need to get quickly up to speed. Made me appreciate Guido van Rossum even more.

2012-06-11 12:18:41 by Jonathan:

Thanks Sam, really helpful for those of us who only very occasionally read perl programs but have a wide knowledge of programming languages in general.

2012-06-29 16:46:08 by David:

Sam, I just came upon this thanks to perl-tutorial.org. It is excellent! My only gripe so far is that you name the "#" character "hash". This may be true, but I think it could easily confuse readers when, in the very next paragraph, you use the word "hash" to refer to %perl_hash data types. Might you consider changing the first sentence of the last paragraph of the Hello World section to "Comments begin with the # symbol." ?

2012-06-29 18:09:33 by qntm:

Good observation. I'll make that change.

2012-08-05 03:37:26 by S:

Thanks, great guide. I learned more out of this than all the kilos of books on my bookshelf. I'm a typical sysadmin - always jumping between languages - Perl, bash, Python, Ruby, JavaScript, back to Perl. Having this short guide is invaluable :-)

2012-08-24 14:04:44 by steve:

Sam, This is a great article. I have passed the link along to many people... one thing... In the OO section, the sentence A quick example. An example module Animals/Animals.pm containing a class Animals::Animal reads like this: did you mean to have a singular Animal for the Animal class ? see below A quick example. An example module Animals/Animal.pm containing a class Animals::Animal reads like this:

2012-08-26 20:30:56 by qntm:

Fixed in the latest update. Thanks. Don't worry, I'm certain the latest update added more broken things.

2012-08-29 12:07:40 by ktat:

Hello. I emailed to you at Aug. 5th about translating of this document. In the email, I ask you that I can host the translation of this document at http://perldoc.jp/. Is it OK? Thanks.

2012-08-29 15:32:36 by qntm:

Hi there, As a general policy I allow translations, but I prefer to host them myself. I'd be happy to host your Japanese translation here.

2012-08-31 01:56:42 by Ktat:

OK, I'll send the translation when I finish. Thanks.

2012-09-11 13:11:36 by qntm:

Marked as updated with the Japanese translation. Thanks to ktat!

2012-09-14 04:36:50 by Ant:

I've got to agree with dvorag, though I'm no Perl expert. What I remember of Perl is that it's a filthy, filthy language. At its inception, I'm sure it was a game changer, but nowadays I'd prefer to steer clear if it. The only redeeming things I've heard about it comes from the 3rd party modules that seem to fix some of the more inane parts of the language.

2013-01-05 18:49:23 by Boo:

I just started learning PERL because I have some time to spare. When I create an array (@array) and then execute: print @array; I always seem to get the size of the array. However, certain places in the tutorial seem to indicate that this will print the entries of the array. I can only print the array if I use double-quotes: print "@array"; What am I missing? Thanks!

2013-01-05 21:48:19 by Boo:

OK, I figured out my problem. If I have an array (@array) of strings, I can: print @array, "n"; to get all strings in the array printed as a single continuous string. I can also do: print "@arrayn"; to print the individual strings separated by spaces. However, if I concatenate using the "." operator: print "@array" . "n"; I need the double-quotes (") to print the array entries and not the size. This is a little weird but I understand how it works.

2013-01-05 21:52:46 by qntm:

I think your main problem here is that the code examples you're giving are not the ones you're actually running.

2013-01-12 17:26:05 by Michael:

Thanks for this great introduction to Perl. After 2 1/2 hours with this, I've learned that I like the language, and feel comfortable enough with it to get deeper into it.

2013-02-04 17:38:09 by Newman:

Great! Simply great intro tutorial. Thanks a lot for this!

2013-04-18 11:58:37 by sree:

Good Stuff

2013-05-03 19:56:15 by qntm:

Marked as updated with a translation into Korean thanks to Jin Kim!

2013-05-07 06:29:38 by GQ:

I taught myself Perl using this in a matter of weeks. Now I'm quite proficient and I regularly refer to the tutorial during coding. Thanks! I would only suggest expanding on the regular expression section to include the regex operators, such as *,+,$ etc.

2013-05-07 18:54:21 by qntm:

I omitted those deliberately since they are the same everywhere, they aren't Perl-specific.

2013-05-28 18:39:39 by FelipeViana:

Hello Sam, I really liked your tutorial. I just have a minor consideration. Maybe you could include a small sentence at the begining stating that the language expressions end with semicolon. It is obvious but maybe you would like to know that this small detail is missing. Thanks.

2013-07-11 08:58:51 by FredBueller:

Cool beans! Even a Spanish edition? Perfect!

2013-07-15 13:20:03 by naderghanbari:

A great tutorial, read thoroughly in exactly 2 hours 30 minutes. Ready to translate to German, French, and/or Persian.

2013-08-30 20:47:55 by Bills:

What do you need as a background to use this guide and at what level programmer do you need to be before undertaking such a task?

2014-02-16 04:14:39 by madhavksm:

neat summary for quick reading, thanks heaps.

2014-04-03 16:48:55 by Zubair:

Probably the best Perl tutorial I've come across. Lots of muscle and little fat.

2014-04-08 02:08:25 by MB:

This site was a great primer to Perl. I havent touched perl in a long time, and frankly if you dont program in the languages (like i havent in a while), you tend to forget. Perl is necessary for a lot of the things I want to do, and the quick primer got me up to speed (funny thing I can read perl code pretty well). The other thing I love about this site, I can now use php / python (www.codeacademy.com) tutorials to build what I am building for the next world domination!

2014-04-20 19:12:02 by Chankey Pathak:

I read this every time I need to give an interview.

2014-05-11 22:09:25 by mw:

Thank you. I finally understood the difference between Arrays and Lists. Very good tutorial. After hacking perl for a while I still found a few useful things in it. Thanks for the write-up!

2014-07-08 18:22:14 by msm:

Thank you so much for sharing. I printed this out and put it in my reference library when I absolutely have to use Perl. I loved the humor!

2014-09-09 23:27:01 by Dorothy:

Very useful. I wish I had seen this earlier, then I would get a higher letter grade in my verification course... Thank you Sam

2014-09-12 21:44:36 by Ryan:

Thank you so much for putting the effort into writing and maintaining this. In the interest of both confirming my understanding and contributing back, I wanted to mention what I believe to be an error in the "Perl calls by reference" section. >>>because the first line of reassign() is equivalent to >>> >>>8 = 7; >>>which is obviously nonsense. I believe the third line should be: >>>8 = 42;

2014-10-22 18:41:07 by Mike Christie:

I use perl once a year or so and always come back to this page for reference. It's easily the fastest way for me to find and code what I need. Now I'm back here for the fourth or fifth time, and I thought it was time to say thank you. This is a great resource for people who need to write code now and don't care about perl beyond that. Thank you for making this available.

2015-01-12 19:00:37 by ruffin:

"In many examples, this results in alotofwordsallsmusheduptogetherononeline if the code is run in reality. Try to ignore this." Might be worth mentioning the `-l` option for running "perl" on the command line, which nicely inserts newlines for each `print` and isn't contingent on a fairly new version of Perl like `say` would be. That's helped me avoid "alotofwordsallsmusheduptogetherononeline" while trolling through the guide. -l and some other cmd line options are discussed in detail here: http://www.perl.com/pub/2004/08/09/commandline.html I'm new to Perl, and other than the sneakily unannounced initial `$#array;` usage, the intro is great. Thanks! (Also enjoyed the comment on php, and David Mitchell's on Perl and God.)

2015-02-26 15:35:09 by TonioGA:

This summary is a great introduction to perl. Thanks a lot!!

2015-03-17 23:15:25 by Akore:

Very helpful for beginners.. Thank you so much..

2015-04-09 03:26:36 by southkid23:

Awesome guide!

2015-04-24 08:56:25 by Sunil Raz:

Very Good!!

2015-06-02 10:23:53 by Markus:

Great job! You saved me a lot of time. Thank you.

2015-08-09 16:35:41 by Rob:

Great guide. Thank you for creating and maintaining it.

2015-08-31 18:23:05 by Basavaraj Patil:

Thank you very much.

2015-10-18 03:43:46 by Perl Machina:

Amazing introduction into amazing language. Thank you, sir!

2015-11-13 13:25:25 by Thiago Ganzarolli:

This saved me a lot of time. I have to learn Perl now, but since I've seen a lot of languages before, strong or weak typed, the Llama book and other tutorials felt boring. They take a long time before spilling out something actually useful. I needed a drive-into crash course. This is just what I needed to get started understanding code and producing code. Thank you so much.

2015-11-19 21:16:19 by Jerry:

I haven't gotten through the whole thing, but this is an awesome tutorial. May I suggest a table of contents, basically a list of links to some of the major sections?

2015-11-25 08:46:58 by Leonard:

This tutorial is amazing. As a computer science and engineering major in my sophomore year in college with no prior perl experience and an internship lined up for next summer that requires writing perl, this was the best find for me on the internet since... well, I'm not filling that part out. Thank you very much, guide writer. Also, with no knowledge of regex besides the wise words of "avoid it like a crazy ex with a gun license", I'd like more information on that. Again, thank you so much

2015-11-25 09:18:57 by Scared:

@MB What would possibly use php, python, and perl? A program promoting the production of propellers for planes, paid for by the Porpoise Posse?

2016-03-24 08:38:40 by Josefine:

Thanks a lot for this awesome introduction!

2016-06-17 23:57:53 by Alex:

This is very helpful!

2016-10-05 10:49:43 by Leo:


2017-01-13 20:35:25 by Ben:

Thanks Sam Hughes! I am a beginner to programming and Perl as of yesterday. I found out that ."\n" acts very similar to , "\n" in denoting my code. Could someone please explain the following? my $array3Ref = [1, 2, 3, 4, 5]; print @{ $array3Ref }, "\n"; # "12345" print @{ $array3Ref }."\n"; # "5" #Why are these printed values different?

2017-01-13 20:41:30 by qntm:

. is the string concatenation operator. It's evaluating @{ $array3Ref } in scalar context, which returns the length of the array, 5, and then coercing it to a string, "5".

2017-03-14 17:40:59 by Nisaea:

Brilliant. Now I remember why I hated Perl. :'D But thank you so much, this is the document I needed. Badly.

2017-04-03 02:10:30 by Jonathan:

Always EXTREMELY useful when I wanna recall perl and write short scripts

2017-08-08 22:19:39 by Ophiucus:

As a sysadmin, I've had a travail to write shell scriplets to automate various tasks. Perl didn't become important until script file management overhead became too inefficient. Not being enthused to put time in to learn from a programmers perspective, I can say thank you for such a more explicit primer. Now I can knock things out ;)

2018-01-22 21:03:20 by toa:

I've almost finished translating the document to Spanish and as of now, I'm doing some minor revisions to it. I emailed you but haven't got a response yet. Should I email it to you again?

2018-01-23 15:44:44 by qntm:

Hi, yes, I got your email. Sorry, I've been in the middle of something. I'll email you back now.

2019-03-07 13:11:32 by shaitand:

Thanks for the tutorial. As a long time programmer it is nice dense reference. It does read a little like Perl for Python programmers though. One thing I find odd is the extensive use of lists throughout. List is highly useful as a context and that is almost entirely how you will see the term used by Perl programmers. Normally you'd use anonymous array or hash syntax and not lists.

2019-11-13 20:12:11 by darren:

Wow, this is simply the best most straight forward breakdown of Perl. No time or confusion wasted talking about edge cases, gets straight to the point. Thanks for putting this guide together!

2021-04-03 11:46:26 by tabuk:

Simply Perl. (with no bullshit) Thanks !

2021-07-31 00:33:38 by Mark Gardner:

‪It’s a great intro. What are the rights associated with it? Could I make my own version (with attribution to the original author), for example, that does use `say` and does acknowledge CPAN?‬

2023-01-11 16:31:08 by Marcont:

Excellent introduction to Perl. I have not seen such a practical and concise way to get started with examples from the simplest to the most complex.

New comment by :

Plain text only. Line breaks become <br/>
The square root of minus one: