How to Program in Perl: An Overview for Learning the Language

An Overview for Learning the Language

Note: The article assumes that the reader has a fundamental understanding of at least one other programming language, and that this is intended serve as a quick reference for Perl's basic syntax & behavior in regards to Variables, Control Structures, & Functions.

Learning to program is not a hard thing, but it can be challenging to get a program to do exactly what you want it to do. Perl is a programming language created in 1987 and has evolved over the last 20 years to be easy, but also very complex in its shortcuts. I will try to cover the basics of how to program in Perl and show how easy it can be to use. Keep in mind, there is more to the language than what I am showing you, and there's lots of shortcuts that can be taken if you take the time to go find them.

Warnings

  • What I am showing here is only the tip of the iceberg. There is so much simplicity and complexity to Perl. Feel free to check any of the Perl forums out there for more help.
  • Again, I do make the assumption that you already somewhat know how to program.

Tips

  • A good guide book for Perl will help you out tremendously. The O'Reilly Programming Perl (aka The Camel Book) is a great starting point.
  • There are many good programs for installation to use Perl. I like to use a program called Notepad++ and ActivePerl from ActiveState or Strawberry Perl.  These programs I use in a Windows environment.  Perl should be installed by default in Linux/Unix.  Can write Perl in vi or emacs.
  • All code blocks in this article should work in ActiveState Perl and a Windows 7 environment.  Should probably also work in Linux/Unix. 

Hello World

#!/usr/local/bin/perl

print "Hello World\n";

A pretty common place to start learning any language.  Now that it's out of the way, lets get into some more interesting stuff.

Variables

For the most part, variables can be defined with $VariableName. In Perl these are scalars. Arrays are defined as @VariableName and hashes are defined as %VariableName. These variables are case sensitive.

If/Else

The first control structure. If/else will run different code based on if some condition is true. You may even have several conditions to check for.

#!/usr/local/bin/perl

$var = 'Hello;

$var2 = 123;

if($var eq "Hello"){

    print "Hello\n";

}

else if ($var2 == 123){

    print "$var2 is 123\n";

}

else {

    print "Nothing Matches\n";

}

Easy enough to use and figure out what you need to, relative to your program.  It should be noted that string comparison versus number comparison use different operators.  Numbers will use '==', '>=', '<=', or '!=' to make comparisons.  Strings will use 'eq', 'lt', 'gt', or 'ne' to compare strings. 

Alternatively it should be noted how boolean's (true or false) are represented in Perl.  If a variable is 0 (zero the number) or empty it will evaluate to false.  If it is anything else, it will evaluate to true, including the string of "0" (zero as an actual character).

Loops

#!/usr/local/bin/perl

for($i = 0; $i < 100; $i++){

    print "Hello World\n";

}

$j = 0

while($j < 100){

    print "Hello World\n";

    $j++;

}

$k = 0

do{

    print "Hello World\n";

    $j++;

}while($k < 100);

Examples of loops. Pretty straight forward.

Functions

#!/usr/local/bin/perl

hello_world(); #0

hello_world(George);#1

hello_world(Bill, Ted);#2

sub hello_world{   

    print "Hello World @_ \n";

}

The 0's function calls the subroutine hello_wold() and just prints out the "Hello World\n".  Nothing is passed, so no scalar value is printed. Example 1, will Print the "Hello World George\n". Scalars are passes put into the subroutines @_ variable which is an array of Scalars. In case 2, it will print "Hello World Bill Ted\n".

An alternative way to define a function:

#!/usr/local/bin/perl

hello_world(Bill, Ted);

sub hello_world{  

    print "Hello $_[0] \n";

    print "Hello $_[1] \n";

}

In this case it will print out:

"Hello Bill

Hello Ted"

Conclusion

That is the basics of the language of Perl. It should be enough to get you creating simple Perl scripts for anything that you need. Feel free to dig around for more details on how to use Perl on the internet. Overall, Perl is really easy to use. It has been used and abused for 20+ years to be simple and fast to use, but also can be complex with all of the built-in shortcuts in the language to help programmers program or hack faster.

Warnings

Perl has been around for a long time.  It has been developed to be easy to use.  Most of the development of Perl has been geared towards making Perl easy to use for experienced programmers.  Lots of the code out there will be obfuscated to a degree but is perfectly legal within Perl usage.

Tips

  • There is so much more to Perl than meets the eye.  This article is just the tip of the iceberg of how easy it can be to use.  As you learn more about Perl you will find "shortcuts" to the language to make programming that much easier but also that much more difficult to read later. 
  • If you discover shortcuts to Perl,  it doesn't hurt to document your code.  Not required but it helps.
  • Keep in mind that in Perl, there is multiple ways of doing things. 
  • The internet is full of useful guides to Perl.  Most are very helpful and in some cases well documented. 
  • As mentioned above, the O'Reilly Programming Perl (aka The Camel Book) is a great starting point for learning more about Perl.

Other Reading

For a crash course in connecting and using a database with Perl, check out my article on how to database program with Perl.

Just updated your iPhone to iOS 18? You'll find a ton of hot new features for some of your most-used Apple apps. Dive in and see for yourself:

3 Comments

Any constructive criticism helps, especially for anything that I've missed that would be good to know to a beginning Perl programmer. I just learned the basics myself but I feel pretty competent about the basics. Maybe something for a secondary how-to on Perl? Like regular expressions, input/output, or whatever. More links to some good forms or something, maybe?

And whom ever changed the Title and added the intro note....Thanks! It is an improvement.

Made a few changes of my own. Few rewordings and removing of words.

Love it. It's a wonderful start... Now... I think input/output would be a GREAT next tutorial. ;-)

Share Your Thoughts

  • Hot
  • Latest