Description

This document is intended to give you a quick overview of the Perl programming language, along with pointers to further documentation. It is intended as a "bootstrap" guide for those who are new to the language, and provides just enough information for you to be able to read other peoples' Perl and understand roughly what it's doing, or write your own simple scripts.

Introduction

Perl is a general-purpose programming language originally developed for text manipulation and now used for a wide range of tasks including system administration, web development, network programming, GUI development, and more. Perl is intended to be practical rather than beautiful.

Its major features are that:

  • easy to use
  • supports both procedural and object-oriented (OO) programming
  • has built-in support for text processing
  • has large collection of third-party modules.

Running Perl Programs

To run a Perl program from the Unix command line:

perl progname.pl

Alternatively, put this as the first line of your script:

#!/usr/bin/env perl
Safetynet

Perl by default is very forgiving. In order to make it more robust it is recommended to start every program with the following lines:

#!/usr/bin/perl
use strict;
use warnings;

The two additional lines request from perl to catch various common problems in your code. They check different things so you need both. A potential problem caught by use strict; will cause your code to stop immediately when it is encountered, while use warnings; will merely give a warning (like the command-line switch -w) and let your code run.

Basic Syntax

A Perl script or program consists of one or more statements. These statements are simply written in the script in a straightforward fashion. There is no need to have a main() function or anything of that kind.

Perl statements end in a semi-colon:

print "Hello, world";
Comments start with a hash symbol and run to the end of the line
# This is a comment

Whitespace is irrelevant:

print
    "Hello, world"
    ;

except inside quoted strings:

# this would print with a linebreak in the middle
print "Hello
world";

Double quotes or single quotes may be used around literal strings:

print "Hello, world";
print 'Hello, world';

However, only double quotes "interpolate" variables and special characters such as newlines (\n):

print "Hello, $name\n";    # works fine
print 'Hello, $name\n';     # prints $name\n literally

Numbers don't need quotes around them:

print 42;

You can use parentheses for functions' arguments or omit them according to your personal taste. They are only required occasionally to clarify issues of precedence.

print("Hello, world\n");
print "Hello, world\n";
Summary

In this introduction you have a very breif understanding of:

  • What is Perl
  • How to run Perl program
  • Basic Syntax

All the documentation in this page is taken from Official Perl Documentation.