根据man perlrun
:
-0[octal/hexadecimal]
specifies the input record separator ($/) as an octal or
hexadecimal number. If there are no digits, the null character is
the separator.
Run Code Online (Sandbox Code Playgroud)
和
The special value 00 will cause Perl to slurp files in paragraph
mode. Any value 0400 or above will cause Perl to slurp files
whole, but by convention the value 0777 is the one normally used
for this purpose.
Run Code Online (Sandbox Code Playgroud)
但是,鉴于此输入文件:
This is paragraph one
This is paragraph two.
Run Code Online (Sandbox Code Playgroud)
我得到了一些意想不到的结果:
$ perl -0ne 'print; exit' file ## \0 is used, so everything is printed
This is paragraph one.
This is paragraph two.
$ perl -00ne 'print; exit' file ## Paragraph mode, as expected
This is paragraph one.
Run Code Online (Sandbox Code Playgroud)
到现在为止还挺好。现在,为什么这两个似乎也在段落模式下工作?
$ perl -000ne 'print; exit' file
This is paragraph one.
$ perl -0000ne 'print; exit' file
This is paragraph one.
Run Code Online (Sandbox Code Playgroud)
为什么这个显然再次吞下整个文件?
$ perl -00000ne 'print; exit' file
This is paragraph one.
This is paragraph two.
Run Code Online (Sandbox Code Playgroud)
进一步的测试表明,这些似乎都可以在段落模式下工作:
perl -000
perl -0000
perl -000000
perl -0000000
perl -00000000
Run Code Online (Sandbox Code Playgroud)
虽然这些似乎吞没了整个文件:
perl -00000
perl -000000000
Run Code Online (Sandbox Code Playgroud)
我想我的问题是我对八进制的理解不够(根本,真的),我是生物学家,而不是程序员。执行后两种啜食的整个文件,因为这两个0000
和00000000
是>= 0400
?或者有什么完全不同的事情发生?
八进制就像十进制一样 0 == 0, 0000 == 0, 0 == 000000, 等等。这里的 switch 的事实-0
可能会让事情有点混乱——我认为关于“特殊值00”表示开关为0,值为1;添加更多的零不会改变后者,所以你得到同样的东西......
在一定程度上。000000
etc.的行为有点像 bug,但请记住,这应该是指单个 8 位值。8 位的十进制范围为 0-255,八进制为 0-377。所以你不可能在这里有意义地使用超过 3 位数字(特殊值都在该范围之外,但仍然是 3 位数字 + 开关)。您可能只是想从以下内容中推断出这一点:
您还可以使用十六进制表示法指定分隔符:-0xHHH...,其中 H 是有效的十六进制数字。与八进制形式不同,该形式可用于指定任何 Unicode 字符,甚至是 0xFF 以外的字符。
0xFF 十六进制 == 255 十进制 == 377 八进制 == 最大 8 位,一个字节的大小和(扩展的)ASCII 集中的一个字符。