perl -00 开关的含义是什么? (八进制或十六进制参数)

What is the meaning of perl -00 switch? (octal or hexadecimal parameter)

举个例子,我知道

perl -l00e 

等同于

$/ = "\n"; $\ = "[=11=]0"; 

它同时设置了 $INPUT$OUTPUT_RECORD_SEPARATOR


此开关还有哪些其他 'tricks'?

OK,对于perl -l00e\OOO就是null byte,来自man ascii

     Oct   Dec   Hex   Char                        Oct   Dec   Hex   Char
     ────────────────────────────────────────────────────────────────────────
     000   0     00    NUL '[=10=]' (null character)   100   64    40    @


因此 -l00e 将输出分隔符设置为 NULL byte,对于通过示例管道传输到 xargs -0

很有用


在其他一堆'tricks'上更进一步:


perldoc 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.)


测试我认为有趣的所有组合:


for i in -l00e -n00e -p00e -n0e -l0e -0ne -0le -p0e -00le -00ne -00pe -0777ne -0777pe -0777le -l0777pe -l0777ne; do
    echo "perl $i"
    perl -MO=Deparse "$i" | head -1 | sed 's|$/|$/ $INPUT_RECORD_SEPARATOR|;s|$\|$\ $OUTPUT_RECORD_SEPARATOR|'
done


perl -l00e
$/ $INPUT_RECORD_SEPARATOR = "\n"; $\ $OUTPUT_RECORD_SEPARATOR = "[=13=]0"
perl -n00e
$/ $INPUT_RECORD_SEPARATOR = ""; $\ $OUTPUT_RECORD_SEPARATOR = undef
perl -p00e
$/ $INPUT_RECORD_SEPARATOR = ""; $\ $OUTPUT_RECORD_SEPARATOR = undef
perl -n0e
$/ $INPUT_RECORD_SEPARATOR = "[=13=]0"; $\ $OUTPUT_RECORD_SEPARATOR = undef
perl -l0e
$/ $INPUT_RECORD_SEPARATOR = "\n"; $\ $OUTPUT_RECORD_SEPARATOR = "[=13=]0"
perl -0ne
$/ $INPUT_RECORD_SEPARATOR = "[=13=]0"; $\ $OUTPUT_RECORD_SEPARATOR = undef
perl -0le
$/ $INPUT_RECORD_SEPARATOR = "[=13=]0"; $\ $OUTPUT_RECORD_SEPARATOR = "[=13=]0"
perl -p0e
$/ $INPUT_RECORD_SEPARATOR = "[=13=]0"; $\ $OUTPUT_RECORD_SEPARATOR = undef
perl -00le
$/ $INPUT_RECORD_SEPARATOR = ""; $\ $OUTPUT_RECORD_SEPARATOR = "\n\n"
perl -00ne
$/ $INPUT_RECORD_SEPARATOR = ""; $\ $OUTPUT_RECORD_SEPARATOR = undef
perl -00pe
$/ $INPUT_RECORD_SEPARATOR = ""; $\ $OUTPUT_RECORD_SEPARATOR = undef
perl -0777ne
$/ $INPUT_RECORD_SEPARATOR = undef; $\ $OUTPUT_RECORD_SEPARATOR = undef
perl -0777pe
$/ $INPUT_RECORD_SEPARATOR = undef; $\ $OUTPUT_RECORD_SEPARATOR = undef
perl -0777le
$/ $INPUT_RECORD_SEPARATOR = undef; $\ $OUTPUT_RECORD_SEPARATOR = ""
perl -l0777pe
$/ $INPUT_RECORD_SEPARATOR = "\n"; $\ $OUTPUT_RECORD_SEPARATOR = "7"
perl -l0777ne
$/ $INPUT_RECORD_SEPARATOR = "\n"; $\ $OUTPUT_RECORD_SEPARATOR = "7"