我使用这个代码我在网上找到读取性能在我的Perl脚本文件:读取和写入同一个文件
open (CONFIG, "myfile.properties");
while (CONFIG){
chomp; #no new line
s/#.*//; #no comments
s/^\s+//; #no leading white space
s/\s+$//; #no trailing white space
next unless length;
my ($var, $value) = split (/\s* = \s*/, $_, 2);
$$var = $value;
}
是否posssible也写这个while循环中的文本文件?比方说,该文本文件看起来像这样:
#Some comments
a_variale = 5
a_path = /home/user/path
write_to_this_variable = ""
我怎么可以把一些文字write_to_this_variable
?
尝试MODE参数 - http://perldoc.perl.org/functions/open.html。 –
你为什么不尝试使用模块为你做阅读和写作?例如。请参阅[Config :: Tiny](https://metacpan.org/module/Config::Tiny)或[Config :: Simple](https://metacpan.org/module/Config::Simple)。 – stevenl
你应该使用'open'三个参数版本以及词法文件句柄和错误检查。例如'打开我的$ config_fh,'<','myfile.properties'或者死掉$ !;' – dgw