Perl Crash Course: Pragmas, Perl Modules, and CPAN

I always like to say that 90% of Perl is its modules. Back in 2000 when I was working as a junior Perl programmer I was asked to write a web application that, among other things, could send contact messages through email. Unfortunately, I never had anyone to really teach me the Path of Perl – I only relied on Learning Perl by Randall Schwartz, and whatever I could find on the net. I had a really hard time with that application, mainly because I didn’t know about Perl modules, MySQL and SQL language. Had I been familiar with at least the Perl modules part, I wouldn’t have had to spend 8 days and nights in the office (including my birthday). I didn’t even know how to use strict; at the time! Keep reading if use strict; makes no sense to you.

Being the extensible and flexible language that it is, Perl provides us with some safeguards and helpers to assist in avoiding what happened to me (I wish I knew that back then). The first of which I’ll talk about is Pragmas.
Continue Reading…

Perl Crash Course: Control Structures

by André Batosti
revision: Fernando Giorgetti and Vinny Alves

Control Structures are used to control the flow of a program. We are going to see programs that can iterate (loop) or make decisions (conditionals) based on the state of variables.

More interesting possibilities arise when we introduce control structures and looping. Perl supports lots of different kinds of control structures which tend to be like those in C, but are very similar to Pascal, too.
Continue Reading…

Perl Crash Course: Basic Regular Expressions

Coming soon, a revamped version of this article. Sorry for the inconvenience.

« Gettin’ jiggy wit it | TOC | Control Structures »

Perl Crash Course: Subroutines


Subroutines are user-created functions that execute a block of code at any given place in your program. It is a best practice, however, to aggregate them all either at the beginning or the end the main program.

Subroutine declarations initiate with the key word “sub” . Conventionally, subroutine names are all lowercase characters


print_hello; # subroutine can be executed/called before the actual block is created

sub print_hello {
      print "Hello world\n";

When we called print_hello we told Perl that we wanted the piece of code named print_hello to be executed. The result is a “Hello World” showing up on our screen. The only benefit we have from that snippet in its current form is that we won’t have to copy/paste the print statement all over our script if we want to repeat it. All we need to do is call print_hello;
Continue Reading…

Perl Crash Course: Gettin’ jiggy wit it

So far we’ve seen how Perl’s basic data structures work and also how to put them all together. Now it’s time to see what built-in functions and techniques Perl provides us to work with scalars, arrays, and hashes.

General functions

print [FILE HANDLE] list of data

Used to print data. If not passed a FILE HANDLE, it will print directly to STDOUT. Interpolation rules apply. Some interesting details about print are:

print @array;  # prints all elements of @array with 
               # no spaces between them
print "@array";  # prints the elements WITH spaces between them
print $var1, $var2, @array;  # prints values with no spaces between them
print FH "some data";  # prints the string "some data" inside file represented by FH (see open below).
# Note the lack of comma between the FH and the data to be printed.

open (FILE HANDLE, mode, filename) or open (FILE HANDLE, “modefilename”)

Opens a file or pipe for reading/writing/appending. Normally filehandles tend to be constants in uppercase, but it’s good practice to use variables to hold your filehandles. That way you can pass them between functions without occupying the main namespace. There are 2 styles of open – old style with 2 parameters and new style with 3.

Continue Reading…

Perl Crash Course: References and Complex Data Structures (CDS)

References and Complex Data Structures (CDS)

Now that we have a pretty good understanding of the 3 types of data in Perl, it’s time we bump it up a notch and put them all together. Perl is most famous for its text manipulation using regular expressions, but it should also be noted by the ability to create multilevel data structures, regardless of them being scalars, arrays, or hashes.

Continue Reading…

Perl Crash Course: Variables and Data Structures

In this chapter, we will see the three types of variables available in Perl: Scalars, Arrays, and Hashes. We will also work with lists, slices, and Complex Data Structures. However, before we go on, you will need to know a few basic commands so you understand the examples.

Code comments: In Perl, you can add comments to the code by using the character #. Whatever is to the right of a # will not be parsed (with at least one exception: the $#array structure which we will see later in the Arrays section). You may find it annoying to not have /*  */ structure for multi-line comments, but you’ll be happy to know that you can emulate multi-line comments using POD, which we’ll see later on.

Printing: Perl prints to the screen through the command print, which takes a list of parameters as input.

Assigning: To assign values to a variable, use  = (the equals operator).

Statement separator: Every statement in Perl ends with ; (semi-colon). Failure to end a statement with ; will result in a syntax error when trying to run your script.

Math: Mathematical operators are +, , *, /, and %, for addition, subtraction, multiplication, division, and modulus – respectively. The modulus operator returns the remainder of a division.

Now on to the scalars…

« Getting it installed on… | TOC | Scalars »