Perl具体代码

发布于 2024-12-18 17:56:03 字数 201 浏览 0 评论 0原文

以下程序是用 Perl 编写的。

cat "test... test... test..." | perl -e '$??s:;s:s;;$?::s;;=]=>%-{<-|}<&|`{;;y; -/:-@[-`{-};`-{/" -;;s;;$_;see'

有人可以帮助我理解它是如何工作的吗?

The following program is in Perl.

cat "test... test... test..." | perl -e '$??s:;s:s;;$?::s;;=]=>%-{<-|}<&|`{;;y; -/:-@[-`{-};`-{/" -;;s;;$_;see'

Can somebody help me to understand how it works?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一场春暖 2024-12-25 17:56:03

Debian 论坛已经有人询问过这段代码。

根据该线程的主持人 Lacek 的说法,该代码最初的作用是 rm -rf / ,尽管他们提到他们已经更改了那里的版本,以便人们试图弄清楚它是如何工作的不要删除他们的整个文件系统。其中还解释了 Perl 代码各个部分的作用。

(你发布这篇文章时知道它做了什么,还是不知道它的作用?)

引用 Lacek 的帖子:

无论如何,这就是脚本的工作原理。

它基本上是两个正则表达式替换和一个音译。
将任何内容通过管道输入到其标准输入中没有任何区别,perl
代码不以任何方式使用其输入。如果你把长线分开
表达式的边界,你会得到:

<前><代码>$??s:;s:s;;$?::
s;;=]=>%-{\\>%<-{;;
y; -/:-@[-`{-};`-{/" -;;
s;;$_;参见

第一行是一个条件,不执行任何操作,使代码保存
看起来比较困难。如果上一个命令源自 perl
代码不成功,它对标准进行了一些替换
输入(程序不使用它,因此它有效地替代了
什么都没有)。由于不存在先前的命令,$?始终为 0,因此
第一行永远不会被执行。

第二行替换
标准输入(无任何内容)看似毫无意义的垃圾。

第三行是音译运算符。它定义了4个范围,分别是
其中字符被替换为 1 范围和 4
音译替换中给出的字符。我不想
把整个音译表写在这里,因为有点长。
如果你真的感兴趣,只需写下定义的字符即可
范围(空格到“/”、“:”到“@”、“[”到“(反引号)”和“{”到“}”),以及
在它们旁边写入替换范围中的字符('(反引号)'到
'{'),最后写入剩余的字符(/、"、空格和 -)
从替换模式来看。当你有这张表时,你可以看到
什么字符被替换为什么。

最后一行执行
通过用结果字符串替换 no 来生成命令
(这是'xterm'。最初它是'system"rm -rf /"',并且被保存
在 $_) 中,将替换计算为表达式并执行它。

(我在这里用“反引号”替换了实际的反引号字符,这样代码自动格式化就不会启动。)

This bit of code's already been asked about on the Debian forums.

According to Lacek, the moderator on that thread, what the code originally did is rm -rf /, though they mention they've changed the version there so that people trying to figure out how it works don't delete their entire filesystem. There's also an explanation there of what the various parts of the Perl code do.

(Did you post this knowing what it did, or were you unaware of it?)

To quote Lacek's post on it:

Anyway, here is how the script works.

It is basically two regex substitutions and one transliteration.
Piping anything into its standard input makes no difference, the perl
code doesn't use its input in any way. If you split the long line on
the boundaries of the expressions, you get this:

$??s:;s:s;;$?::
s;;=]=>%-{\\>%<-{;;
y; -/:-@[-`{-};`-{/" -;;
s;;$_;see

The first line is a condition which does nothing save makes the code
look more difficult. If the previous command originated from the perl
code wasn't successful, it does some substitutions on the standard
input (which the program doesn't use, so effectively it substitutes
the nothing). Since no previous command exists, $? is always 0, so the
first line never gets executed.

The second line substitutes the
standard input (the nothing) for seemingly meaningless garbage.

The third line is a transliteration operator. It defines 4 ranges, in
which the characters gets substituted to the one range and the 4
characters given in the transliteration replacement. I'd prefer not to
write the whole transliteration table here, because it's a bit long.
If you are really interested, just write the characters in the defined
ranges (space to '/', ':' to '@', '[' to '(backtick)', and '{' to '}'), and
write next to them the characters from the replacement range ('(backtick)' to
'{'), and finally, write the remaining characters (/,", space and -)
from the replacement pattern. When you have this table, you can see
what character gets replaced to what.

The last line executes the
resulting command by substituting the nothing with the resulted string
(which is 'xterm'. Originally it was 'system"rm -rf /"', and is held
in $_), evaluates the substitution as an expression and executes it.

(I've substituted 'backtick' for the actual backtick character here so that the code auto-formatting doesn't kick in.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文