如何在Perl中将变量写入文件

戴维·赖特

我的信息包含在需要写入文件的变量中。我的脚本需要先创建文件,然后写入文件。

这是我当前的脚本:

my $file_location = '/network/$custom_directory/$custom_filename';
open(my $file ">", $file_location) or die $!;
print $file "$variable_data";
close $file;

我感到我的脚本被挂在实际的文件创建上,而不是挂在变量写入过程上。我在运行脚本时遇到的错误是:在我尝试打开文件的那一行上没有“没有这样的文件或目录”。

疾病

您的程序中存在语法错误。的所有三个参数open必须用逗号分隔。

open my $file, '>', $file_location or die $!;

与双引号不同,单引号不会插值,因此您可能需要在文件路径中使用它们:

my $file_location = "/network/$custom_directory/$custom_filename";

顺便说一句:在双引号服务器中包括唯一变量,但不包含字符串内容。您可以等效地

print $file $variable_data;

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章