My first Perl program

From BITS wiki
Jump to: navigation, search
Go back to Perl introductionary training#Exercises

We will start with a simple (but potentially useful) program los.pl that just counts the number of characters in a string of text (los could be an acronym for "length of string"). We will improve it bit by bit. First, start Notepad and write the following text into a new file:

$string = qwerty;
$length = length $string;
print $length;

Some explanation : the first line of the program puts the string of characters qwerty in the variable string. Perl has only one type of simple variable and stores all data as text strings. If needed for the purpose of computation, text data can on-the-fly be transformed into numerics. The name of a Perl variable is preceded by a dollar '$' character to indicate that it is a variable, so that there is no risk that $string can interfere with a word string belonging to the Perl language (if it existed).

The second line uses the Perl built-in function length, which returns the number of characters in a text string. Note by the way that you could also have written :

$length = length($string);

A property of Perl is that it has a very flexible syntax. This allows the programmer to use his favourite style, but it is sometimes a source of pitfalls (so, beware !).

The third line uses the Perl function print. If you do not precise where to print and your program runs under Windows in a DOS box or under UNIX/Linux/MacOS X in a terminal window the output will probably just appear on the screen, but it might also go to a dedicated window or get into a "log" file.

Note that each line ends with a semicolon ';'. Perl uses semicolons to separate statements, contrary to some programming languages that put each statement on a separate line and use the end-of-line as separator. So, you could as well have written :

$string = qwerty; $length = length $string; print $length;
Error creating thumbnail: Unable to save thumbnail to destination

When you have finished writing the text, save it in the directory/folder C:\Users\BITS\My Documents\Perl under the name los.pl (note : you will have to set the "Save as type:" selector to "All files" to prevent the Notepad from appending .txt to the name). Now we must run the program to see the result. Do this by typing in the DOS box the command :

perl los.pl
Error creating thumbnail: Unable to save thumbnail to destination

It is not impossible that you might have some trouble in seeing the result (6). The reason is that the program prints the number 6 without having it followed by a newline character, which is invisible but makes the display/printer move to the begin of the next line. Under Windows however the DOS command interpreter usually does append a newline after the output. No matter what, modify the third line, so that it becomes :

print "The length is $length\n";

and convince yourself that you now do get an extra newline. Some explanation : Perl knows about "interpreted strings", which are delimited by double quotes ("). Inside an interpreted string you can among other things use the following :

	$xxx	will be replaced by the value of variable xxx
	${xxx}	idem (use this if the Perl interpreter cannot easily see where the
         	name xxx ends because it is followed immediately by an alphanumeric)
	\n	a newline
	\t	a tabulation mark
	\"	use this for a " inside the string, so that it is not confused with the
                terminating "
	\\	use this for a \ inside the string

Perl has also simple strings, delimited by single quotes ('). You can replace the first line by :

$string = 'qwerty $asd';

This will make sure that qwerty and $asd and the intermediate blank space are taken as one string rather than as two separate strings. Note by the way that $string = qwerty and $string = 'qwerty' are equivalent, since the Perl interpreter can infer that qwerty must be a string ; but the statement $x = 3 + 5 will put in variable $x the value 8 (since 3 + 5 is interpreted as an expression to be evaluated), while $x = '3 + 5' will put the value 3 + 5.

You have now seen that you need no more than a simple text editor to write the Perl program and the perl command to run it. It is however possible to work under more comfortable conditions. Start Padre ; you can do that by simply typing padre in the DOS box, or by looking for "Padre, the Perl IDE" in the start menu of Windows. Do "File"/"Open" to call a file browser and open the file C:\Users\BITS\My Documents\Perl\los.pl. You will see that the text of the program appears in the box at the left and is automatically "colourized", what makes reading the program easier. To run the program, do "Run"/"Run Script". You will see that a new window pops up, which contains the output that would have appeared had the program been run in a DOS box.

Padre.png

A final improvement to our program is to make that you can feed the string to be counted from the outside, rather than having to write it in the program itself. Replace in Padre the first line of the program by :

$string = $ARGV[0];

and do "File"/"Save". If you want to run a Perl program with arguments added you cannot do it simply with "Run Script". You can in the DOS box type the commands :

perl los.pl bla
perl los.pl "bla bla"

You can do everything from Padre by running Perl as an external command, but first you must tell Padre where your working folder is. Do "Tools"/"Preferences"/"Behaviour" to call a window, in which you will see a box "Default Project Directory". The box probably contains a text C:\Users\BITS\My Documents. You must change this into C:\Users\BITS\My Documents\Perl. You can do this by typing or using the "Browse" button. Then you can do "Run"/"Run Command", type into the "Command-line" box perl los.pl bla and click "OK". You should again see the pop-up window with the result.

Some explanation : Perl has "arrays", that are lists of simple variables, where each individual variable can be accessed by its position number. So, you have :

	@xxx	     the array with name xxx (will not be confused with a simple
                     variable $xxx, if this exists)
	$xxx[0]	     the first element of @xxx (Perl has the unfortunate habit,
 	             inherited from the language C, of starting to count from 0)
	$xxx[n]	     element number n of @xxx (is actually the n+1th element)
	$xxx[$#xxx]  the last element of @xxx

Note that Perl arrays are extensible ; if you put $proteases[7] = papain the element number 7 of the array @proteases will get value papain and elements from 0 to 6, in so far that they did not exist yet, will magically pop into existence with as value a 'NULL' string. You can attribute values to several elements at the same time with a statement of type :

@proteases = ('trypsin', 'chymotrypsin', 'pepsin')

The array @ARGV is special ; when the program starts it pops automatically into existence and contains the words that follow the name of the program on the command line.

You can practice a little bit with the concepts you just learned by writing a new program (you give it the name you like ; if you use Padre, do not forget to do "File"/"New") :

print 10 * 5; print "\n";
@proteases = ('trypsin', 'chymotrypsin', 'pepsin');
print $proteases[1];

You should get as output "50" and "chymotrypsin". Take some time (but not too much) by experimenting with writing Perl code.

To comply with "good practice" you can add on top of the program the so-called "shebang" line

#!/usr/bin/perl

Under Windows this line is not used but under UNIX/Linux/MacOS X it makes sure that when you declare the file "executable" and then just type its name the "shell" knows that it must use /usr/bin/perl as interpreter to run the script in the file. We will in the rest of this exercise sheet always invite you to type it.

Go back to Perl introductionary training#Exercises