perl常用代码

字符串联结和重复操作符 
  联接: .
  重复:x
  联接且赋值(类似+=): .=
例:
  $newstring = "potato" . "head";
  $newstring = "t" x 5;
  $a = "be";
  $a .= "witched"; # $a is now "bewitched"

写文件

open(MF,">/tmp/test.txt");#open(MF,">>/tmp/test.txt");追加
print MF ("hello test");
close(MF);

sleep

#!/usr/bin/perl
use Time::HiRes ('gettimeofday','usleep','sleep');
$i = 0;
while($i <100)
{
usleep(1*50*1000);
print time() . " === " . $i ."\n";
$i++;
}

打印一个数组

#!/usr/bin/perl
@a = ('a','b','c','d');
print $#a;
printf "@a\n";
foreach (@a) {
    print "$_\n";
}
print join("\n",@a),"\n";

打印数组长度

print $#a;

判断中的"elsif"而不是"elseif"

if ($ARGV[0] eq 'a') {
    print("a\n");
}
elsif ( $ARGV[0] eq 'b' ) {
    print("b\n");
}
else{
    print("c\n");
}

#dump一个数组

#!/usr/bin/perl
use Data::Dumper qw(Dumper);
my @dwarfs = qw(Doc Grumpy Happy Sleepy Sneezy Dopey Bashful);

$dwarfs[3] = undef;

print Dumper \@dwarfs;

#拆分一个字符串

@ARGV_vars = split(/-----/, $str);

#查看已经安装的Perl模块

find `perl -e ‘print “@INC”‘` -name ‘*.pm’ -print

上一篇:Uva-11374-Airport Express


下一篇:Xamarin生成的APK大小分析