Modification of a read-only value--perl语言改变只读变量问题

代码如下:

for  ("k","p","c","o","f","g","s"){
     my $name="$_.txt";
     open IN ,"<",$name;
     while(<IN>){
         ........
    }          
}

会在while(<IN>)这一行报错:Modification of a read-only value

这是因为while循环里默认变量也是$_,循环外和循环内$_内容不一致,程序就会试图改变read-only value

一般多层循环,只让最内层用默认变量

posted @ 2019-03-07 18:07  xlfj1205  阅读(366)  评论(0编辑  收藏  举报