/ proc 文件系统包含正在运行的进程的详细信息。例如,在Linux上,如果你的PID是 123 ,那么该进程的命令行将在 / proc / 123 / cmdline
中找到cmdline使用空字节来分隔参数。
我怀疑应该使用解包但是我不知道怎么样,我使用各种模板(“x”,“z”,“C *”,“H *”)对它进行了悲惨的尝试,“A *”等)只是没有用。
答案 0 :(得分:9)
简单的split("\0", $line)
可以很好地完成工作。
答案 1 :(得分:5)
您可以将$/
设置为"\0"
。例如:
perl -ne 'INIT{ $/ = "\0"} chomp; print "$_\n";' < /proc/$$/environ
答案 2 :(得分:3)
我实际上并不建议使用它,而只是为了您的信息:可行的解包模板是unpack "(Z*)*", $cmdline
。 Z
打包并解包以null结尾的字符串,但因为它是字符串类型,后面是数字或星号 length ,而不是重复 - Z*
解包一个以null结尾的任意长度的字符串。要解压缩任何数量,需要将它包装在括号中,然后将重复应用于带括号的组,从而获得(Z*)*
。
答案 3 :(得分:2)
这可以通过命令行开关-l
和-0
完成,也可以手动更改$/
。
-l
和-0
依赖于订单,可以多次使用。
感谢您鼓励我阅读perlrun文档。
# -0 : set input separator to null
# -l012 : chomp input separator (null)
# and set output separator explicitly to newline, octol 012.
# -p : print each line
# -e0 : null program
perl -0 -l012 -pe0 < /proc/$$/environ
# -l : chomp input separator (/n) (with -p / -n)
# and set output separator to current input separator (/n)
# -0 : set input separator to null
# -p : print each line
# -e0 : null program
perl -l -0 -pe0 < /proc/$$/environ
# partially manual version
# -l : chomp input separator (/n) (with -p / -n)
# and set output separator to current input separator (/n)
# -p : print each line
# -e : set input record separator ($/) explicitly to null
perl -lpe 'INIT{$/="\0"}' < /proc/$$/environ
# DOESN'T WORK:
# -l0 : chomp input separator (/n) (with -p / -n)
# and set output separator to \0
# -e0 : null program
perl -l0 -pe0
# DOESN'T WORK:
# -0 : set input separator to null (\0)
# -l : chomp input separator (\0) (with -p / -n)
# and set output separator to current input separator (\0)
# -e0 : null program
perl -0l -pe1