O'Reilly logo

Learning Perl, Second Edition by Randal L. Schwartz, Tom Christiansen

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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!

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required