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]";
  }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值