Managing Files

In addition to simply reading from and writing to files, you can also manage them from inside Perl, just as you might from a command line or in a file manager: you can rename them, delete them, change their permissions, or create links to them. Each of these tasks makes use of several Perl built-in functions. In this section, you get an overview of each of these.

Renaming Files

To rename a file from one name to another, keeping its contents intact, use the rename function with two arguments: the old name of the file and the new name:

rename myfile, myfile.bak;

If a file with the same name as the new file already exists, Perl will overwrite it. If you're used to Unix, this won't be a problem. If you're a Windows or Mac user, beware! ...

Get Sams Teach Yourself Perl in 21 Days, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.