A Perl Program
There are two main steps in running a Perl program:
- Create a text file with the Perl commands you wish to carry out,
- Run this file through the Perl interpreter,
It must be stressed that this file is a text (ascii) file. Word processors such as Microsoft Word are inconvenient to use for this purpose, as by default they save files in their own (binary) format. Life will be easier in your programming career if you start off with a text-only editor, such as emacs or xemacs.A simple Perl program that just prints out something to the terminal screen is
# file hello.pl (this is a comment)
print "Hello, world!n";
Note the trailing semi-colon at the end – this indicates to Perl the end of the command. Anything after the “#” sign is treated as a comment by the interpreter, and thus ignored. Generally spaces or line breaks are ignored by the interpreter – the above program could also be written as
print
"Hello, world!n" ;
although for human eyes this is less readable.After creating this file, save it as, for example, hello.pl, and run it through the Perl interpreter (the “pl” extension is used in some contexts to indicate the type of file). Running it through the interpreter can be done through a terminal window as
C:> perl hello.pl
This assumes that perl is in your PATH environment variable.On Unix there is a shortcut you can use to run such programs. Modify the above file so that it includes a shebang line as the first line:
#!/usr/bin/perl
print "Hello, world!n";
This line will indicate to the shell that the program found as /usr/bin/perl will be used to carry out the commands that follow. On Unix it is often customary to name such files without an extension, so save this file, for example, as simply hello. After this, you can make the file executable by
bash$ chmod u+x hello
You can then run the file as
bash$ hello
If the current directory is not in your PATH environment variable, you would have to run this as
bash$ ./hello
Programming Elements
There are five basic elements of programming that are present in essentially all languages.
- Variables: This will how data is represented. It can range from something very simple, such as the age of a person, to something very complex, such as a record of university students holding their names, ages, addresses, what courses they have taken, and the marks obtained.
- Loops: This will allow us to carry out execution of a group of commands a certain number of times.
- Conditionals: This will specify execution of a group of statements depending on whether or not some condition is satisfied.
- Input/Output: This will allow interaction of the program with external entities. This might be as simple as printing something out to the terminal screen, or capturing some text the user types on the keyboard, or it can involve reading and/or writing to files.
- Subroutines and functions: This will allow you to put oft-used snippets of code into one location which can then be used over and over again.
These notes are not meant to be an all-inclusive reference guide to Perl. You should become very familiar with the copious documentation that comes with Perl – on Windows, see the HTML links under the ActivePerl entry in the Start menu. You can also access the documentation within a terminal window via perldoc:
C:> perldoc perl # brings up a table of contents
C:> perldoc perlintro # introduction to Perl
C:> perldoc -f rand # quick help on using the rand function
C:> perldoc File::Copy # help on using the File::Copy module
Related Posts: