Let's look at a couple of examples. Here is the classic 'Hello World' in Perl:
On UNIX save the file as hello.pl and make it
executable with the command chmod +x hello.pl. The
program can be run with the command
> hello.pl
If you have Linux it is probably as easy as that because Perl is pre-installed with most Linux distributions and the first line of the Perl program tells the Linux shell how to invoke the Perl interpreter. On Windows, you need to download a Perl interpreter. The O'Reilly Perl site14 has links to downloads for Windows and UNIX. On Windows I used the ActiveState ActivePerl interpreter, which is freely available but not open source. On Windows, you can invoke the Hello World program in exactly the same way if you use a .pl extension and that is registered to invoke the Perl interpreter. Otherwise, use
> perl hello.pl
Here is a program to validate that all the characters in a given string are valid DNA symbols. The program makes use of Perl regular expression to ensure that the DNA characters input from the command line all belong to the set {'a', 't', 'c', 'g', 'A', 'T', 'C', 'G'}.
The my keyword defines a variable. Its use is
optional but improves readability. The built-in variable $ARGV[0]
access the first command line argument. The program can be tested
from the command line using the example
input
> perl validate_dna.pl atcgNSTOP
This gives the output
Invalid DNA symbol found: N
Validation of input data is a very important consideration when creating web user interfaces for security reasons.
Here is an example that demonstrates the foreach language construct to iterate over a list of codons:
The program uses the quoted words (qw) shortcut to
create the array @codonList. Notice the use of the @
symbol when referring to the array. The foreach keyword
enables iteration over the array @codonList placing the
value in the variable $codon with each iteration.
Executing the script leads to the output
Here is an example that transcribes a DNA sequence to RNA using a regular expression.
This demonstrates the use of the regular expression assignment operator =~, which evaluates the regular expression substitution operation and assigns the output to the left hand side. The 's' in the expression stands for 'substitute', as in the UNIX command. Running this script will give the following output:
Here is an example program that performs the RNA to amino acid
translation as discussed in the biology background section. It
demonstrates use of a hash table (%rna_to_amino_acid).
The curly brackets {} indicate that the inside is to be used as a key to look up the value in the given hash table. The output is the same as the example output from the Biology Background section above:
>perl rna_translation.pl
Amino acid string MAQALLVPPGPESFRLFTR
Perl makes the concept of references explicit. References are
indicated with a '\' symbol. For example, in the following script \@symbols
is a reference, or pointer, to the array symbols. Braces
(or curly brackets {}) deference a variable so that @{$inputSymbols}
is the array again.
The array has been passed into the subroutine by reference, as opposed
to by value, which involves making a copy.
This example also introduced the use of Perl subroutines, which are
the equivalent of functions in other languages, such as C. The
keyword sub is used to define a subroutine. The
last statement in a subroutine defines the optional return value,
although that was not used in the example above. The subroutine
parameters are passed in the variable @_, which is
accessed implicitly with the shift function above. The shift
function returns the first member of the array argument and deletes it
from (shifts it off) the array. Running the program gives the following
output:
There are no user comments.
Please send ideas and opinions by email at alexamies@gmail.com.