SoFunction
Updated on 2025-03-03

Comments on the Perl code for proposals and love

Original code
Code source: /?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

The B::Deparse module is a great printer that can uncover the mystery of Perl code and help you understand the transformations the optimizer does for your code. In other words, it will regenerate Perl code, try to omit some obscure parts and write the code in a consistent format.

One way to use the B::Deparse module:

Copy the codeThe code is as follows:
    perl -MO=Deparse heart_raw.pl > heart_deparse.pl

The following code is output:

Copy the codeThe code is 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 the codeThe code is as follows:

#Open the warning switch
    BEGIN { $^W = 1; }

#Symbol Reference Check
    use strict 'refs';

#The index number of the first element in the array
    my $f  = $[;
    my $ch = 0;

#Note that there is a newline character in the string
    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);

#Get the length of the string
    sub l {
        length $_;
    }

#Invert string
#join, 0 is used to make up the numbers, and can be omitted
    sub r {
        join '', reverse( split( //, $_[0], 0 ) );
    }

#Extract substrings in strings
    sub ss {
        substr $_[0], $_[1], $_[2];
    }

#Output
    sub p {
        print @_;
    }

#Extract a character alternately from the first and second half of the string and output it
    sub be {
        $_ = $_[0];
        p( ss( $_, $f, 1 ) );
        $f += l() / 2;
        $f %= l();
        ++$f if $ch % 2;
        $ch++;
    }

Code rewriting
Copy the codeThe code is 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 the codeThe code is as follows:
kristen, will you marry me?