Raw code
Source code: HTTP://WWW.PERLMONKS.ORG/INDEX.PL?NODE_ID=384100
#!/usr/bin/perl-w
Use strict;
my$f= $[;my
$ch =0;sub L{length}
Sub R{join "", Reverse split
("", $_[$[])}sub ss{substr ($_[0)
, $_[1],$_[2])}sub be{$_=$_[0];p
(SS ($_, $f, 1)); $f +=l ()/2; $f%=l
() $f ++if$ch%2; $ch ++}my$q=r
("\NTFGPFDFAL,THG?BNGBJ".
"Naxfcixz"); $_= $q; $q =~
tr/f[a-z]/[L-za-k]
/;my@ever=1..&l
; my$mine= $q
; Sub p{
Print
@_;
}
Be $mine for @ever
Code refactoring
B::D eparse module is a good printer that can uncover the mysteries of the Perl code and help you understand what the optimizer has done for your code. In other words, it regenerates the Perl code, trying to omit some obscure parts and write the code in a consistent format.
One way to use the B::D eparse module:
Copy Code code as follows:
Perl-mo=deparse heart_raw.pl > heart_deparse.pl
Output the following code:
Copy Code code as follows:
BEGIN {$^w = 1;}
Use strict ' refs ';
my $f = $[;
my $ch = 0;
Sub L {
Length $_;
}
Sub R {
Join ', Reverse (split (//, $_[0], 0));
}
Sub SS {
Substr $_[0], $_[1], $_[2];
}
Sub be {
$_ = $_[0];
P (SS ($_, $f, 1));
$f + = L ()/2;
$f%= L ();
+ + $f if $ch% 2;
$ch + +;
}
My $q = R ("\ntfgpfdfal,thg?bngbjnaxfcixz");
$_ = $q;
$q =~ tr/[]a-z/[]l-p r-za-k/;
My (@ever) = 1. &l;
my $mine = $q;
Sub P {
Print @_;
}
Be $mine foreach (@ever);
Code comments
Copy Code code as follows:
#打开警告开关
BEGIN {$^w = 1;}
#符号引用检查
Use strict ' refs ';
#数组中第一个元素的索引号
my $f = $[;
my $ch = 0;
#注意字符串中有个换行符
My $q = R ("\ntfgpfdfal,thg?bngbjnaxfcixz");
$_ = $q;
$q =~ tr/[]a-z/[]l-p r-za-k/;
My (@ever) = 1. &l;
my $mine = $q;
Be ($mine) foreach (@ever);
#获取字符串的长度
Sub L {
Length $_;
}
#反转字符串
#join, 0 are dine, you can omit
Sub R {
Join ', Reverse (split (//, $_[0], 0));
}
#提取字符串中的子串
Sub SS {
Substr $_[0], $_[1], $_[2];
}
#输出
Sub P {
Print @_;
}
#从字符串的前半段和后半段交替提取一个字符并输出
Sub be {
$_ = $_[0];
P (SS ($_, $f, 1));
$f + = L ()/2;
$f%= L ();
+ + $f if $ch% 2;
$ch + +;
}
Code rewrite
Copy Code code as follows:
#!/usr/bin/env Perl
Use strict;
Use warnings;
Use UTF8;
my $pointer = 0;
my $character = 0;
My $string = reverse ("\ntfgpfdfal,thg?bngbjnaxfcixz");
$string =~ tr/a-z/l-p r-za-k/;
foreach (1.. length ($string)) {
Print substr ($string, $pointer, 1);
$pointer + = Length ($string)/2;
$pointer%= Length ($string);
+ + $pointer if $character% 2;
$character + +;
}
Code output
Copy Code code as follows:
Kristen, would you marry me?