Monday, December 27, 2010

chomp(), chop(), substr(), length(), uc(), ucfirst(), lcfirst() functions in Perl

These are some inbuilt functions in Perl which are commonly used in the course 
of programing

#!/usr/bin/perl
print "content-type: text/html \n\n";

$str="hello world\n";

chomp($str);  # removes the new line character from the end of a string
print $str."Intrasoft\n";

chop($str); # removes the last character from the end of a string
print $str."\n";

$temp_str=substr($str,4,3); # returns the characters starting from the 5th position of the string to 3 characters from the 5th position of the string
print $temp_str."\n";

$temp_str=substr($str,4); # returns the characters starting from the 5th position of the string till the end of the string
print $temp_str."\n";

$str_len=length($str); # return the length of the string in number of charaters
print $str_len."\n";

$temp_str=uc($str); # converts and returns the string in upper case
print $temp_str."\n";

$temp_str=ucfirst($str); # converts and returns the string with it's 1st character in upper case
print $temp_str."\n";

$temp_str=lc($str); # converts and returns the string in lower case
print $temp_str."\n";

$temp_str=lcfirst($str); # converts and returns the string with it's 1st character in lower case
print $temp_str."\n";

Output:
-------
hello worldIntrasoft
hello worl
o w
o worl
10
HELLO WORL
Hello worl
hello worl
hello worl

Sunday, December 26, 2010

join() function in Perl

join() function in Perl is responsible for joining all the elements in an array
into a single string.

#!/usr/bin/perl

print "content-type: text/html \n\n";

@array = ("David","Larry","Roger","Ken","Michael","Tom");
$astring = join(",",@array);
print $astring."\n";

@array2 = qw(Pizza Steak Chicken Burgers);
$string = join("\n",@array2);
print $string;

Output:
-------
David,Larry,Roger,Ken,Michael,Tom
Pizza
Steak
Chicken
Burgers

Friday, December 24, 2010

scalar() function in Perl

scalar() is an inbuilt function of Perl responsible for returning the number of
elements present in the array passed to the function

#!/usr/bin/perl

print "content-type: text/html \n\n";

@nums = (1 .. 20); # assigning 1 to 20 in the array sequentially as separate elements
@alpha = ("a" .. "z"); # assigning a to z in the array sequentially as separate elements
$numofnums = @nums;

print "@nums\n";

print "@alpha\n";

print "There are $numofnums numerical elements";

print "There are ".scalar(@alpha)." letters in the alphabet!";

Output:
-------
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
a b c d e f g h i j k l m n o p q r s t u v w x y z
There are 20 numerical elements
There are 26 letters in the alphabet!

Wednesday, December 22, 2010

Hash/ Associative arrays in Perl

Hash/ Associative arrays in Perl are also responsible for storing multiple 
elements together in one variable, as a key value pair.

#!/usr/bin/perl

print "content-type: text/html \n\n"; 

%country_capital_hash=(
'india' => 'New Delhi',
'china' => 'beiging'
);

print "Capital of India is $country_capital_hash{'india'}\n";

Output:
-------
Capital of India is New Delhi

Tuesday, December 21, 2010

Some more examples of Perl arrays

Individual element in an array can be referred with their subscript position in
the array. The first element in an array is always at position "0". An array 
structure is always circular.

#!/usr/bin/perl

print "content-type: text/html \n\n";  

@array = ("Quarter","Dime","Nickel");

print "@array\n";

print "$array[0]\n"; # Prints the first element in the array

print "$array[1]\n"; # Prints the second element in the array

print "$array[2]\n"; # Prints the third element in the array

print "$array[-1]\n"; # Prints the last element in the array

print "$array[-2]\n"; # Prints second to last element in the array

Output:
-------
Quarter Dime Nickel
Quarter
Dime
Nickel
Nickel
Dime

Monday, December 20, 2010

Array variables in Perl

Arrays are list type variables, they contain any number of elements desired. 

#!/usr/bin/perl

print “content-type: text/html \n\n";

@days = ("Monday", "Tuesday", "Wednesday");
@months = ("April", "May", "June");

print "@days\n";
print "\n"; # displaying new line on the screen
print "@months\n";

Output:
-------
Monday Tuesday Wednesday
April May June

Saturday, December 18, 2010

Operators & Assignment


#!/usr/bin/perl

print "content-type: text/html \n\n";

$x = 81;                          
$add = $x + 9;    # calculating addition value
$sub = $x - 9; # calculating minus value
$mul = $x * 10; # calculating multiplication value
$div = $x / 9; # calculating division value
$exp = $x ** 5; # calculating exponential value
$mod = $x % 85; # calculating modulas/ remainder value

print "$x plus 9 is $add";

print "$x minus 9 is $sub";

print "$x times 10 is $mul";

print "$x divided by 9 is $div";

print "$x to the 5th is $exp";

print "$x divided by 85 has a remainder of $mod";


Output :
--------
81 plus 9 is 90
81 minus 9 is 72
81 times 10 is 810
81 divided by 9 is 9
81 to the 5th is 3486784401
81 divided by 79 has a remainder of 2

Wednesday, December 15, 2010

Concatenation of strings

Concatenation of strings here means, merging  two or more variables holding 
string type data into one variable side by side.

#!/usr/bin/perl

print "content-type:text/html\n\n";

$str1="Hello";

$str2="World";

$str=$str1." ".$str2;

print $str;

Output:
-------
Hello World

Friday, December 10, 2010

Scalar variables

Scalar variables are simple variables containing only one element. An element
can be either an entire string, a number, or a reference. Strings may contain any
symbol, letter, or number. Numbers may contain exponents, integers, or decimal
values.

#!/usr/bin/perl

print “content-type: text/html \n\n";

$mystring = "Hello, World";
$myescapechar = "Welcome to Joe\'s";
$myinteger = "5";
$myinteger2 = "-5";
$mypi = "3.14159";

print $mystring;

print $myescapechar;

print $myinteger;

print $myinteger2;

print $mypi;

Output:
-------
Hello, World!
Welcome to Joe's
5
-5
3.1459

Friday, December 3, 2010

Displaying "Hello World" on the screen


#!/usr/bin/perl

print "content-type:text/html\n\n"; #specifies the response header for the Perl script

print "Hello World"; #Displays Hello World on the screen

Output:
-------
Hello World

Thursday, December 2, 2010

How to use Perl for writing a program and executing it?

Following are the numbered steps, that are needed to be followed for successful 
execution of a Perl script :

1) Install Perl in your UNIX/ LINUX system ( get it from http://www.perl.org/get.html ),
if not already installed.

2) Open any text editor in your system and write the codes for the script.

3) Save the script by specifying a file name  with a ".pl" or ".cgi" extension 
( optional ). e.g: test.pl

4) In the above step the script file created can be stored in any location of your 
system but try to save the file in the "cgi-bin" folder ( which is automatically 
created at the time of Perl installation ) because then the script can also be run 
as a CGI script if requirement arises.

5) Finally the script can be executed as follows:
  • Go to the command prompt of your system and change directory to the location where the script is stored.
  • Type "perl file name.pl" and then hit enter. e.g: perl test.pl
  • or if running a CGI script, then open any web browser and type "http://server_address/cgi-bin/" and then hit enter. e.g: http://localhost/cgi-bin/test.pl

Wednesday, December 1, 2010

What is Perl?

Perl stands for (Practical Extraction and Report Language) and is also a popular CGI 
programming language. Perl makes our life easy in writing complex report processing 
programs, the main strength of the programing language is it's text processing 
facilities and easy manipulation of text files, it is also popularly used for server 
side scripting via CGI method.