Being able to store DNA and RNA in Perl programs is an essential skill needed for more advanced programming skills, such as transcription of a DNA strand. Below is a program that stores DNA and RNA and prints them out accordingly. First, type the program in your favorite text editor and run it to get a feel of the structure of the program. Then, examine the syntax closely, line by line, with the explanations shown below.
# This program shows how to store DNA and RNA into variables and then print them out.
# The RNA is first stored in the variable $RNA.
$RNA = ‘AUGCU’;
# The DNA is stored in the variable $DNA.
$DNA = ‘AGTCT’;
# Now it is time to print the DNA and RNA onto the screen
print “This is the RNA strand: “;
print “$RNA “;
print “This is the DNA strand : “;
print “$DNA “;
This is the RNA strand:
This is the DNA strand:
Explanation for Program
Line #1 – #!/usr/bin/perl -w
This tells Unix that the program is a Perl program. All operating systems check this line for certain commands at the end of the line. For example, -w is at the end of this line. This command tells Perl that there should be warning reporting.
Line #2/Line #3/Line #5/Line #7 – Pseudocode
Pseudocode does not follow traditional synax. Pseudocode always begins with the pound sign (#). The pound sign alerts Perl to ignore that line of code. Pseudocode usually describes the function of the program.
Line #4 – $RNA = ‘AUGCU’;
The entire line in line #4 is a statement. Statements always end in a semicolon (;), which is the equivalence of the period in English. There are many types of statements according to the function of the program.
$RNA is considered to be a variable. There are different types of variables you will experience in Perl. $RNA is a scalar variable. A scalar variable only holds one line of data. A variable can be any name beginning with the dollar sign ($) (ex. $Joe or $be_happy). Uppercase and lowercase letters and digits can be used. However, the first letter cannot be a digit. The underscore is used as spaces between words. It doesn’t matter what name you assign a variable as long as it begins with a dollar sign. Most programers assign a variable name that is appropriate for the function of the program.
This part of line #2 is a string. A string is always surrounded by quotes.
Assignment in Line #4
The equal sign (=) is used to assign the string to a variable. The equal sign is referred to as an assignment variable. In line #4, ‘AUGCU’ is assigned to $RNA. This causes the sequence AUGCU to be stored in the $RNA variable. Assignment must always be in the “variable = ‘string’;” order.
Line #6 – $DNA = ‘AGTCT’;
Again, the whole line of this code is a statement. Notice that it ends in a semicolon. $DNA is the variable, and ‘AGTCT’ is the string. $DNA is assigned to ‘AGTCT’ through the equal sign (=).
Line #8 – print “This is the RNA strand: “;
“This is the RNA strand: “;
This is a string. All the strings before this have been designated by single quotes (‘ ‘). However, strings can also be designated by double quotes (” “), as shown above.
This is one of the many Perl commands. This command instructs Perl to print the string.
This is called the newline. This tells Perl to start the next line.
Line #9 – print “$RNA “;
This command tells the computer to print the variable $RNA.
This string stores the variable $RNA. As you noticed, Perl printed AUGCU, not $RNA. This is because $RNA represents AUGCU, since AUGCU is assigned to $RNA, as explained earlier. The characters tell Perl to go to the next line, as shown above.
Line #10 – print “This is the DNA strand : “;
This statement tells Perl to print whatever is stored in the variable $DNA, as shown above.
Line #11 – print “$DNA “;
This statement tells Perl to print whatever is stored in variable $DNA, as shown above.
Line #12 – exit;
The exit command tells Perl to exit the program.
Tisdall, James. Beginning Perl for Bioinformatics. 1st ed. Sebastopol, California: O’Reilly Media, Inc., 2001. Print.