Tutorial 9 - Perl

26 November 1998


Created 26/11/1998

To recall, we have learned that a computer system can be viewed as a hierarchy with multiple levels. Down at the very bottom is the machine hardware; then the OS; the user command level; and finally at the top, applications.

The following is a view of that hierarchy, presented up-side-down. For each level, there is an interface via which the user or programmer makes use of the "functions" provided by that level. The interface of the machine consists of machine instructions, the interface of the OS is the set of system calls, and so on.

Of special interest to us is the OS interface (i.e., system calls). The system calls can be categorized according to the structure of our textbook. I give examples of system calls belonging to each category below. These are the ones we have seen before or I wanted to explain in class today a bit (but didn't have time).

Machine
=======
machine instructions

OS - System Calls
=================

Process Management
------------------

processes
	fork()
	pipe(), semaphore, shared memory, messages, signal
	exec()
	wait()
	thread (lwp - light weight process)

CPU scheduling
	nice()

deadlocks

Storage Management
------------------

memory management
	brk() (malloc())

virtual memory

file system
	open(), close(), read(), write(), dup(), stat()

I/O Systems
-----------
	ioctl()
	poll()

Distributed Systems
-------------------

Protection and Security
-----------------------
	access(), acl()
	permissions, user, group

Standard Libraries
==================
Language-specific

User Commands
=============
shell, non-shell commands

Programs, Applications
======================
Note that I have included "standard libraries" in the hierarchy, because they represent a vast pool of functions (level-wise, in between user commands and system calls) that user programs can make use of in order to perform certain common tasks. The most well-known one is probably the "C standard library" containing such frequently used functions as fopen(), strcat(), printf(), getc(), etc. There are standard libraries for using sockets (to communicate with another machine), for thread management, etc. You can find a list of these by typing "man -s 3 intro".

Likewise, a list of system calls will be displayed if you type "man -s 2 intro", and "man -s 1 intro" (or just "man intro") to view the list of user commands.

User tasks often require multiple functions which interact. "Languages" are what users and programmers use to glue these functions together to form more complex functions able to handle specific tasks.

At the very top level, applications, the way to glue multiple programs or applications together is via such mechanisms as Microsoft's OLE, and the cut-and-paste function provided in virtually all GUI-based OS's.

At the user command level, shell scripts prevail. And then at the OS level, one has to use a full-feature high-level programming language such as C to make calls to system calls and library functions. C is not so convenient as far as manipulating commands is concerned; you will need to be rather skilled in order to implement something as simple as "p1 | p2" which can be easily done using the "|" symbol in a shell script, not to mention the amount of code you need to churn out.

On the other hand, shell script languages have no support for accessing system calls. It is just not meant to be used for that purpose. Shell script languages are not serious programming languages either: their syntax tends to be rigid, they have no or little typing on variables, some even don't have built-in arithmetic operations, ...

What the Perl inventor, Larry Wall, has done is to merge the two worlds: shell script + a C-like language. Now you can have all the convenience of shell scripts (such as "|", ">", ">>", ` `, ...) and at the same time the ability to access the functions of the OS:

This is why Perl is also known as the "Pathologically Eclectic Rubbish Lister". Larry dumped everything that appeared to be good in shell script languages and C into one big bowl. It is more common known as the "Practical Extraction and Report Language".

Want to get convinced? Check out Why use Perl?

There are numerous online tutorials available on Perl. Here is one by NCSA (where the Mosaic browser was brewed): NCSA Perl Tutorial.

This one contains some interesting Perl resources: CERN Perl Home Page; the shortest Perl program in there that implements the RSA encryption algorithm is a real delight.

To learn Perl, you could buy a book (I prefer the ones by O'Reilly) or try the following at your browser: Perl 5 by Example.

Perl is now famed for its productivity; that is, you can use Perl to solve any problem on hand much faster than using any other language. This is based on an independent study by some research company. Their results show that Perl is indeed a much "higher-level" language than most other common languages in use today.

You can write faster code in C, but you will write code faster in Perl! - Lionel Cons

Finally, this is worth a look too.