Perl - Environment Setup

Perl - Environment Setup

Try it Option Online

We have set up the Perl Programming environment online, so that you can compile and execute all the available examples online. It gives you confidence in what you are reading and enables you to verify the programs with different options. Feel free to modify any example and execute it online.
Try the following example using our online compiler available at CodingGround
#
# Hello World Program in Perl
#
print "Hello World!\n";
For most of the examples given in this tutorial, you will find a Try it option in our website code sections at the top right corner that will take you to the online compiler. So just make use of it and enjoy your learning.
Before we start writing our Perl programs, let's understand how to setup our Perl environment. Perl is available on a wide variety of platforms −
  • Unix (Solaris, Linux, FreeBSD, AIX, HP/UX, SunOS, IRIX etc.)
  • Win 9x/NT/2000/
  • WinCE
  • Macintosh (PPC, 68K)
  • Solaris (x86, SPARC)
  • OpenVMS
  • Alpha (7.2 and later)
  • Symbian
  • Debian GNU/kFreeBSD
  • MirOS BSD
  • And many more...
This is more likely that your system will have perl installed on it. Just try giving the following command at the $ prompt −
$perl -v
If you have perl installed on your machine then you will get a message something as follows −
This is perl 5, version 16, subversion 2 (v5.16.2) built for i686-linux

Copyright 1987-2012, Larry Wall

Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.

Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl".  If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.
If you do not have perl already installed then proceed to the next section.

Getting Perl Installation

The most up-to-date and current source code, binaries, documentation, news, etc. are available at the official website of Perl.
Perl Official Website − http://www.perl.org/
You can download Perl documentation from the following site.
Perl Documentation Website − http://perldoc.perl.org

Install Perl

Perl distribution is available for a wide variety of platforms. You need to download only the binary code applicable for your platform and install Perl.
If the binary code for your platform is not available, you need a C compiler to compile the source code manually. Compiling the source code offers more flexibility in terms of choice of features that you require in your installation.
Here is a quick overview of installing Perl on various platforms.

Unix and Linux Installation

Here are the simple steps to install Perl on Unix/Linux machine.
  • Open a Web browser and go to http://www.perl.org/get.html.
  • Follow the link to download zipped source code available for Unix/Linux.
  • Download perl-5.x.y.tar.gz file and issue the following commands at $ prompt.
$tar -xzf perl-5.x.y.tar.gz
$cd perl-5.x.y
$./Configure -de
$make
$make test
$make install
NOTE − Here $ is a Unix prompt where you type your command, so make sure you are not typing $ while typing the above mentioned commands.
This will install Perl in a standard location /usr/local/bin and its libraries are installed in /usr/local/lib/perlXX, where XX is the version of Perl that you are using.
It will take a while to compile the source code after issuing the make command. Once installation is done, you can issue perl -v command at $ prompt to check perl installation. If everything is fine, then it will display message like we have shown above.

Windows Installation

Here are the steps to install Perl on Windows machine.
  • Follow the link for the Strawberry Perl installation on Windows http://strawberryperl.com
  • Download either 32bit or 64bit version of installation.
  • Run the downloaded file by double-clicking it in Windows Explorer. This brings up the Perl install wizard, which is really easy to use. Just accept the default settings, wait until the installation is finished, and you're ready to roll!

Macintosh Installation

In order to build your own version of Perl you will need 'make' which is part of the Apples developer tools usually supplied with Mac OS install DVDs. You do not need the latest version of Xcode (which is now charged for) in order to install make.
Here are the simple steps to install Perl on Mac OS X machine.
  • Open a Web browser and go to http://www.perl.org/get.html.
  • Follow the link to download zipped source code available for Mac OS X.
  • Download perl-5.x.y.tar.gz file and issue the following commands at $ prompt.
$tar -xzf perl-5.x.y.tar.gz
$cd perl-5.x.y
$./Configure -de
$make
$make test
$make install
This will install Perl in a standard location /usr/local/bin and its libraries are installed in /usr/local/lib/perlXX, where XX is the version of Perl that you are using.

Running Perl

The are following are the different ways to start Perl.

(1) Interactive Interpreter

You can enter perl and start coding right away in the interactive interpreter by starting it from the command line. You can do this from Unix, DOS, or any other system, which provides you a command-line interpreter or shell window.
$perl  -e <perl code>           # Unix/Linux

or 

C:>perl -e <perl code>          # Windows/DOS
Here is the list of all the available command line options −
OptionDescription
-d[:debugger]Runs program under debugger
-IdirectorySpecifies @INC/#include directory
-TEnables tainting checks
-tEnables tainting warnings
-UAllows unsafe operations
-wEnables many useful warnings
-WEnables all warnings
-XDisables all warnings
-e programRuns Perl script sent in as program
fileRuns Perl script from a given file

(2) Script from the Command-line

A Perl script is a text file which keeps perl code in it and it can be executed at the command line by invoking the interpreter on your application, as in the following −
$perl  script.pl          # Unix/Linux

or 

C:>perl script.pl         # Windows/DOS

(3) Integrated Development Environment

You can run Perl from a graphical user interface (GUI) environment as well. All you need is a GUI application on your system that supports Perl. You can download Padre, the Perl IDE. You can also use Eclipse Plugin EPIC - Perl Editor and IDE for Eclipse if you are familiar with Eclipse.
Before proceeding to the next chapter, make sure your environment is properly setup and working perfectly fine. If you are not able to setup the environment properly then you can take help from your system admininstrator.
Al the examples given in subsequent chapters have been executed with v5.16.2 version available on the CentOS flavor of Linux.