Äîęóěĺíňŕöč˙
HTML CSS PHP PERL äđóăîĺ

Unix Stuff

 
Previous Table of Contents Next

Unix Stuff

This portion of the hour is primarily for the users of Perl who are working under the Unix operating system. If you don't use Perl on a Unix system, you can safely skip this section and not miss anything important; you can read it if you're curious about Unix, however.

Unix users should know that Perl has a deep Unix heritage, and some Perl functions come right from Unix commands and operating system functions. Most of these functions you will not use. Some functions—like unlink—come from Unix but have meanings that are not really tied to Unix at all. Every operating system deletes files, and Perl ensures that unlink does the right thing on every operating system. Perl makes a great effort to ensure that concepts—such as file I/O—that should be portable between operating systems actually are portable, and it hides all the compatibility issues from you if it can.

The fact that many Unix functions and commands are embedded in the Perl language—which has been ported to many non-Unix operating systems—is a tribute to the desire of Unix developers and administrators to take a little bit of the Unix toolkit with them wherever they go.

By the Way

As the next section header suggests, this description is not to be taken as a full explanation of Unix file system permissions and how to manipulate them. For a full explanation, consult your operating system's documentation or any good book on Unix, such as Sams Teach Yourself Unix in 24 Hours.


A Crash Course in File Permissions

In Hour 1, to make a Perl program run as though it were a regular command, you were given the command chmod 755 scriptname without any real explanation of what it meant. First of all, chmod is a command in Unix that sets the permissions on files. Next, the 755 is a description of the permissions being given to the file scriptname. Each of the three digits represents one set of permissions, given to the owner of the file, the group that the file belongs to, and any nonowner and nongroup—called other—user. In this case, the owner has permission 7, whereas the group and others have permission 5, as shown in Figure 10.1.

Figure 10.1. Breakdown of Unix permission bits.


Table 10.2 lists the possible values of the digit for each possible combination of permissions.

Table 10.2. File Permissions

Permission

Allows That Permission Group

7

Reading, writing, and executing the file

6

Reading and writing the file

5

Reading and executing the file

4

Reading the file only

3

Writing and executing the file

2

Writing the file only

1

Executing the file only

0

No access to the file


To set permissions on a file in Perl, you use the built-in function called chmod:


chmod mode, list_of_files;


The chmod function changes the permission on all the files in list_of_files and returns the number of files whose permissions were changed. The mode must be a four-digit number beginning with 0 (because it's a literal octal number, as mentioned in Hour 2 and later in this hour) followed by the digits that you want to indicate permission. The following are some examples of chmod. Note that R stands for read permission, W for write permission, and X for execute permission:


chmod 0755, 'file.pl';     # Grants RWX to owner, RX to group and non-owners

chmod 0644, 'mydata.txt';  # Grants RW to owner, and R to group and non-owners

chmod 0777, 'script.pl';   # Grants RWX to everyone (usually, not very smart)

chmod 0000, 'cia.dat';     # Nobody can do anything with this file


Earlier this hour, you learned about the mkdir function. The first argument of mkdir is a file permission—the same kind that chmod uses:


mkdir "/usr/tmp", 0777;  # Publicly readable/writable/executable

mkdir "myfiles", 0700;   # A very private directory


By the Way

The permissions on a UNIX file are often called its mode. So chmod is simply short for "change mode."


    Previous Table of Contents Next
    © 2000- NIV