求婚示爱的Perl代码之注释篇
原始代码
代码出处:http://www.perlmonks.org/index.pl?node_id=384100
代码重写
代码输出
代码出处: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
代码重构
B::Deparse 模块是一个很好的打印机,它可以揭开 Perl 代码神秘的面纱,帮助你理解优化器为你的代码做了那些转换。换言之,它会重新生成Perl代码,试着略去一些晦涩难懂的部分,把代码用一致的格式写出来。
使用 B::Deparse 模块的一种方法:
代码如下:
perl -MO=Deparse heart_raw.pl > heart_deparse.pl
输出如下代码:
代码如下:
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);
代码注释
代码如下:
#打开警告开关 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均为凑数用,可以省略 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++; }
代码重写
代码如下:
#!/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++; }
代码输出
代码如下:
kristen, will you marry me?
相关推荐
边城客栈学无止境 2020-07-05
Walter的学习笔记 2020-07-04
A宇 2020-06-14
边城客栈学无止境 2020-06-10
邓博学习笔记 2020-06-03
davidliu00 2020-05-26
ShiShuo 2020-05-16
Aggressivesnail 2020-05-10
ShiShuo 2020-04-26
hanxingwang00 2020-04-22
davidliu00 2020-03-06
ShiShuo 2020-03-06
ShiShuo 2020-03-05
Aggressivesnail 2020-02-28
aaLiweipeng 2020-02-01
amberom 2020-01-16
Walter的学习笔记 2020-01-06