A Perl Script

These sites are FOR SALE: miscperlinfo.com, mpihowto.com!
If you are interested, please use my Contact page.

Generally, a Perl script consists of a sequence of comments, declarations and statements.

Click Below to See the Best

Perl How-to Snippet Collection

and Save Hours of Surfing on Internet!

You can insert comments in your script wherever you want by using the # symbol, and anything you type (after it) until the end of the line will be ignored by Perl. However, there is an exception and we are speaking about the first line of a Perl script:


This line is not necessary in Windows because the Perl interpreter is determined by the file association of the system. However, even if you use Perl for windows, the interpreter does take note of this first line in order to determine any additional command line switches that you might need to apply. The #! character sequence known as "hash-bang" lets the linux/unix shell know what interpreter to use and where this interpreter is located.

As you know, there is no need to declare variables in a Perl script. There are global declarations like subroutine and format declarations that we typically include at the beginning or the end of the script. You may either declare scoped variable using my or our preceding the variable name. However, a Perl declaration can be placed anywhere a statement could be placed, i.e anywhere in the script file.

A statement is an expression for the computer to process or evaluate. Generally, a statement ends in a semicolon which means that the statement is complete. After evaluated, each sentence has a value which is used by Perl to evaluate or process other statements. The statements can be grouped in blocks by surrounding them in curly braces. The Perl interpreter views a statement block as an individual statement. There are:

  • conditional statements like if and unless, which use a relation expression to check if a condition is true
  • loop statements like while, for, foreach, do-while, until which check a condition and execute the statements included in curly braces until that condition is evaluated true
The Perl language has three data types: scalars, arrays and hashes. For storing the data, like any other programming language, Perl uses variables which are of course of the type enumerated above. To process or manipulate the data objects, Perl has a lot of operators which could be numerical, string or special.

To process some actions, Perl made available a various number of Perl built-in functions grouped by categories, such as functions for scalars and strings, input/output functions and so on. You can build your own functions, called subroutines.

If you want to reuse some piece of code already written by others, you can use the modules in your Perl script. You can search on CPAN for a great variety of modules, that you can use for free. Or you can write your own. Using modules in Perl could be essential if you need to work with databases or to write Internet programming applications, and the examples don't stop here.

One of the simplest Perl script example is the following:

This Perl script, after executing, will print the message "Hello World".

Now, we’ll show you a more complicated Perl script, for educational purposes only, where we’ll try to use some of the topics mentioned above.

#!/usr/bin/perl        # "hash-bang" line

# A Perl script
# this is a comment line

# some declarations with the use clause
# =====================================

use warnings;   # it warns about undefined values
use strict;     # it's a lexically scoped declaration

# a simple statement
# ==================
# the next code initializes $pi scalar variable
# any scalar variable begins with $ symbol

my $pi = 3.14;    # the statement ends in a semicolon

# a conditional statement
# =======================

# also note the numeric operator == and 
# the use of the print function

print "pi = $pi\n" if $pi == 3.14;
# it will output pi = 3.14

# a while loop statement
# ======================

my @numbers = ();   

# this line declares and empties the 
# @numbers array variable;
# any array variable begins with the @ symbol

while(my $line = )
  chomp $line;
  push(@numbers, $line);

# we read some lines from  using the
# $line scalar variable
# chomp function will remove the ending newline
# push function will append the read number 
# to @numbers array
# the loop will end when we type Ctrl/Z

# calling a subroutine
# ====================

# in the following lines we will declare and 
# call a subroutine that will add two numbers
# and it will return their sum

sub add2Numbers
  my $firstNumber = $_[0];    # first argument
  my $secondNumber = $_[1];   # second argument
  return $firstNumber + $secondNumber;

# calling the subroutine declared above:

print add2Numbers(3, 5), "\n";

# it will output 8

# using a module
# ==============

# the following code snippet will show you
# how to use a module in your script;
# we’ll exemplify with the DateTime-0.47 module;
# in order to run this script you must have 
# already installed this module on your system

use DateTime;

# the module must be declared first through use clause

my $dt = DateTime->now;

# $dt variable is initializes with the current date

print "Current Date:\n";
print "Month:", $dt->month, "\n";
print "Day:", $dt->day, "\n";
print "Year:", $dt->year, "\n";

# it will print the current month, day and year

A-N-Y-O-N-E Can Learn and Master Perl!
And That Includes YOU!

Check these how-to tutorial eBooks (PDF format):

Table of Contents:

A Perl Script (more)
Install Perl
Running Perl
Perl Data Types
Perl Variables
Perl Operators
Perl Lists
Perl Arrays
    Array Size
    Array Length
Perl Hashes
Perl Statements
    Perl if
    Perl unless
    Perl switch
    Perl while
    Perl do-while
    Perl until
    Perl do-until
    Perl for
    Perl foreach
Built-in Perl Functions
    Functions by Category
        String Functions
        Regular Expressions and Pattern Matching
        List Functions
        Array Functions
        Hash Functions
        Miscellaneous Functions
    Functions in alphabetical order

return from A Perl Script to Perl Basics

Would you like to create your own website like this one?
Hit the Alarm Clock!

Site Build It!