File Tests

Opening files for reading and writing is all well and good when you know something about the files you're working with—for example, that they're all there, or that you're not about to overwrite something important. But sometimes in Perl you want to be able to check the properties of a file before you open it, or handle a file differently depending on the various properties of that file.

Perl has a (rather large) set of tests for the various properties of files: to see if they exist, that they have data in them, that they're a certain kind of file, or that it's a certain age. (1996 was a very good year for binary files, wasn't it?) These tests all look like switches (-e, -R, -o, and so on), but don't confuse them with actual switches ...

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.