Learning Perl

Learning PerlSearch this book
Previous: 13.3 Creating Alternate Names for a File: LinkingChapter 13
File and Directory Manipulation
Next: 13.5 Modifying Permissions
 

13.4 Making and Removing Directories

You probably couldn't have made it this far (on a UNIX system, anyway) without knowing about the mkdir (1) command, which makes directories that hold other filenames and other directories. Perl's equivalent is the mkdir function, which takes a name for a new directory and a mode that will affect the permissions of the created directory. The mode is specified as a number interpreted in internal permissions format. If you're not familiar with internal permissions, see chmod (2). If you're in a hurry, just say 0777 for the mode and everything will work.[2] Here's an example of how to create a directory named gravelpit:

[2] You aren't making a directory with wide-open permissions. Your process's current umask will also help determine the permissions. On UNIX systems, see the shell's umask command or umask (2).

mkdir("gravelpit",0777) || die "cannot mkdir gravelpit: $!";

The UNIX rmdir (1) command removes empty directories; you'll find a Perl equivalent with the same name. Here's how to make Fred unemployed:

rmdir("gravelpit") || die "cannot rmdir gravelpit: $!";

Although these Perl operators take advantage of the same-named system calls, they'll work even on systems without those system calls (albeit a bit slower). Perl calls the mkdir and rmdir utilities automatically for you (or whatever they're called on your system). Strike one blow in the name of portability!


Previous: 13.3 Creating Alternate Names for a File: LinkingLearning PerlNext: 13.5 Modifying Permissions
13.3 Creating Alternate Names for a File: LinkingBook Index13.5 Modifying Permissions