Wednesday, 16 February 2011


# Hello world in perl
print "Hello World!\n";

#[This example does not provide the UNIX file directory path #which is usr/bin. The \n is also used in C and Java; without it #the prompt would remain on the same line]
#ActivePerl from ActiveState is probably now the most the #industry-standard, commercial-grade Perl distribution, 
#available for Solaris, AIX and HP-UX or indeed the Enterprise #Edition. I would actually rely on ActivePerl to protect my #infrastructure and stay competitive with quality-assured #business solutions. Since it is compatible with the reference #distribution of Perl, the Code tested will run on any Perl #installation that has the extensions installed.

#Perl adapts the syntax of the C language including the use of semicolons at
the end of each statement and the use of curly brackets to identify a block of

$Name = “James “ . “Snyder”;

#the concatenation operator takes the string “James “ and combines it with the second string “Snyder” to create a single
string that contains “James Snyder”.

# In Perl 5.8, the default sorting algorithm is merge sort.
#In earlier versions of Perl, the default sorting algorithm was quick sort.

if $x > 5
#To identify a block of commands in Perl, use curly brackets

#Perl allows variables to hold any data types.
variable names begin with the dollar sign symbol
Perl is case-sensitive, so the Perl variable $myVar is considered completely different from $MYVar

< = >
#a Perl only relational operator that compares two values and returns a True or False value

== # The relational operator in Perl is two equal sign symbols (==) whereas the relational operator in other programming languages is just a single equal sign symbol (=).

#this is a unique comparison with signed result operator (< = >)
#this compares two values
#this returns 0 (if the two values are equal),
#thias returns 1 (if the first value is greater than the second)
#this returns  –1 (if the first value is less than the second)

# This Logical operator compares two Boolean values
# compares (True [1] or False [0]) and return a single True or False value to a truth table
#Perl has a special increment (++)  operator, which simply adds to a variable

#Perl has a special decrement (--) operator, which simply subtracts 1 to a variable

#Addition assignment operator

# Subtraction assignment operator

if (condition) {
#branching statement
#The simplest branching statement is an IF statement that only runs one or more commands if a Boolean condition is True. #In Perl, the IF statement uses curly brackets to enclose one or more commands

if (condition) {
else {
#make the computer choose between two mutually exclusive sets of commands
#use an IF-ELSE statement in Perl

if (condition1) {
elsif (condition2) {
elsif (condition3) {
#The IF-ELSE statement only offers two choices.
#If you want to offer multiple choices, use the IF-ELSEIF statement
#IF-ELSEIF uses two or more Boolean conditions to choose which of two or more groups of commands to run.
#use the ELSIF keyword

#Perl does not provide a SWITCH statement.

sub functionname {
return $value;
# In Perl, every subprogram is a function that can return a value.
# This is the format of a typical Perl function

sub functionname {
foreach $variablename (@_) {
return $value;
#When you pass parameters to a Perl function,
#that function can access parameters with the foreach keyword and the @_ array

@arrayname = (element1, element2, element3);
# Perl offers three data structures: arrays, hash arrays, and references
#. An array stores multiple items, identified by an index number.
#A hash array stores multiple items, identified by a key, which can be a number or a string.
#Perl arrays are zero-based, so the first element of an array is considered 0, the second is 1, and so on.
#,you must name that array with the @ symbol when you create a Perl array .
#You can define the elements of an array at the time you create the array

@numberarray = (1, 2, 3, 4, 5);
# you can list each number individually if you want to create an array that contains a range of numbers

@numberarray = (1..5);
# use the range operator (..) to define the lower and upper bounds of a range

@numberarray = (1..10);
$thisone = $numberarray[0];
# use the dollar sign ($) symbol in front of the array name to access the individual elements stored in an array
#The value stored in the $thisone variable is the first element of the @numberarray, which is 1.

push(@arrayname, item2add);
#use arrays to mimic a stack data structure with Perl’s push and pop commands.
# use the push command to push a new item onto an array.

$variablename = pop(@arrayname);
# use the pop command to pop an item off the array

%hasharray = (
key1 => value1,
key2 => value2,
key3 => value3,
# This is a Perl hash array
# A hash array stores an item along with a key.
#there are two ways to store values and keys; this is the first way
# hash arrays are identified by the percentage (%) symbol.

%hasharray = (“key1”, value1, “key2”, value2, “key3”,
# A second way to define a hash array

$variable = $hasharray (“key1”);
# you need to know the key associated with that value
#identify the hash array name by using the $ symbol
#This command would store the value associated with

package className;
sub new {
my $objectname = {
bless $objectname, $className;
return $objectname;
sub methodname{
# Perl is a true object-oriented programming language.
# create and use objects in your programs.
# define a class to create an object
# This is a typical class definition

my $variablename = classname->new();
# After i define a class, i can create an object from that class by declaringconstructor method commonly called "new"
a variable as a new class type.
# I create an object by creating a

package newobject;
use class2inheritfrom;
@ISA = qw(class2inheritfrom);
# use the @ISA variable inside a new class to use inheritance in Perl

$DNA3 = “$DNA1$DNA2”;
print $DNA3;
# concatenation is the simplest form of string manipulation
#concatenation joins multiple strings into one.
“key1” into the $variable.
# This is what this simple program will print

$compDNA = tr/ACGT/TGCA/;
# The
# The tr/ACGT/TGCA/; command tells Perl to translate every A into a T, every C into a G, every G into a C, and every A into a T all at once.The two strands of DNA are complimentary sequences.
use a simple program that replaces every A with a T, every C with a G, every T with
an A, and every G with a C.
# This is to calculate a complimentary sequence by knowing only one of the sequences
tr command simply tells Perl to translate or swap one character for another.

$compDNA = tr/ACGT/TGCA/;
$revDNA = reverse $compDNA;
# The second step in determining a complimentary sequence is to reverse the
order of that sequence.
# sequences are always written a specific way
# sequences start with the end of the sequence known as 5’ phosphoryl (also
known as 5 prime or 5’) and ending with 3’ hydroxyl (known as 3 prime or
# reverse it using this Perl command to display the complimentary sequence correctly

$DNA3 = “$DNA1$DNA2”;#
The first method  to concatenate two strings is to smash two strings together

$DNA3 = $DNA1 . $DNA2;
# The second way to concatenate the same two strings uses this dot operator.

No comments: