PERL Basic Programs

PERL Basic Programs

A Perl program runs in a special interpretive mode, the entire script is compiled internally in memory before being executed. Unlike other interpreted language like the shell and awk, script errors are generated before execution itself.

1. Simple Hello World program:

#!/usr/bin/perl
#Program to print Hello World and greeting message
print("Hello World\n");
print("Welcome to Perl Programming\n");

 

Every Perl program starts with the location of Perl interpreter. In this case, Perl interpreter is present at /usr/bin/perl.

We used the interpreter line as the first line in all of our Perl script.

Perl variables need the $ prefix both in the definition as well as in evaluation.

The output of the Program:

Hello World
Welcome to Perl Programming

 

2. Perl program to demonstrate reading input from the standard keyboard.

#!/usr/bin/perl
#Sample Perl script to read from standard from the keyboard

print ("Enter Name: ");
$name = <STDIN>;
print ("The entered name is $name\n");

Like scanf statement in C programming and cin statement in C++ programming language, In this program <STDIN> is used to read data from standard input.

READ  PERL Basics

Output:

Enter Name: Vidyashri MH
The entered name is Vidyashri MH

 

3. Sample Perl script to show the use of variables & computation

#!/usr/bin/perl
#Sample perl script to show use of variables & computation – simple.pl

print ("Enter Your City Name: ");
$name = <STDIN>;
chop($name);
print ("Enter a temperature in Centigrade: ");
$centigrade = <STDIN>;
chop($centigrade );
$fahrenheit = $centigrade * 9 / 5 + 32;
print ("The temperature of $name in Fahrenheit is $fahrenheit\n");

<STDIN> is a file-handle representing standard input.

Here we have used chop function, which removes the extra newline character from the string. When prompt is displayed to read data from keyboard, we read data and press [Enter] button t finish reading data from keyboard. By default, if we read data from standard input such as keyboard, Perl considers pressing [Enter] as a part of data read from keyboard. Hence the data consists of actual data plus newline character. To remove the extra newline character we have used chop function.

Output:

Enter Your City Name: Belgaum[Enter]
Enter a Temperature in Centigrade: 38[Enter]
The temperature of Belgaum in Fahrenheit is 100.4

 

READ  PERL Basics

4. Sample Perl script to read two numbers and demonstrate arithmetic operations

#!/usr/bin/perl
#Sample Perl script to read two numbers and demonstrate arithmetic operations

print ("Enter First Number: ");
$num1 = <STDIN>;
chop($num1);
print ("Enter Second Number: ");
$num2 = <STDIN>;
chop($num2);
$sum = $num1 + $num2;
$dif = $num1 - $num2;
$mul = $num1 * $num2;
$div = $num1 / $num2;
$mod = $num1 % $num2;
print ("Addition of $num1 and $num2 is $sum\n");
print ("Substraction of $num1 and $num2 is $dif\n");
print ("Multiplication of $num1 and $num2 is $mul\n");
print ("Division of $num1 and $num2 is $div\n");
print ("Modulus of $num1 and $num2 is $mod\n");

 

Output:

Enter First Number: 10
Enter Second Number: 2

Addition of 10 and 2 is 12
Substraction of 10 and 2 is 8
Multiplication of 10 and 2 is 20
Division of 10 and 2 is 5
Modulus of 10 and 2 is 0

If you like the post, for regular updates do like my facebook page VTUPulse