Overview
In this example, you will learn how to write a simple program that will print out values stored in a variable.
Example
Output
Hello, Genomics!Decode
Open SubEthaEdit, go to Mode>Switch Mode> , then select "perl". This tells the editor that you are writing perl code.
Go to Mode and select "Insert SheBang". This will give you line 1.
>>Line 1: #/usr/bin/env perl
For every perl program you write, the first line needs to be "#/usr/bin/env perl" This line tells your machine to run the file through perl. You can manually write it down. Or you can selet "Insert SheBang" from "Mode" in the SubEthaEdit Editor.
>>Line 3,5,8, 11: # write anything here
Everything you write after "#" will be ignored by perl. It is always a good programming practice to write comments to explain what your code is doing.
>>Line 6: my $greeting;
$greeting is a variable. A variable is just a space holder. It starts with the "$" sign and a variable name such as $greeting, $sequence, $myDNA etc. If you variable name is more than one word, it is a good programming practice to capitalize the first letter of the words after the first word. Eg, $thisVariableNameIsTooLong . You can create a variable by declaring it using "my" command."my $greeting;" creates a variable called $greeting. Every perl command ends with a semicolon (;) .
>> Line 9: $greeting="Hello, Genomics!";
"=" is an assignment operator. It puts the value on the right ("Hello, Genomics!") into the variable $greeting. The varialbe can be an integer, a string or a Boolean statement (true or false).
-Assign a string into a variable, surround the characters with double or sigle quote:
$sequence="ACGT" ; Or $sequence='ACGT';
-Assign an integer or any numerical values into a variable:
$count= 5;
$percentage= 0.53;
-Assign a boolean value:
$found= true;
$found=false;
>> Line 12: print "$greeting \n";
This statement prints the value stored in $greeting. (print "$greeting \n";) will ouput "Hello, Genomics! ".
"\n" is next line. For instance, (print "$greeting $greeting" ;) will output "Hello, Genomics! Hello, Genomics!" all in one line.
(print "$greeting \n $greeting" ;) will produce two lines:
Hello, Genomics!
Hello, Genomics!
Save the program first. Before you run the program, go the Mode, then select "chmod ug+x" . If you forget to do this when you run the program, you will get a "permission denied" error.
Click "run" (located on the top right corner) to run the program.
Exercise One
>> Write a program that creates a variable called $sequence, assign values to $sequence and print it out.