11月 29

在Perl中使用Term::ANSIColor模块控制颜色的三种方法

  • 方法一

Term::ANSIColor的函数color有以下参数:
clear, reset, dark, bold, underline, underscore, blink, reverse, concealed, black, red, green, yellow, blue, magenta, cyan, white, on_black, on_red, on_green, on_yellow, on_blue, on_magenta, on_cyan, and on_white

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/usr/bin/perl
 
use Term::ANSIColor;
 
print color "red";
print "REDn";
print color "green";
print "GREENn";
print color "bold blue";
print "BOLD BLUEn";
print color "on_yellow";
print "ON YELLOWn";
print color "on_cyan";
print "ON CYANn";
print color "reset";

Continue reading

11月 29

在Perl程序中显示进度条之多姿多彩的自写代码

  • 方法一

1
2
3
4
5
6
7
8
#!/usr/bin/perl
 
my $max = 10;
for ( 1 .. $max ) {
   my $percent = $_ / $max * 100;
   print "$_ - $percent % OK!n";
   sleep(1);
}
  • 方法二

  • 从“…..”的长度来判断进度。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    #!/usr/bin/perl -w
     
    $| = 1;
    my $max = 10;
    for ( 1 .. $max ) {
       print ".";
       print " Complete!n" if ( $_ == $max );
       sleep(1);
    }
    • 方法三

    用滚轮在原地显示进度,当然也可以加入百分比。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    
    #!/usr/bin/perl -w
     
    local $| = 1;
    my @progress_symbol = ( '-', '', '|', '/' );
    my $n = 0;
    for ( my $i = 1 ; $i <= 100 ; $i++ ) {
       print "r $progress_symbol[$n] $i";
       $n = ( $n= 3 ) ? 0 : $n + 1;
       select( undef, undef, undef, 0.1 );
    }
    print "n";
    local $| = 0;

    Continue reading

    11月 29

    在Perl程序中显示进度条之使用Term::ProgressBar模块

    • A really simple use

    • 1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      
      #!/usr/bin/perl -w
       
      use Term::ProgressBar 2.00;
      use constant MAX =100_0000;
       
      my $progress = Term::ProgressBar-new(MAX);
       
      for ( 0 .. MAX ) {
         my $is_power = 0;
         for ( my $i = 0 ; 2**$i= $_ ; $i++ ) {
             $is_power = 1
               if 2**$i == $_;
         }
       
         if ($is_power) {
             $progress-》update($_);
         }
      }
    • A smoother bar update

    • 1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      
      #!/usr/bin/perl -w
       
      use Term::ProgressBar 2.00;
       
      my $max = 100000;
       
      my $progress = Term::ProgressBar-new($max);
       
      for ( 0 .. $max ) {
         my $is_power = 0;
         for ( my $i = 0 ; 2**$i= $_ ; $i++ ) {
             $is_power = 1
               if 2**$i == $_;
         }
       
         $progress-》update($_);
      }

      Continue reading