功能丰富的 Perl JAPH 的精致 Just Another Perl Hacker-2
 
- UID
- 1066743
|

功能丰富的 Perl JAPH 的精致 Just Another Perl Hacker-2
清单 7. Abigail解释的原型1
2
| #Abigail
perl -wle 'sub _ "Just another Perl Hacker"; print prototype \&_'
|
理解这段 JAPH 需要一定的使用原型的知识。请参阅“perldoc -fsub”和“perldoc -fprototype”文档来了解这是怎么一回事。基本上,这建立了一个新的函数,它名为“_”,没有函数体,但带有一个“Justanother Perl Hacker”的原型。
如果您看了 Programming Perl,第三版(请参阅 )关于原型的实际章节,您会发现原型不能是字符串。Abigail很随便地忽略了这个事实(因为这个函数永远都不会用到),然后打印出了无效的原型。
这样合法吗?可能吧,因为它并不引起程序崩溃。这样疯狂吗?只有一点点。还有很多更疯狂的方法,但这一种至少还证明了在定义函数时原型的合法性不会受到检查。它还示范了我们可以定义一个名为“_”的函数― 这种方法您不应该经常使用,因为它会和内建的“_”操作符冲突。
难看的我们看过了良好的和糟糕的 JAPH,所剩下的就是难看的 JAPH了。这些怪兽被精心打造,就是为了让人们畏惧然后到处找药吃,它们定义得太丑陋了。
下面这一段 Kickstart 编写的 JAPH您应该送给对您非常重要的另一半(不包括家里的宠物)。注意,称其为一段难看的JAPH 是不止一个原因的 ― 四行的限制被远远的抛在一边。
清单 8. 燃烧的心1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| #Kickstart from http://www.perlmonks.com/
#note: a slight valentine variation 
$LOVE= AMOUR.
true.cards. ecstacy.crush
.hon.promise.de .votion.partners.
tender.truelovers. treasure.affection.
devotion.care.woo.baby.ardor.romancing.
enthusiasm.fealty.fondness.turtledoves.
lovers.sentiment.worship.sweetling.pure
attachment.flowers.roses.promise.poem;
$LOVE=~ s/AMOUR/adore/g; @a=split(//,
$LOVE); $o.= chr (ord($a[1])+6). chr
(ord($a[3])+3). $a[16]. $a[5]. chr
(32). $a[0]. $a[(26+2)]. $a[27].
$a[5].$a[25]. $a[8].$a[3].chr
(32).$a[29]. $a[8].$a[3].
$a[62].chr(32).$a[62].
$a[2].$a[38].$a[4].
$a[3].'.';
print
$o;
|
信不信由你,这种代码也能运行。但是它能做什么呢?
变量 $LOVE是揭示其神秘之处的第一把钥匙。我们分解这段脚本以便看清 $LOVE变量,我们把它放到一个可执行文件中,这样我们就可以随意进行调试。
输出显示(不带插入的换行符),$LOVE 是:“ AMOURtruecardsecstacycrushhonpromisedevotionpartnerstendertrueloverstreasureaffection
devotioncarewoobabyardorromancingenthusiasmfealtyfondnessturtledovesloverssentiment
worshipsweetlingpureattachmentflowersrosespromisepoem ”,
它告诉我们所有这些不加修饰的单词都被 Perl 作为字符串来解释。
清单 9. 倾倒爱情1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| #!/usr/bin/perl
use Data: umper;
$LOVE= AMOUR.
true.cards. ecstacy.crush
.hon.promise.de .votion.partners.
tender.truelovers. treasure.affection.
devotion.care.woo.baby.ardor.romancing.
enthusiasm.fealty.fondness.turtledoves.
lovers.sentiment.worship.sweetling.pure
attachment.flowers.roses.promise.poem;
print Dumper $LOVE;
$LOVE=~ s/AMOUR/adore/g; @a=split(//,
$LOVE); $o.= chr (ord($a[1])+6). chr
(ord($a[3])+3). $a[16]. $a[5]. chr
(32). $a[0]. $a[(26+2)]. $a[27].
$a[5].$a[25]. $a[8].$a[3].chr
(32).$a[29]. $a[8].$a[3].
$a[62].chr(32).$a[62].
$a[2].$a[38].$a[4].
$a[3].'.';
print
$o;
|
现在,我们把“AMOUR”替换成“adore”,然后将 $LOVE 分解成名为 @a 的单个字符的数组。数组 @a 中第一个元素是 “a”,第二个是“d”,依此类推组成:“ adoretruecards
ecstacycrushhonpromisedevotionpartnerstendertrueloverstreasureaffectiondevotion
carewoobabyardorromancingenthusiasmfealtyfondnessturtledovesloverssentimentworship
sweetlingpureattachmentflowersrosespromisepoem ”
最后,我们通过从 @a 数组中选取字母组成了字符串$o。有时候我们还需要修改它们 ― 只是为了让事情变得有趣 ―但最终爱情会胜利。
拆解后的脚本是:
清单 10. 拆解后的爱情1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| #!/usr/bin/perl
$LOVE = "AMOURtruecardsecstacycrushhonpromisedevotionpartners".
"tendertrueloverstreasureaffectiondevotioncarewoobaby".
"ardorromancingenthusiasmfealtyfondnessturtledoveslovers".
"sentimentworshipsweetlingpureattachmentflowersroses".
"promisepoem";
$LOVE=~ s/AMOUR/adore/g;
@a=split(//, $LOVE);
$o.= chr (ord($a[1])+6). chr (ord($a[3])+3). $a[16]. $a[5] .
# j u s t
chr (32). $a[0]. $a[(26+2)]. $a[27]. $a[5].$a[25]. $a[8].
# space a n o t h e
$a[3].chr (32).$a[29]. $a[8].$a[3]. $a[62].chr(32).$a[62].
# r space p e r l space l
$a[2].$a[38].$a[4]. $a[3].'.';
# o v e r
print $o;
|
真的,爱情是个谜题 |
|
|
|
|
|