读书人

perl中的下面语句是什么意思?该如何解

发布时间: 2012-03-22 17:43:57 作者: rapoo

perl中的下面语句是什么意思?
$fh = new FileHandle $astTypes or die "Cannot open $astTypes: $! ";
while (defined($str = <$fh> ))
{
if ($str =~ /^\s*kind_([^\s]+)\s*=\s*(\d+)/)
{
$value = $2;
$label = $1;
die "value > 255 " if ($value > 255);
$kindLabels[$value] = $label;
}
}
$fh-> close();

[解决办法]
FileHandle #一个Perl模块,操作文件。

$fh = new FileHandle $astTypes or die "Cannot open $astTypes: $! ";
#打开文件$astTypes,返回handle到$fh

while (defined($str = <$fh> ))
#循环读取文件行到$str

if ($str =~ /^\s*kind_([^\s]+)\s*=\s*(\d+)/)
#正则表达式,匹配形如 "kind_name = 7 "的赋值语句。

$value = $2; # $2 为前面匹配到的赋值结果7
$label = $1; # $1 为前面匹配到的赋值名 "name "

die "value > 255 " if ($value > 255);
#如果被赋值大于255则程序错误退出。

$kindLabels[$value] = $label;
#如上例,把@kindLables中下标为7的元素赋值为 "name "

沙发否?

读书人网 >perl python

热点推荐