perl 备忘录

就是写了一段简单的perl脚本,提醒我常用的语法(主要是字符串操作)。语法速查见脚本的第二行。

脚本perl_demo.pl(对操作系统无要求,只要支持dir和echo命令,安装了perl环境即可):

#!/usr/bin/perl
#perl reference http://www-cgi.cs.cmu.edu/cgi-bin/perl-man
http://vergil.chemistry.gatech.edu/resources/programming/perl-tutorial/regex.html

#hello world
print "hello world/n";

#scalar variable
$t="bye world";

#concatenate strings
print $t."/n";

#split strings, array $t2="apple=red=yellow";
@a=split(/=/,$t2);

#print the array element, for statement, array size
for($i=0;$i<=$#a;$i++){ print $a[$i];
  print " ";
}
print "/n";

#string replace, regular expression
$_="abc 123";

#switch "abc" and "123"
s/([a-z]*)[/s/t]*([0-9]*)//2 /1/g;
$rlt=$_;
print "rlt=$rlt/n";

#bash shell
$t3=`echo hello world`;

#bash shell
$r=system("dir *.pl");

#check return value to see if the process succeed ?
print "r=$r/n";

#subroutines
&test(`echo para1`,`echo para2`,`echo para3`);

sub test{
  #parameters transfered in array, foreach statement
  foreach $i (@_){
    print "i=$i ";
  }
 
  #print parameters again
  for($j=0;$j<=$#_;$j++){
    #access each parameter
    print "j=$_[$j]";
  }
}

你可能感兴趣的:(perl 备忘录)