Filehandles and File Tests
-----------------------------
1. Closing a Filehandle
Perl will automatically close a filehandle if you reopen it (that is, if you reuse the filehandle name in a new open) or if you exit the program.
2. Bad Filehandles
If you try to read from a bad filehandle (that is, a filehandle that isn't properly open), you'll see an immediate end-of-file. (With the I/O methods we'll see in this chapter, end-of-file will be indicated by undef in a scalar context or an empty list in a list context.) If you try to write to a bad filehandle, the data is silently discarded.
3. Fatal Errors with die
1) This human-readable complaint message will be available in Perl's special variable $!.
2) There's one more thing that die will do for you: it will automatically append the Perl program name and line number to the end of the message, so you can easily identify which die in your program is responsible for the untimely exit.
3) If you don't want the line number and file revealed, make sure that the dying words have a newline on the end.
4. Changing the Default Output Filehandle
1) By default, if you don't give a filehandle to print (or to printf, as everything we say here about one applies equally well to the other), the output will go to STDOUT. But that default may be changed with the select operator.
2) Once you've selected a filehandle as the default for output, it will stay that way. But it's generally a bad idea to confuse the rest of the program, so you should generally set it back to STDOUT when you're done.
3) Also by default, the output to each filehandle is buffered. Setting the special $| variable to 1 will set the currently selected filehandle (that is, the one selected at the time that the variable is modified) to always flush the buffer after each output operation.
5. Reopening a Standard Filehandle
1) We mentioned earlier that if you were to reopen a filehandle (that is, if you were to open a filehandle FRED when you've already got an open filehandle named FRED, say), the old one would be closed for you automatically. And we said that you shouldn't reuse one of the six standard filehandle names unless you intended to get that one's special features. And we also said that the messages from die and warn, along with Perl's internally generated complaints, go automatically to STDERR. If you put those three pieces of information together, you now have an idea about how you could send error messages to a file, rather than to your program's standard error stream.
# Send errors to my private error log
open STDERR, ">>/home/barney/.error_log"
or die "Can't open error log for append: $!";
2) If one of the three system filehandles -- STDIN, STDOUT, or STDERR -- fails to be reopened, Perl kindly restores the original one.
6. File Tests
1) These tests generally tell whether the system would try to permit something, but it doesn't mean that it really would be possible. For example, -w may be true for a file on a CD-ROM, even though you can't write to it, or -x may be true on an empty file, which can't truly be executed.
2) The -s test does return true if the file is nonempty, but it's a special kind of true. It's the length of the file, measured in bytes, which evaluates as true for a nonzero number.
3) When checking the age of a file, you might even get a negative value like -1.2, which means that the file's last-access timestamp is set at about thirty hours in the future! The zero point on this timescale is the moment your program started running, so that value might mean that a long-running program was looking at a file that had just been accessed. Or a timestamp could be set (accidentally or intentionally) to a time in the future.
4) You'd think that -T and -B would always disagree, since a text file isn't a binary and vice versa, but there are two special cases where they're in complete agreement. If the file doesn't exist, both are false, since it's neither a text file nor a binary. Alternatively, if the file is empty, it's an empty text file and an empty binary file at the same time, so they're both true.
5) If you omit the filename or filehandle parameter to a file test (that is, if you have just -r or just -s, say), the default operand is the file named in $_. So, to test a list of filenames to see which ones are readable, you simply type:
foreach (@lots_of_filenames) {
print "$_ is readable\n" if -r; # same as -r $_
}
分享到:
相关推荐
filehandles软件包是一个Python库,它通过从目录,zip归档文件,tar归档文件,文件的URL地址等中删除用于打开文件的模板代码,从而简化了文件处理过程。已处理。 链接 filehandles @ filehandles @ 安装 该file...
标题“marc-hughes-ObjectHandles-e263d7f.zip”表明这是一个与对象处理相关的代码库或项目,作者可能是Marc Hughes。这个压缩包的版本标识为“e263d7f”,通常这代表了Git仓库中的一个特定提交哈希值,用于追踪源...
Modules, and now it's Intermediate Perl, but we like to think of it as just Learning More Perl. [*] This is the book that picks up where Learning Perl leaves off. We show you how to use Perl to ...
Runtime_Transform_Handles可用于Unity场景编辑的插件
If no output parameter is specified, then an interactive GUI window will be displayed with a tree-view of all container components, their properties and callbacks. Syntax: [handles,levels,parentIds,...
components, each of which handles a single responsibility. Individual microservices are easy for small teams without extensive knowledge of the entire system design to build and maintain. Microservice...
在Flex开发中,"flex-object-handles.zip_flex"这个压缩包可能包含了关于如何操作和交互Flex中的对象,特别是涉及到对象的移动、编译、放大和缩小功能的代码示例或教程。Flex是一种基于ActionScript和MXML的开源框架...
完整的代码,测试ok # backup-reporter - handles many types of backup jobs, # and produces a uniform report for each job.
Earlier versions of Delphi and C++ Builder will not be supported. If you need Delphi 3 or C++ Builder 3 support you will have to revert to version 3.7 of the Drag and Drop Component Suite. The ...
TSAPI c++ source - Tsapi.cpp : interface to TSAPI Tserver. This file handles the hidden TSAPI ...// Note that this sample hard-codes the number of calls and connections that the // device can support.
Copyright Preface What You Need to Know Who This Book Is For How This Book Is Organized Reference Sections Filesystem Conventions Apache and Perl Versions ...
- 文件句柄(Filehandles):用于读取和写入文件的机制。 - 打开(open)和关闭(close)文件的操作。 - 读取(read)和写入(write)文件内容。 - 文件测试操作符:检查文件或文件句柄的属性,如大小、类型、...
- Note: The list of files is contained in a separate file called FILELIST.DOC, which will appear in the target directory you specify during installation. - After your initial installation, you ...
- Note: The list of files is contained in a separate file called FILELIST.DOC, which will appear in the target directory you specify during installation. - After your initial installation, you ...
To view the statistics of the results, open Matlab and run the file: cd ~/lane-detector/matlab/ matlab& >>Stats ====================== Command line options ====================== LinePerceptor 1.0 ...
I recently did some more tests and came up with these rough tests: 663mb (797 files) of Guttenberg texts on a Pentium 4 running Windows XP with 1 GB of RAM. Indexing max 100,000 fields ?Jlucene: ...
- handles files/memory buffers as input - mouse wheel support ;-) - advanced search functions - change bytes; copy/paste/cut memory blocks - ... Usage: ------ See APIs.tXt. THX: ---- Chris Hobbs - ...
svn, version 1.9.0 (r... - handles 'file' scheme * ra_serf : Module for accessing a repository via WebDAV protocol using serf. - using serf 1.3.8 - handles 'http' scheme - handles 'https' scheme