Vous êtes sur la page 1sur 7

Activity 1: Introduction To Perl

Objectives:
Introduce the Perl programming language. Introduce some Perl communities. Coding and running our first Perl script.

Concepts
System Administration with Perl
For quite some time, system administrators have chosen Perl as the primary scripting language. Some considerable factors for this are: Excellent text manipulation capabilities. Perl is the best tool to use at manipulating log files and other regular data. This makes it easy to automate general housekeeping associated with system maintenance. It is a very powerful tool extracting data and trends from various kinds of application log files. CPAN (Comprehensive Perl Archive Network) gives Perl virtually infinite extensibility, full database connectivity and Unicode support. Literally, there are thousands of third party modules to solve all sorts of different problems. If you have a task to fulfill then chances are reasonable that someone else has already done some of it for you. Perl simplifies the management of disparate database platforms. Portability. Perl is deployed in over 30 various operating systems. Speed. Perl is fast to write and fast to run, making it perfect for small once-off tasks but also great for huge projects. Perl has extensive documentation freely available. Much of what can be done in bash, sed, awk and C can be transferred almost directly into Perl code. Likewise the format of many functions are equivalent to common Linux/Unix commands. Perl makes it easy to work directly with hardware and sockets. Open-source

What is Perl?
The name of the programming language is Perl and it stands for Practical Extraction and Report Language created by Larry Wall. Larry Wall developed Perl because during his time (1980s) he needed something with the quickness of coding available in shell or awk programming and with some of the power of more advanced tools like grep, cut, sort and sed (see instructors past lessons for more details) without having to resort to a language like C.

Setting Up Perl
Almost every major operating system aside from Windows ships with Perl by default. This is often referred as the system Perl. To test whether you already have Perl installed, execute perl -v. See illustration below:

As you can see, I am using Perl5 version 14 on my Linux Mint box.

Using Windows
Unfortunately, Windows operating system does not package Perl by default. There are a lot of options one can use to obtain Perl. The following are most commonly used: Strawberry Perl (http://strawberryperl.com) ActivePerl (http://www.activestate.com/activeperl) Cygwin (http://www.cygwin.com)

For our class we will be using Strawberry Perl. The instructor has chosen Strawberry Perl because it has a strong community supporting it; this community can help us work through even the most unusual issues. On this activity paper, you will find an instruction on how to install Perl in Windows which is fairly easy to follow.

The Perl Community


PerlMonks PerlMonks (http://www.perlmonks.org) is a great Perl site that has been around for more than a decade. You can find useful links that answers a lot of Perl script problems. In addition to answering questions, PerlMoks has book reviews, tutorials, news, site discussion and chatterbox for those who just want casual conversation or have a quick question.

Perl Mongers Perl Mongers (http://www.pm.org) is an organization of Perl hackers in different cities who meet periodically. They are Perl enthusiasts who enjoy hanging out together and talking about stuff mostly about Perl. StackOverflow StackOverflow (http://stackoverflow.com/) was created in 2008 by Joel Spolsky and Jeff Atwood as an open site for anyone to ask programming-related questions. It has spun off numerous related sites and has become extremely popular as the site where you can ask just about any technology question. Perl questions are answered quickly with solid information and you can easily see the rating of the users who respond to your questions. Because of how questions are tagged, it is easy to quickly drill down to questions that might be relevant to your situation.

Installing Perl in Windows (Strawberry Perl)


Installing Strawberry Perl in Windows is fairly straightforward. Follow the steps highlighted below. 1. Download the latest stable version of Strawberry Perl (http://strawberryperl.com). They both have 32-bit and 64-bit version. Choose the one compatible with your operating system.

2. Once download is finished, double-click the installer file (strawberry-perl-5.16.3.1-32bit.msi). 3. Installation wizard appears, just click Next.

4. Confirm the Licensing Agreement by ticking the checkbox.

5. You will be asked where to install the Perl files. Just accept the default. (C:\strawberry)

6. Confirm Perl installation.

7. Once you are finished, reboot your Windows operating system and you are done.

Coding Our First Perl Script


1. Enter the following lines of code using a text editor of your choice and save it as hello.pl

2. From the command line, cd to the directory where you save your Perl script and execute: perl hello.pl. You should be seeing the following output.

3. On Linux and other system that understand chmod, you can give your Perl script an executable permission by executing chmod u+x Perl_Script. Then to run it, you can use either a relative path or an absolute path to your Perl script. See illustration below:

Analysis
On the command line, when you type perl followed by the name of the file containing a Perl program, Perl reads that file, parses the code and executes it. The first line of a Perl script often starts with a shebang line. o A shebang line starts with the sharp (#) and an exclamation point, also known as bang (!), hence the term shebang. o The line is followed by a path telling the shell where to find the interpreter that is used to execute the program. On Windows operating systems you might see the following:
#!C:\Perl\bin\perl.exe #!C:\strawberry\perl\bin\perl.exe

o o

The first line is often found when running with ActiveState Perl. The second line is found when running with Strawberry Perl.