Your First Perl Script on Windows

Dec 15, 2007 12:35 GMT  ·  By

Perl is a programming language that is very easy to learn. In order to begin, you will need to download and install a Perl distribution package compatible with your operating system. You can get the latest Perl version from perl.com website.

To simplify the Perl installation it is recommended to use ActivePerl from Active State, because this Perl distribution is pre-configured to provide an automatic installation of Perl on any operating system. After you select the free Windows distribution package, download it and the automatic installer will configure Perl to work on your operating system.

You can check if Perl was installed successfully on your system by typing in a DOS command.com window: perl -v. You should see the Perl version information in the same window. Now, you are ready to deploy your first Perl script.

Let's consider the next code example:

code
print "This is my first Perl Script
";
print "A New Line
";
$a = 3;
$b = 5;
$c = $a+$b;
print 'the result of the sum between '.$a.' + '.$b.' is '.$c;
print"
";
print "The value of c variable is $c";
To create Perl codes, you can use any text editor you wish, but for the present example Notepad will be enough. In a new Notepad window, copy and paste this code and save the file as script.pl on the root of c drive, or in any other directory you want. In the command prompt window, change the directory path to your script.pl directory. You can do that by using the cd (change directory) command. Now, in order to run the script, you must type on the DOS prompt perl script.pl. Let's identify the role of every script line in creating the program output, which can be seen in screenshot.

The print function allows you to display strings and variables. To display text or variables values on a new line the directive must be specifyed. $a, $b and $c are scalar variables. A variable is denoted by the $ sign in Perl programming language. The sum of variables $a and $b is stored in the $c variable, whose value is outputed in lines 6 and 8 using different coding techniques.

This Perl short introduction should be enough to understand the simplicity of this programming language. If you have a previous programming experience with PHP, C or C++, Perl language will be easily understood and implemented in your next scripts. The next step should be towards employing Perl in CGI scripts to create dynamic server side web pages and applications.