Introduction to Perl Script - BunksAllowed

BunksAllowed is an effort to facilitate Self Learning process through the provision of quality tutorials.

Community

Introduction to Perl Script

Share This

It's a open source, general-purpose scripting language (supports cross platform) developed for text processing. Though now-a-days, it is used in different types of tasks, like web development, system administration, network programming, and many more.

Officially, Perl is not an acronym though it is known as Practical Extraction and Report Language.

It is a scipting language, as its interpreted not compiled. Unlike Python, it's a free-format language, hence, indentation is not mandatory. Though you are being suggested to follow any indentation rule to make this code readable.

Installation Procedure

Download Perl from it's official website https://www.perl.org.

If you are using Unix/Linux system, download the zip file and install the code according to the following instructions:

$tar -xzf perl-5.x.y.tar.gz $cd perl-5.x.y $./Configure -de $make $make test $make install

Once installation is done, you can issue perl -v command at $ prompt to check installation. If installation is successful, it will display version information.

To install this software on Windows platform, you can download Strawberry Perl installation file from http://strawberryperl.com. Then install the file as you install other applications on Windows platform.

How to run Perl Script?

To start Perl coding in the interactive interpreter, you can write the code on terminal (on Unix/Linux)/command prompt (on Windows) as shown below.


$perl -e <sample_code>

or
C:\MyPrograms>perl -e <sample_code>

Perl programs can be executed from a graphical user interface (GUI) environment, such as Padre. If you are familiar with Eclipse, you can also use Eclipse Plugin EPIC.

A Perl program consists of a sequence of declarations and statements, which run from the top to the bottom.

Let's try to test our first program as follows -


$perl -e 'print "Hello World\n"'

Let us try to write a program in a file and run it. Open a text file test.pl and put the following lines inside your file.


#!/usr/bin/perl print "Hello, world\n";

Here /usr/bin/perl is actual the perl interpreter binary. If the installation location is different, choose the proper location of the interpreter library.

In this program, parenthesises are not mandatory, it's upto your chice. Try the following code.


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

Remember that a Perl file must be saved with a .pl or .PL file extension.

Comment Lines

Like other programming languages, in this programming language # is used for single line comment. For multi-line comment, the text is placed between =begin comment and =cut . Check the following program.

#!/usr/bin/perl # It is single line comment print "Hello, world\n"; =begin comment Multi-line comment Multi-line comment Multi-line comment =cut

Quotes in Perl

Both double or single quotes can be used in string literals as shown below

#!/usr/bin/perl

print "Hello, world\n";
print 'Hello, world\n';

But these two are not equivalent. Double quotes can be used to interpolate special characters and variables. But single quotes can not be used for these. Check the following example

#!/usr/bin/perl $a = 10; print "Value of a = $a\n"; print 'Value of a = $a\n';

To work with multi-line string you can use the following technique.


#!/usr/bin/perl $a = 10; $var = <<"EOF"; This line one This line two This line three The value of variable a is $a EOF print "$var\n"; $var = <<'EOF'; This line one This line two This line three The value of variable a is EOF print "$var\n";

Escape Character Sequences

Like most of the programming languages, Perl uses the backslash (\) character to escape any type of character, which may interfere code. Try the following code:

#!/usr/bin/perl $result = "It is \"number\""; print "$result\n"; print "\$result\n";

Happy Exploring!

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.