首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > perl python >

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

2012-03-22 
perl中的下面语句是什么意思?$fhnewFileHandle$astTypesordieCannotopen$astTypes:$! while(defined($

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 "

沙发否?

热点排行