奇怪的perl代码 - 寻找解释

Nem*_*emo 14 obfuscation perl

我知道一点perl,但还不够深入了解下一个.

阅读perldelta 5.18我发现了5.18已经禁用的下一段代码.不计算这个,仍然想了解它是如何工作的.

这是代码,在评论中是我理解的

%_=(_,"Just another "); #initialize the %_ hash with key=>value   _ => 'Just another'
$_="Perl hacker,\n";    #assign to the $_ variable with "Perl..."
s//_}->{_/e;            # darkness. the /e - evauates the expression, but...
print
Run Code Online (Sandbox Code Playgroud)

它打印:

Just another Perl hacker,
Run Code Online (Sandbox Code Playgroud)

我试过了,perl -MO=Deparse然后得到了下一个

(%_) = ('_', 'Just another ');   #initializing the %_ hash
$_ = "Perl hacker,\n";           # as above
s//%{'_';}/e;                    # substitute to the beginning of the $_ - WHAT?
print $_;                        # print the result
japh syntax OK
Run Code Online (Sandbox Code Playgroud)

有什么奇怪的(至少对我来说) - 运行"去掉的"代码并没有给出原始结果和打印:

1/8Perl hacker,
Run Code Online (Sandbox Code Playgroud)

我会很开心的:

  1. 如果有人可以解释代码,特别是如果有人可以写一个帮助代码,(有额外的步骤)什么帮助我理解它是如何工作的 - 会发生什么.
  2. 解释,为什么解压缩的代码不打印原​​始结果.

%{'_';}在解除代码中意味着什么?

ike*_*ami 14

替换运算符实际执行的代码实际上可能是这样的

my $code = "do { $repl_expr }";
Run Code Online (Sandbox Code Playgroud)

因此,当替换表达式时_}->{_,执行以下操作:

do { _}->{_ }
Run Code Online (Sandbox Code Playgroud)

_只返回字符串_(因为严格关闭),所以它是相同的

do { "_" }->{_}
Run Code Online (Sandbox Code Playgroud)

这是一样的

"_"->{_}
Run Code Online (Sandbox Code Playgroud)

你有什么哈希元素解除引用,其中引用是一个符号引用(即一个字符串而不是一个实际的引用).通常禁止严格,这是工作中符号引用的一个例子:

%h1 = ( id => 123 );
%h2 = ( id => 456 );
print "h$_"->{id}, "\n"
   for 1..2;
Run Code Online (Sandbox Code Playgroud)

这意味着

"_"->{_}    # Run-time symbol lookup
Run Code Online (Sandbox Code Playgroud)

是相同的

$_{_}       # Compile-time symbol lookup
Run Code Online (Sandbox Code Playgroud)

类似的技巧通常用于单行.

perl -nle'$c += $_; END { print $c }'
Run Code Online (Sandbox Code Playgroud)

可以缩短为

perl -nle'$c += $_; }{ print $c'
Run Code Online (Sandbox Code Playgroud)

因为-n使用时实际执行的代码是从等价的东西中获得的

my $code = "LINE: while (<>) { $program }";
Run Code Online (Sandbox Code Playgroud)
%{'_';}
Run Code Online (Sandbox Code Playgroud)

写作是一种非常奇怪的方式

%{'_'}
Run Code Online (Sandbox Code Playgroud)

这是一个哈希解除引用.同样,这里的引用是一个符号引用.它相当于

%_
Run Code Online (Sandbox Code Playgroud)

在标量上下文中,散列值当前返回一个值,该值报告有关该散列内部的一些信息(如果为空,则返回false值).有人建议改变它以返回键的数量.