Introduction to PERL - first steps and work with variables

Published:

Add / read comments

PERL acronym means Practical Extraction and Report Language and it's very practical and flexible. Its versatility permits versions of many programming paradigms: procedural, functional, and object-oriented. PERL is very similar to PHP in many ways. Let's start to run some PERL script on Windows 7 operating system.

First, you need to have PERL installed on your machine. To make sure, just open command line window and type something like this:

 C:\Users\dubak>perl -v

If answer is starting with something like in the box below, you are good to go. If not, just install PERL.

 This is perl, v5.10.1 (*) built for MSWin32-x86-multi-thread

Now we can start with first testing PERL script. Open your code / text editor (some IDE like Eclipse or NetBeans is the best option) and write down the following:

 #!"C:\xampp\perl\bin\perl.exe"
 
 print "Hello, Dubak!";

Firs line is pointing directly to PERL executable and second line is just simple statement which will be displayed in command window. So save file as "pl" extension and open command window. Change directory to same directory where you saved your PERL script and run next command:

 D:\web\perl>perl first.pl
 
 Hello, Dubak!

As you can see in command window, script displayed simple statement. Our PERL script is running great.

There is also another way to see the result of PERL scripts which is more convenient by my meaning. If you have server running on your system (best way is to install whole XAMPP (Apache, MySQL, PHP, Perl) package), try to open the PERL script on localhost via some web browser.

http://localhost/perl/first.pl 

You will see the server error 500. To correct it, open your previous script in code editor and update your code to following:

 #!"C:\xampp\perl\bin\perl.exe"
 
 print "Content-Type: text/html; charset=utf-8\n\n";
 
 print "Hello, Dubak!";

Second line of code will set content type and characters set. If you open PERL script via browser again, you should see displayed statement "Hello, Dubak!"

Now it's time to start work with variables. Let's start with adding of integer numbers. Open editor and write down the following code:

#!C:\xampp\perl\bin\perl.exe
 
# header for showing via browser  
print "Content-Type: text/html; charset=utf-8\n\n";
 
print "Work with variables";
 
# declaration and initialization of variable of INTEGER type
$a = 6;
$b = 7;
# adding 
$c = $a + $b;
# printing the result
print '<br />Result: '. $a . ' + ' . $b . ' = '.$c;

Open the PERL script via web browser and you should see the following:

Work with variables
Result: 6 + 7 = 13

Adding of float numbers. Open editor and the following code to your script:

# declaration and initialization of variable of FLOAT type (has floating point)
$a = 1.3;
$b = 2;
# adding 
$c = $a + $b;
# printing the result
print '<br />Result: '. $a . ' + ' . $b . ' = '. $c;

Open the PERL script via web browser and you should see the following:

Result: 1.3 + 2 = 3.3

Working with float numbers and their scientific notation. Open editor and the following code to your script:

# declaration and initialization of variable of FLOAT type (scientific notation)
$a = 1.2e4;  
$b = 1e6;
# printing the result
print '<br />1.2e4 (12 thousands) is - '. $a . ' and 1e6 (million) is - ' . $b;
print '<br />Result: '. $a . ' + ' . $b . ' = '.  ($a + $b);

Open the PERL script via web browser and you should see the following:

1.2e4 (12 thousands) is - 12000 and 1e6 (million) is - 1000000
Result: 12000 + 1000000 = 1012000

Let's move on subtraction, dividing, multiplying of numbers. Open editor and the following code to your script:

# adding, subtraction, dividing, multiplying
$a = 2;
$b = 3;
 
print '<br />Adding:  '. $a . ' + ' . $b . ' = '.( $a + $b );
print '<br />Subtraction: '. $a . ' - ' . $b . ' = '.( $a - $b );
print '<br />Dividing:   '. $a . ' / ' . $b . ' = '.( $a / $b );
print '<br />Multiplying: '. $a . ' * ' . $b . ' = '.( $a * $b );

Open the PERL script via web browser and you should see the following:

Adding: 2 + 3 = 5
Subtraction: 2 - 3 = -1
Dividing: 2 / 3 = 0.666666666666667
Multiplying: 2 * 3 = 6

Multiplying can be easily used to get powered values. Open editor and the following code to your script:

$a = 5;
print '<br />Multiplication of number: '.$a.' to the square of number = '. $a * $a .', and 5 to the third power = '. $a * $a * $a;

Open the PERL script via web browser and you should see the following:

Multiplication of number: 5 to the square of number = 25, and 5 to the third power = 125

PERL has many built-in numeric mathematical functions which are very handy. Open editor and the following code to your script:

# built-in mathematical functions (http://perldoc.perl.org/index-functions-by-cat.html#Numeric-functions)
 
$a = log(1000);
print '<br />Natural logarithm (base <em>e</em>) for a number 1000 is: '. $a;
 
$a = log(1000) / log(10);
print '<br />Decimal logarithm for a number 1000 is: '. $a;
 
$a = sqrt(25);
print '<br />Square root for a number 25 is: '. $a;
 
$a = exp(3);
print '<br />Exp() function returns <em>e</em> (the natural logarithm base) to the power of expression ( inverse function of log () ): exp(3) = '. $a;
print '<br />Log() function (inverse function of exp() ): log(20.0855369231877) = '. log(20.0855369231877);

Open the PERL script via web browser and you should see the following:

Natural logarithm (base e) for a number 1000 is: 6.90775527898214
Decimal logarithm for a number 1000 is: 3
Square root for a number 25 is: 5
Exp() function returns e (the natural logarithm base) to the power of expression ( inverse function of log () ): exp(3) = 20.0855369231877
Log() function (inverse function of exp() ): log(20.0855369231877) = 3

From these short examples, PERL seems to be very flexible, poweful and quick to learn programming language.

Published:

Add / read comments

FIND ME

Share, follow or connect with me.