如何使用 -0 选项在 Perl 中读取固定宽度的记录?

How do I read fixed-width records in Perl using the -0 option?

所以我知道你可以编写 Perl 单行代码,它使用非默认记录分隔符读取记录,并带有

之类的选项
perl -064 -ne '#... delimited by @'

或一行中的整个文件:

perl -0777 -ne '#... file at once'

我也知道如果您以编程方式将记录分隔符 $\ 设置为对数字的引用,您可以读取固定宽度的记录。

perl -ne '$/ = ; #... 10 chars at a time'

但我找不到任何使用 -0 选项读取固定宽度记录的方法。这可能吗?

没有命令行开关。

来自 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. Other switches may precede or follow the digits. For example, if you have a version of find which can print filenames terminated by the null character, you can say this:

find . -name '*.orig' -print0 | perl -n0e unlink

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.

You can also specify the separator character using hexadecimal notation: -0xHHH..., where the H are valid hexadecimal digits. Unlike the octal form, this one may be used to specify any Unicode character, even those beyond 0xFF. So if you really want a record separator of 0777, specify it as -0x1FF. (This means that you cannot use the -x option with a directory name that consists of hexadecimal digits, or else Perl will think you have specified a hex number to -0.)

当然,您可以使用以下内容:

perl -ne'BEGIN { $/ =  } ...'

perl -e'$/ = ; while (<>) { ... }'