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: Basic I/O

author: Valeria Paixão
revision: André Batosti
enhancement/translation: Vinny Alves

Note from UseStrict: Some of the examples in this tutorial were borrowed from Randall Schwartz’s Learning Perl. It’s a book that EVERY beginner Perl programmer should have. If you don’t have a hardcopy, please consider getting one. You can find it here: Learning Perl, 5th Edition


In this article, you will learn how to use basic I/O in Perl, learn about @ARGV, and become familiar with string formatting using printf.


<STDIN> tells Perl to read from the standard input – usually the keyboard.

while (defined($_ = <STDIN>)) {
	print "I saw $_";  # echoes whatever is typed onto the screen. 
                                # Quit with ^D or ^Z (depending on your system)

foreach (<STDIN>) { 
        print "I saw $_"; # almost the same as above

The difference between the while and foreach loops above is that while executes its statements at every hit of the return key, while foreach slurps into memory all the input until eof (^D on Unixes) and only then executes its instructions.

It is important to note this difference if you don’t want to crash your machine. If your input comes from a webserver with a 400MB log file, you’re better off processing each line individually than slurping it all into memory.
Continue Reading…

Perl Crash Course: File and Directory Tests and Manipulation

by André Batosti
revision: Vinny alves

Opening Files

To read or write files in Perl, you need to open a filehandle. Filehandles in Perl are yet another kind of identifier.
They act as convenient references (handles, if you will) between your program and the operating system about a particular file. They contain information about how the file was opened and how far along you are in reading (or writing) the file. They also contain user-definable attributes about how the file is to be read or written.

To open a new file on system you need to create the filehandle for this file using the command open

open(filehandle, pathname);

The filehandle is the identifier that will describe the file and the pathname – the full path of the file you trying to open. Typically it is represented by a constant, but when working with complex programs, it is best to use a scalar variable in order to safely pass it from one subroutine or method to another.

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…

Future postings on MySQL

On my previous post, I talked about MySQL functionality “insert into … on duplicate key update” which I found really cool. It has since been the most popular of my messages (although nobody leaves any comments), so I guess I’ll start talking more about MySQL… Don’t forget to keep an eye out for fresh Perl updates as well though – the Perl Crash Course is slowly coming out of the oven.

Cheers to all.

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…