Perl is a general purpose, high-level programming language. High-level language means, which you can use for wide variety of applications. It also has many modules available which can be used easily. It has made useful of great features from several other programming languages including C, shell script, awk, sed etc. This makes it even more efficient. It has most advanced feature of text data manipulations which makes it very useful in computational biology. It has also been used for a long time in Geophysics and there are many popular codes available in this language.
You can even use this language as a wrapper of your other group of programs which will make your program easy to use. The codes in Perl are very easy to read and understand which makes it very popular.
Let’s see some basics to get started with using Perl:
Any command can be called from the command prompt:
perl -e # Unix/Linux
—————————————————————————–
To change the line of the output or the command equivalent to return command in perl :
print "\n"; #new line character
-----------------------------------------------------------------------------
Each command in perl should terminate with the semicolon, “;”.
—————————————————————————–
Commenting
Comment in a perl script starts with the “#” entry.
Multiline Comment can be done using
=begin comment something something=cut
—————————————————————————–
We can also input set of strings or text as follows:
print "Hello world\n";
—————————————————————————–
The perl script can be invoked by the first line “#!/usr/bin/perl” (if the perl executable is present in this path, else you can find the path to perl using the shell command “which perl“).
—————————————————————————–
You can even make an alert or warning sound in Perl (enter in perl script)
print "Alert or bell - \a";
Manipulating the Capital/Small letter size:
# Only W will become upper case. $str = "\uwelcome to utpalworld.com!"; print "$str\n"; # Whole line will become capital. $str = "\UWelcome to utpalworld.com!"; print "$str\n"; # A portion of line will become capital. $str = "Welcome to \Uutpalworld\E.com!"; print "$str\n";
Following is a example perl script which can be run using the command:
Scalar Variables in Perl
$age = 25; # An integer assignment $name = "John Paul"; # A string $salary = 1445.50; # A floating point print "Age = $age\n"; print "Name = $name\n"; print "Salary = $salary\n"; print "\n";
Arrays in Perl
@ages = (25, 30, 40); @names = ("John Paul", "Lisa", "Kumar"); print "\$ages[0] = $ages[0]\n"; print "\$ages[1] = $ages[1]\n"; print "\$ages[2] = $ages[2]\n"; print "\$names[0] = $names[0]\n"; print "\$names[1] = $names[1]\n"; print "\$names[2] = $names[2]\n";
@var_10 = (1..10); @var_20 = (10..20); @var_abc = (a..z); print "@var_10\n"; # Prints number from 1 to 10 print "@var_20\n"; # Prints number from 10 to 20 print "@var_abc\n"; # Prints number from a to z print "Size: ",scalar @var_10,"\n"; #size of the array print "\n";
@array = (1,2,3); $array[50] = 4; $size = @array; $max_index = $#array; #maximum index print "Size: $size\n"; print "Max Index: $max_index\n"; print "\n";
push/pop & shift/unshift command:
#Adding and removing elements in the array # create a simple array @coins = ("Quarter","Dime","Nickel"); print "1. \@coins = @coins\n"; # add one element at the end of the array push(@coins, "Penny"); print "2. \@coins = @coins\n"; # add one element at the beginning of the array unshift(@coins, "Dollar"); print "3. \@coins = @coins\n"; # remove one element from the last of the array. pop(@coins); print "4. \@coins = @coins\n"; # remove one element from the beginning of the array. shift(@coins); print "5. \@coins = @coins\n"; print "\n";
Splice command for replacing arrays
#Replacing array elements @nums = (1..20); print "Before - @nums\n"; splice(@nums, 5, 5, 21..25); #splice(array,offset,length,replacement) print "After - @nums\n"; print "\n";
We can also split the string into an array and vice-versa:
#transforming strings into arrays # define Strings $var_string = "Rain-Drops-On-Roses-And-Whiskers-On-Kittens"; $var_names = "Larry,David,Roger,Ken,Michael,Tom"; # transform above strings into arrays. @string = split('-', $var_string); @names = split(',', $var_names); print "$string[3]\n"; # This will print Roses print "$names[4]\n"; # This will print Michael print "\n";
#transforming arrays to strings # define Strings $var_string = "Rain-Drops-On-Roses-And-Whiskers-On-Kittens"; $var_names = "Larry,David,Roger,Ken,Michael,Tom"; # transform above strings into arrays. @string = split('-', $var_string); @names = split(',', $var_names); $string1 = join( '-', @string ); $string2 = join( ',', @names ); print "$string1\n"; print "$string2\n"; print "\n";
Sorting the elements of an array:
##sorting arrays # define an array @foods = qw(pizza steak chicken burgers); print "Before: @foods\n"; # sort this array @foods = sort(@foods); print "After: @foods\n"; print "\n";
Merging two arrays:
##Merging arrays @odd = (1,3,5); @even = (2, 4, 6); @numbers = (@odd, @even); print "numbers = @numbers\n"; print "\n";
Hash Variables:
###HASH VARIABLES key/value pairs %data = ('John Paul', 45, 'Lisa', 30, 'Kumar', 40); print "\$data{'John Paul'} = $data{'John Paul'}\n"; print "\$data{'Lisa'} = $data{'Lisa'}\n"; print "\$data{'Kumar'} = $data{'Kumar'}\n"; print "\n";
For clarity, you can use => as an alias for , to indicate the key/value pairs as follows −
%data = ('John Paul' => 45, 'Lisa' => 30, 'Kumar' => 40); %data = (-JohnPaul => 45, -Lisa => 30, -Kumar => 40); print "\n";
Extracting slices:
%data = (-JohnPaul => 45, -Lisa => 30, -Kumar => 40); @array = @data{-JohnPaul, -Lisa}; print "Array : @array\n"; print "\n";
Extracting keys and values:
%data = ('John Paul' => 45, 'Lisa' => 30, 'Kumar' => 40); @names = keys %data; print "$names[0]\n"; print "$names[1]\n"; print "$names[2]\n"; @ages = values %data; print "$ages[0]\n"; print "$ages[1]\n"; print "$ages[2]\n"; print "\n";
Adding/removing elements in Hashes:
%data = ('John Paul' => 45, 'Lisa' => 30, 'Kumar' => 40); @keys = keys %data; $size = @keys; print "1 - Hash size: is $size\n"; # adding an element to the hash; $data{'Ali'} = 55; @keys = keys %data; $size = @keys; print "2 - Hash size: is $size\n"; # delete the same element from the hash; delete $data{'Ali'}; @keys = keys %data; $size = @keys; print "3 - Hash size: is $size\n"; print "\n";
If/else loop and exist command:
%data = ('John Paul' => 45, 'Lisa' => 30, 'Kumar' => 40); if( exists($data{'Lisa'} ) ){ print "Lisa is $data{'Lisa'} years old\n"; } else{ print "I don't know age of Lisa\n"; } print "\n";
Other Conditional operator
$name = "Ali"; $age = 10; $status = ($age > 60 )? "A senior citizen" : "Not a senior citizen"; print "$name is - $status\n"; print "\n";
For loop in Perl
for (my $i=0; $i <= 9; $i++) { print "$i\n"; }
Foreach loop in Perl
foreach my $i (0..9) { print "$i\n"; }
While loop in Perl
$i=0; while ($i<15) { $i++; print "Running while...\n"; print "i = $i\n"; }
Math functions in Perl
use feature 'say'; say "EXP 1 = ", exp 1; say "HEX 10 = ", hex 10; say "OCT 10 = ", oct 10; say "INT 6.45 = ", int(6.45); say "LOG 2 = ", log 2; say "Random between 0-10= ", int(rand(11)); say "SQRT 9= ", sqrt 9; print "\n";
-Utpal Kumar (IES, Academia Sinica)