当条件为假时,执行指定的语句:
my $fred = "45";
unless ($fred =~ /^[A-Z_]\w*$/i) {
print "\n[1]The value of \$fred doesn't look like a Perl identifier name.\n\n";
}
my $mon = "Feb";
unless ($mon =~ /^Feb/) {
print "[2]This month has at least thirty days.\n\n";
}
else {
print "[2]Do you see what's going on here?\n\n";
}
my $mon = "Mar";
unless ($mon =~ /^Feb/) {
print "[3]This month has at least thirty days.\n\n";
}
else {
print "[3]Do you see what's going on here?\n\n";
}
循环体一直执行,直到条件为真:
my $j = 1;
my $i = 20;
print "\n";
until ($j > $i) {
$j *= 2;
print "j = $j \n\n"
}
类似于verilog中的 else if
sim.log:
Compile rtl...
./simv update
# -----------------------------------------
# teststatus: failed
# failed_reason: check failed for y output
# -----------------------------------------
my $file = "sim.log";
print "\n";
open (LOG,"<",$file) or die "can not open $file for reading!\n";
while (defined (my $line = )) {
if ($line =~ /^\s*$/) {
print "it is a empty line!\n\n";
}
elsif ($line =~ /compile\s+rtl/i) {
print "start to compile rtl.\n\n";
}
elsif ($line =~ /teststatus:\s*(\w+)/) {
print "detectd the test status and status is \"$1\"\n\n";
}
elsif ($line =~ /failed_reason:\s*(.*)/) {
print "detectd the failed reason and the reasons is \"$1\"\n\n";
}
}
close (LOG);
$i- - 先赋值后自减
$i++
- -$i
++$i
print "\n";
my $n = 0;
for (my $i = 0;$i <3 ; $i ++) {
print "i = $i ;\n";
}
print "-"x30 . "\n";
my $m = 5;
my $a = ++$m; #m=6
my $b = --$m; #m=5
my $c = $m++; #m=6 , c=5
my $d = $m--; #m=5 , c=6
print "a = $a ,b = $b ,c = $c,d = $d,m = $m;\n";
循环控制用于佛for、foreach、while、until等循环;
my $file = "info.log";
open (LOG,"<",$file) or die "Can not open $file for reading!\n";
my $line_num = 0;
while (defined (my $line = )) {
$line_num++;
print "line_num = $line_num \n";
next if ($line =~ /^phone/i);
last if ($line =~ /^version/i);
print "current line = $line \n\n";
}
close (LOG);
print "="x40 . "\n";
for (my $i = 0; $i < 2 ; $i++) {
print "\ni = $i \n";
for (2..4) {
last if ($_ == 3);
print "i = $i ,j = $_ \n";
}
}
print "="x40 . "\n";
FOR_LOOP: for (my $i = 0; $i < 2 ; $i++) {
print "\ni = $i \n";
for (2..4) {
last FOR_LOOP if ($_ == 3);
print "i = $i ,j = $_ \n";
}
}
在phone那行没显示跳出本次循环,在version那行没显示,直接结束循环。
后一次last if 指定循环FOR_LOOP直接跳出,前一次则跳出所在循环。
expression ? if_ture_expr : if_false_expr
for (1 .. 6) {
my $num = $_;
if ($num < 4 && $num >1) {
print "num = $num and this is the \"&&\"\n\n";
}
elsif ($num == 4 || $num == 6) {
print "num = $num and this is the \"||\"\n\n";
}
}
my @files = qw(ext5.0.pl ext5.1.pl ext5.2.pl ext5.3.pl ext5.4.pl ext5.5.pl ext5.6.pl ext5.7.pl);
my @small_files;
print "\n";
foreach my $file (@files) {
if (! -e $file) {
print "$file does not exist in current dir \n\n";
}
elsif (-s $file < 100_000) {
push (@small_files, $file)
}
}
if (defined $small_files[0]) {
my $num = @small_files;
print "\nThe following $num files' size are samller than 100KB:\n";
print "-"x40 . "\n";
print "@small_files";
}
my $local_time = localtime();
my $time = time();
my $gm_time = gmtime();
print "local_time = $local_time\n";
print "time = $time\n";
print "gm_time = $gm_time\n";
my ($sec,$min,$hour,$day,$mon,$year,$wday,$yday,$isdat) = localtime();
print "\nsec = $sec\n";
print "min = $min\n";
print "hour = $hour\n";
print "day = $day\n";
print "mon = $mon\n";
print "year = $year\n";
print "wday = $wday\n";
print "yday = $yday\n";
print "isdat = $isdat\n";
$year +=1900;
$mon += 1;
my $btime = sprintf ("%d/%02d/%02d/%02d:%02d:%02d",$year,$mon,$day,$hour,$min,$sec);
print "\nyear = $year \n";
print "mon = $mon\n";
print "btime = $btime\n";
my @all_files = golb"*"; #不含以点开始的文件
my @pm_files = glob "*.pm";
my @all_files_including_dot=glob ".**";
my $dir = "/etc"
my @dirfiles = <$dir/* $dir/.* >;
my $dir = "./";
opendir DIR,$dir or die "Can not open $dir: $!";
print "\n";
foreach my $file (readdir DIR) {
if ($file eq ".") {
print "[WARN] -- current obtained file in $dir dir starts with \".\" and skip it \n";
}
elsif ($file eq "..") {
print "[WARN] -- current obtained file in $dir dir starts with \"..\" and skip it \n";
}
else {
print "[INFO] -- current obtained file in $dir dir is $file \n";
}
}
closedir DIR
unlink "slate","bedrock","lava";
unlink glob "*.o";
foreach my $file (glob "./logs/*.old") {
my $newfile = $file;
$newfile =~ s/\.old$/.new/;
if (-e $newfile) {
warn "can't rename $file to $newfile: $newfile exists\n";
}
elsif (rename $file,$newfile) {
print "rename $file to $newfile successfully!\n";
}
else {
print "rename $file to $newfile failed: &!\n";
}
}
mkdir dir_name,permission
mkdir sims,0755;#0755是八进制数
rmdir dir_name;
unlink glob "$tmp_dir/* $tmp_dir/.*";
rmdir $tmpdir;
chmod 0755 FileA,FileB;
my $user = 1004;
my $group =100;
chown $user,$group,glob "*.o";
defined (my $user = getpwnam "merlyn") or die "bad user";
defined (my $group = getpwnam "users") or die "bad groups";
chown $user,$group,glob "/home/merlyn/*";
my $now = time,
utime $now,$now,glob"*";
$where = index($big,$small);
or
$where = index($big,$small,$start_point);
my $stuff = "Howdy world!";
my $where = index ($stuff,"wor");
print "\n[INFO1] input string = \"$stuff\",where = $where \n\n";
my $where1 = index($stuff,"w"); #2
my $where2 = index($stuff,"w",$where1 + 1); #6
my $where3 = index($stuff,"w",$where2 + 1); #-1
print "\n[INFO2] input string = \"$stuff\",where1 = $where1, where2 = $where2, where3 = $where3 \n\n";
my $fred = "Yabba dabba doo!";
my $where1 = rindex($stuff,"abba"); #7
my $where2 = rindex($stuff,"abba",$where1 - 1); #1
my $where3 = rindex($stuff,"abba",$where2 - 1); #-1
print "\n[INFO3] input string = \"$stuff\",where1 = $where1, where2 = $where2, where3 = $where3 \n\n";
$part=substr($string,$initial_position,$length);
my $mineral = substr("Fred J. Flintstone",8,5); #gets "Flint"
my $rock = substr("Fred J. Flintstone",13,100); #gets "stone"
print "\n[INFO_1] mineral = \"$mineral\",rock = \"$rock\"\n\n";
my $pebble = substr("Fred J. Flintstone",13); #gets "stone"
my $out = substr("some very long string",-3,2); #gets "in"
print "[INFO_2] pebble = \"$pebble\",out = \"$out\"\n\n";
my $long = "some very very long string";
my $right = substr($long,index($long,"l")); #index 15
print "[INFO_3] long = \"$long\",right = \"$right\"\n\n";
my $string = "Hello world!";
substr($string,0,5) = "Goodbye";
print "[INFO_4] string = \"$string\"\n\n";
substr($string,0,7) =~ s/goodbye/ByeBye/ig;
print "[INFO_5] string = \"$string\"\n\n";
my $year = "2020";
my $mon = "08";
my $day = "02";
my $hour = "19";
my $min = "32";
my $sec = "17";
my $date_tag = sprintf("%4d/%02d/%02d %2d:%02d:%02d",$year,$mon,$day,$hour,$min,$sec);
print "\n[INFO] date_tag = $date_tag \n\n";
# [INFO] date_tag = 2020/08/02 19:32:17
my @result = sort by_number @items;
sub by_number {
if ($a < $b) {-1} elsif ($a > $b) {1} else {0};
}
sub by_number { $a <=> $b }
sub by_number { $b <=> $a }
my @strings = sort { $a cmp $b} @any—strings;
my @data_arrays = qw(4 2 10 23 7 40 30 20);
my @rise_arrays = sort {$a <=> $b} @data_arrays;
my @fall_arrays = sort {$b <=> $a} @data_arrays;
print "\n[INFO_0] data_arrays = @data_arrays\n";
print "[INFO_0] rise_arrays = @rise_arrays\n";
print "[INFO_0] fall_arrays = @fall_arrays\n";
my @str_arrays = qw(an good buy eggs cups);
my @rise_arrays = sort {$a cmp $b} @str_arrays;
my @fall_arrays = sort {$b cmp $a} @str_arrays;
print "\n[INFO_1] str_arrays = @str_arrays\n";
print "[INFO_1] rise_arrays = @rise_arrays\n";
print "[INFO_1] fall_arrays = @fall_arrays\n";
system "date"
system 'ls -l $HOME';
#单引号防止变量内插system "run_cmd with parameters &";
!system "rm -r files" or die "operation wrong"
my $tarfile = "something*wicked.tar"
my @dirs = qw(fred|flint bet) :
system "tar","cvf",$tarfile,@dirs;#三个待打包目录
如果不采用多参数形式,而采用如下的形式,shell会扩展目录名称@dirs中的特殊字符,造成意想不到的结果:
system "tar cvf $tarfile @dirs";
效果同system类似,用于执行外部命令。system会创建子进程执行外部命令,父进程等待创建的子进程结束并继续执行下而的代码。exec不会开启子进程,而是取代父进程,因此执行完引号中的命令后进程即结束。一般和fork配合使用。
exec调用之后写的任何代码都无法运行
Example:
chdir "/tmp" or die "Cannot chdir/tmp:$!";
exec "bedrock","-o","args1",@ARGV;
$ENV{'PATH'} = "/home/usra/bin:$ENV{'PATH'}";
my $now = `date`;
fork用于从一个进程中创建两个进程。如果它成功,该函数给父进程返回新创建的子进程ID,而给子进程返回0。如果系统没有足够的资源分配一个新的进程,那么调用失败并返回undefo
父进程可以使用waitpid函数等待子进程的结束。
my $opt = 0;
my $localtime;
if (@ARGV > 0) {
$opt = $ARGV[0];
}
else {
print "Usage: perl $0 opt \n";
print " Where opt = 0 or 1 \n";
exit;
}
if ($opt == 1) {
defined (my $pid = fork()) or die "Fork process failured:$!\n";
unless ($pid) {
print "\n[DEBUG1] This is the child process.\n\n";
$localtime = localtime();
print "[DEBUG1] -- child:current time is $localtime \n\n";
sleep(3);
print "[DEBUG1] Exit child after 3seconds wait!\n";
exit();
}
print "[DEBUG1] This is the parent process.\n\n";
waitpid($pid,0);
$localtime = localtime();
print "[DEBUG1] -- parent:current time is $localtime \n\n";
print "[DEBUG1] exit parent!\n\n";
}
else {
defined (my $pid1 = fork()) or die "Fork process failured:$!\n";
unless ($pid1) {
print "\n[DEBUG2] This is the child process.\n\n";
$localtime = localtime();
print "[DEBUG2] -- child:current time is $localtime \n\n";
sleep(3);
print "[DEBUG2] Exit child after 3seconds wait!\n";
exit();
}
print "[DEBUG2] This is the parent process.\n\n";
$localtime = localtime();
print "[DEBUG2] -- parent:current time is $localtime \n\n";
print ("[DEBUG2] exit parent!\n\n");
}