A comment on the Perl code of the love of courtship _perl

Source: Internet
Author: User
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?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.