Clicky

Perl


Last updated: May 22, 2013

If you are not interested in this introduction you can jump directly to the Table of Contents. I began using Perl almost 15 years ago. I wanted to learn because a coworker of mine wrote a helpdesk program in Perl and I thought it was the greatest thing ever. I was fairly new to computers and programming languages in general and it seemed like black magic to me. My first few times of trying to learn I quit in frustration telling myself I just was not intelligent enough to learn programming. When I started my first job out of the Air Force, one of my first tasks was to do something with the firewall logs, which no one was even bothering to look at. I knew Perl could help me with this so I again attempted to learn it, but again given up in frustration another one or two times. It was not until I found the site PerlMonks where I was able to ask questions which stumped me and received almost an immediate response. As I used Perl more and more I began liking programming and decided to get my bachelors in Computer Science which was 75% programming where I also learned C, C++, Java (although I have forgotten most of what I learned in these langauages). I have written many Perl scripts and programs (calling them scripts vs programs is a sensitive topic for some in the Perl community. Personally I generally refer to mine as scripts, but I do agree it sort of degrades the language into being pigeoned holed into a scripting language when it is so much more then that).

Here are some other reasons I like Perl:
  1. CPAN - this is a repository of Perl modules others have written and are sharing for all to use. There are modules for just about everything. Currently (09Nov13) it states there are 127,189 modules. Yes, other languages also have their equilvant (ie. Python has pip) but I have not found them to be as comprehensive nor as easy to install and use. I am sure this will change as the languages mature and age.

  2. Documentation - the built in documentation is excellent. You pretty much could learn the entire language just by reading it.

  3. Regular Expression (regex) Engine - it is so good many people associate regex with Perl.

  4. Perl Community - sure the community is dwindling, but I still find it easier to find Perl help then any other language.

The best thing to happen to Perl recently was the book Perl Best Practices which shows how to write clean Perl code. I agree with the common sentiment that a lot of Perl code out there is unintelligible line noise. There is a lot of Perl code I can not decipher and I have been using it for almost 15 years now. Personally I feel it is the person writing the code that is the problem, not the code itself, but Perl does make it easy to write bad code whereas Python makes it difficult to write bad code. Regular expressions

Perl 6 is around the corner (it has been for almost 10 years now), but I do not know much about it and I'm not too motivated to learn it (although I like to dabble in programming languages so I'm sure I will mess around with it eventually). I read someone say Python is what Perl 6 should have been and I agree. I have since learned Python, really to see what all of the fuss was about and being a bit embrassed whenever I would tell someone I only knew how to program in one programming language (aka a one trick pony), Perl. I like Python and it will probably be my language of choice for the foreseeable future, but the last few times I've had to write code, especially those that require heavy text parsing (which the majority of my code is for) I have used Perl because I was in a time crunch and knew I could write it in half the time it would take me to in Python (because I am not as experienced in Python).

If you are interested in learning Perl 6, here are some good resources I have bookmarked:
  1. Using Perl 6 Book (free download)
  2. Going From Perl 5 to Perl 6
  3. Perl 6 Documentation

Actually writing this document I have learned a great deal more about Perl. Final note - I consider myself an intermediate Perl programmer so take what I say with a grain of salt.

 


 

Table of Contents


  1. Quotes

  2. Special Characters

  3. Data Types

  4. Flow Control

  5. Subroutines

  6. Complex Data Structures

  7. Date

  8. Mail

  9. Useful Snippets

  10. Command Line Options

  11. Debugging

  12. CPAN

  13. Documentation

  14. Best Practices

  15. Perl 6



 

 

Quotes

Quotes in Perl mean different things. Below is a table which will help you remember what they are.

Construct Results In Interpolates Stands For
q// ' ' No Literal
qq// " " Yes Literal
qx// ` ` Yes Command
qw// ( ) No Word List
qr// m// Yes Regex

"Interpolates" means if whatever is between the quotes (or slashes) will be executed it has a special meaning in Perl. The best way to show this is with an example using variables:
    my $var = "some string"
    print "$var" <--This will print: some string
    print '$var' <--This will print $var

The above could have also been written like this and it would have produced the exact same results:
    my $var = 'some string';
    print qq/$var\n/;
    print q/$var/;

That is what the "Construct" above means. It provides you a shortcut (or an alternative way - which is Perl's strength or weakness depending on who you ask) and saves you typing.

A good example of how these constructs can save you typing is using "qw" to build arrays. Here is the traditional way to build an array:
    my @array = ("first", "second", "third", "fourth");
and here it is with the "qw" construct:
    my @array = qw/first second third fourth/;
Note, you do not have to use forward slashes (/) to enclose your data. Another common symbol you see are parentheses. You could have written the above like this:
   my @array = qw(first second third fourth);

"qx" is for system commands. Here are two ways to print the contents of your current directory:
  my $cmd = `ls -al`;
  my $results = $cmd;
  print $results;
  versus
  my $cmd = qx/ls -al/;
  my $results = $cmd;
  print $results;

"qr" is very usefull. It allows an easy way to use a regex in a variable which is very handy when you have a long regex. Here is a simple example:
  print "Yes\n" if 5 =~ /\d+/;
  versus
  $regex = qr/\d+/;
  print "Yes\n" if 5 =~ $regex;

 

[Table of Contents]

 

Special Characters

Perl has A LOT of special characters. They are meant to cut down on the amount of code you have to write, but this is a big reason Perl has the reputation of line noise (aka difficult to read). There are two different versions of the special characters, the more common short hand version, and the long hand version. According to Perl Best Practices, you should always use the long hand version, but you are more likely to see the short hand version in other people's code so you should know both.

In parantheses I put the "English" name of the variable. Perl Best Practices states you should use these. When calling it in your program, always call it like so:
use English qw(-no_match_vars);

"man perlvar" for the complete list and more more details on these

 

[Table of Contents]

 

Data Types

Perl has three types of variables: scalars, arrays, and hashes.

Scalars Arrays Hashes


Support this site by shopping at Amazon.com