Perl 如何知道文件是二进制的? [英] How does Perl know a file is binary?

查看:77
本文介绍了Perl 如何知道文件是二进制的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道您可以使用文件测试运算符 -B 来测试文件是否为二进制文件,但是 Perl 如何在内部实现它?

I know you can use the file test operator -B to test if a file is binary, but how does Perl implement this internally?

推荐答案

来自 perldoc -f -B:

-T-B 开关的工作方式如下.第一个块或检查文件中是否有奇怪的字符,例如奇怪的设置了高位的控制代码或字符.如果太多发现奇怪的字符 (>30%),这是一个 -B 文件;否则它是一个 -T 文件.此外,任何包含 null 的文件第一个块被认为是一个二进制文件.如果 -T-B用于文件句柄,检查当前 IO 缓冲区而不是第一个块.-T-B 都返回 true测试文件句柄时为空文件或 EOF 处的文件.因为你必须读取一个文件来做 -T 测试,所以在大多数情况下有时您想首先对文件使用 -f,例如next 除非 -f $file & -T $file".
The -T and -B switches work as follows. The first block or so of the file is examined for odd characters such as strange control codes or characters with the high bit set. If too many strange characters (>30%) are found, it’s a -B file; otherwise it’s a -T file. Also, any file containing null in the first block is considered a binary file. If -T or -B is used on a filehandle, the current IO buffer is examined rather than the first block. Both -T and -B return true on a null file, or a file at EOF when testing a filehandle. Because you have to read a file to do the -T test, on most occasions you want to use a -f against the file first, as in "next unless -f $file && -T $file".

这篇关于Perl 如何知道文件是二进制的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆