2

Perl Second Idiom

So far we have learned how to tell our shell environment that the Perl interpreter is to execute our script., but we have not instructed Perl to perform any tasks for us.

#!/usr/bin/perl
print "I am alive";

When creating this single line program in a text editor such as vi, we save the file on our hard drive. In order to for the OS to understand that this file needs to be executed in some fashion, the OS has to be told that this program is executable. In Unix, we set the permissions of the file to executable.

ruben@ruben:/home/ruben/perl_course > ls -al
total 4
drwxr-xr-x 2 ruben users 1024 Oct 19 17:39 .
drwxr-xr-x 29 ruben users 3072 Oct 19 17:39 ..
-rw-r--r-- 1 ruben users 0 Oct 19 17:39 file1
ruben@ruben:/home/ruben/perl_course >

This command in Unix (ls -al) lists all the files in the directory in long format. The first column of information that you see is the file permissions. The second column is the owner of the file; the third is the group who owns the file, the forth is the size of the file. The 5th is the date the file was changed, and the last is the file name.

-rw-r--r-- 1 ruben users 0 Oct 19 17:39 file1

This means that file1 is owned by ruben, and group users. It was changed on Oct 19th at 5:19PM, and has no bytes of information within it. The owner can read and write to it. Users in the group (users) and everyone else can read it.

The first space in the first column is the file type code. File .. or . represent the directory above and the current directory. The d in the first slot is for directory. file1 has dash (-). This means it is a regular file. The next three spaces determine the rights for the user of the file. rw means the owner can read right. It can also say rwx, which means that the file is read, write execute. The next 3 spaces are permissions for the group. Users assigned to the group users can read this file. The last 3 spaces indicate the permissions of everyone else.

Too change file1 to owner execute, we can give the following command:

The command which changes file permissions is called chmod. chmod takes 2 arguments: File permission and file name. The permissions can be represented by a series of 3 integers.

ruben@ruben:/home/ruben/perl_course > chmod 100 file1
ruben@ruben:/home/ruben/perl_course > ls -al
total 4
drwxr-xr-x 2 ruben users 1024 Oct 19 17:39 .
drwxr-xr-x 29 ruben users 3072 Oct 19 17:50 ..
---x------ 1 ruben users 0 Oct 19 17:39 file1

This command gives ruben execute permission. 1 represents execute.

ruben@ruben:/home/ruben/perl_course > chmod 200 file1
ruben@ruben:/home/ruben/perl_course > ls -al
total 4
drwxr-xr-x 2 ruben users 1024 Oct 19 17:39 .
drwxr-xr-x 29 ruben users 3072 Oct 19 17:50 ..
--w------- 1 ruben users 0 Oct 19 17:39 file1

2 represents write permissions......

ruben@ruben:/home/ruben/perl_course > chmod 400 file1
ruben@ruben:/home/ruben/perl_course > ls -al
total 4
drwxr-xr-x 2 ruben users 1024 Oct 19 17:39 .
drwxr-xr-x 29 ruben users 3072 Oct 19 17:50 ..
-r-------- 1 ruben users 0 Oct 19 17:39 file1

4 represents read permissions....

Adding these combinations summed together creates combined permissions

ruben@ruben:/home/ruben/perl_course > 744 file1
bash: 744: command not found
ruben@ruben:/home/ruben/perl_course > chmod 744 file1
ruben@ruben:/home/ruben/perl_course > ls -al
total 4
drwxr-xr-x 2 ruben users 1024 Oct 19 17:39 .
drwxr-xr-x 29 ruben users 3072 Oct 19 17:50 ..
-rwxr--r-- 1 ruben users 0 Oct 19 17:39 file1
ruben@ruben:/home/ruben/perl_course >

Here, ruben is given read, write and execute(1+2+4 = 7), users read (4), and all read permissions.

We see that file1 has no data within it. We can change this with out text editor, vi -

ruben@ruben:/home/ruben/perl_course >
vi file1
ruben@ruben:/home/ruben/perl_course >

VI opens the file and waits for a command. You are not given a cursor as in many editors you are familiar with. Common commands in vi are:

(i) for input

(x) for deleting a character

(a) for appending to the end of the file

(o) opening a new line.

The commands i, o, or a puts the editor into edit mode. You get a cursor and can type into the file. When done editing hit escape.

It is important to understand how to use the vi editor. Vi was a natural outgrowth of Unix. It was an extention of the original editor called ed. Ed was Unixes line editor. It took many commands to help the user manipulate files. At the time it did many advanced tings like letting a user search through a file and make global changes in it using regular expressions. Out of ed came, grep, sed and later awk. The development of regular expressions continued with these new tools, and eventually Perl inherited the mantle from awk. Learning to use VI teaches many important programing lessons for Perl. Take the time out now to do aVI tutorial on the internet. Learn how to substitute text in a file opened by VI using a regular expression.

ESC brings you out of edit mode, back in command mode. In command mode you can run a regular expression substitution like this:

:1,$/ruben/ellen/g

This changes the pattern ruben for pattern ellen everywhere it appears in the opened file.

Open file1 and insert the code

#!/usr/bin/perl

print "I am alive\n";
Your vi editor might tell you that your in insert mode or command mode in the lower left corner of the screen. If not, try: <ESC> :set number on :set showmode on Now save your file <ESC> !wq file1

Vi closes and you are returned to your command prompt.

Make sure the file is set too executable. Use the command ls -al. If everything is fine, run your program.

ruben@ruben:/home/ruben/perl_course > file1
I am alive
ruben@ruben:/home/ruben/perl_course >

This simple 2 line program demonstrates a number of common Perl characteristics.

1. All perl commands other that #! get a semicolon at the end of the statement.

2. Perl supports a number of quoting mechanisms. The most common is the double quotes. Certain character combinations become transformed in double quotes. One example of this is the 2 character sequence "\n".

Try this:

#!/usr/bin/perl
print "There is no linefeed here";
print "But here is a \\n \n";
print "But not here ==>";


ruben@ruben:/home/ruben/perl_course > vi file2

ruben@ruben:/home/ruben/perl_course > chmod 744 file2
ruben@ruben:/home/ruben/perl_course > file2
There is no linefeed hereBut here is a \n
But not here ==>ruben@ruben:/home/ruben/perl_course >

Obviously \n means something.

Can you determine its meaning?

3. Perl has built in Keywords.

One keyword is print. In its simplest form, print sends a string of characters to standard output.

Standard Output is a software routine which handles a hardware interface. It receives code and does something useful with it. By default, bytes of code are transmitted through a physical port, through the cable, and to your monitor where it is interpreted as English letters printed on your screen (the standard output device). The print on the screen is something useful to the end user.

If a Perl program runs as a cgi on a web server, the standard output is sent through the modem or the ethernet card, through the internet, to your web browser, and interpreted by the web browser on the otherside.

When sent to your screen, STDOUT is a stream of bytes representing ASCII code. When sent to a Web Browser, the STDOUT can be anything from ASCII, HTML, graphics, sound or even video. The browser figures out how to properly handle the servers STDOUT. A web servers STDOUT is a web browsers STDIN. Together they create a socket. The browser takes that STDIN and responds by sending even more complex output to your screen. This process can be described as a client/server relationship.

Third Perl Idiom