Perl Programming Page 6 - File Tests in Perl |
Every time you use stat, lstat, or a file test in a program, Perl has to go out to the system to ask for a stat buffer on the file (that is, the return buffer from the stat system call). That means if you want to know if a file is readable and writable, you’ll ask the system twice for the same information, which isn’t likely to change in a nonhostile environment. This looks like a waste of time,* and can be avoided. Doing a file test,stat, orlstaton the special_filehandle (the operand being a single underscore) tells Perl to use whatever happens to be lounging around in memory from the previous file test,stat, orlstatfunction, rather than going out to the operating system again. Sometimes this is dangerous: a subroutine call can invokestatwithout your knowledge, blowing your buffer away. If you’re careful, you can save yourself a few unneeded system calls, thereby making your program faster. Here’s that example of finding files to put on the backup tapes again, using the new tricks you’ve learned: my @original_files = qw/ fred barney betty wilma pebbles dino bamm-bamm /; We used the default of$_for the first test; this is as more efficient (except perhaps for the programmer), and it gets the data from the operating system. The second test uses the magic_filehandle. For this test, the data left around after getting the file’s size are used, which are what we want. Testing the_filehandle is different from allowing the operand of a file test,stat,orlstatto default to testing$_. Using$_would be a fresh test each time on the current file named by the contents of$_, but using_saves the trouble of calling the system again. Here is another case where similar names were chosen for radically different functions. ExercisesSee Appendix Q for answers to the following exercises:
* It’s more likely that, instead of having the list of files in an array as our example shows, you’ll read it directly * The -o and -O tests relate only to the user ID and not to the group ID. † For advanced students, the corresponding -R, -W, -X, and -O tests use the real user or group ID, which becomes important if your program may be running set-ID. In that case, it’s generally the ID of the person ‡ This is the case on many non-Unix filesystems but not all of the file tests are meaningful everywhere. For example, you aren’t likely to have block special files on your non-Unix system. * This information will be somewhat different on non-Unix systems since not all keep track of the same times that Unix does. For example, on some systems, the ctime field (which the -C test looks at) is the file creation time (which Unix doesn’t keep track of), rather than the inode change time. See the perlport manpage. † As recorded in the $^T variable, which you could update (with a statement like $^T = time;) if you needed to get the ages relative to a different starting time. * The -t file test is an exception since that test isn’t useful with filenames (they’re never TTYs). By default, it tests STDIN. † On a non-Unix system, stat and lstat, as well as the file tests, should return “the closest thing available.” For example, a system that doesn’t have user IDs (that is, a system that has just one “user,” in the Unix sense) might return zero for the user and group IDs as if the only user is the system administrator. If stat or lstat * The first character in that string isn’t a permission bit. It indicates the type of entry: a hyphen for an ordinary file, d for directory, or l for symbolic link, among others. The ls command determines this from the other bits past the least significant nine. * Because it is. Asking the system for information is relatively slow.
blog comments powered by Disqus |