如何用Perl将变量写入文件

如何用Perl将变量写入文件,perl,Perl,我需要将变量中包含的信息写入文件。我的脚本需要创建文件,然后写入 以下是我当前的脚本: my $file_location = '/network/$custom_directory/$custom_filename'; open(my $file ">", $file_location) or die $!; print $file "$variable_data"; close $file; 我感觉我的脚本在实际的文件创建过程中被挂断了,而不是在变量编写过程中。 运行脚本时出现的错误

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

以下是我当前的脚本:

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;

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

open my $file, '>', $file_location or die $!;
与双引号不同,单引号不插值,因此您可能需要在文件路径中使用它们:

my $file_location = "/network/$custom_directory/$custom_filename";
顺便说一句:在双引号服务器中包含一个唯一变量对字符串内容没有任何意义。你可以等价地

print $file $variable_data;

两件事:首先,文件位置在单引号中,因此
$
变量不会被插值。其次,在调用
open
时缺少一个逗号。守则应改为:

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

两件事:首先,文件位置在单引号中,因此
$
变量不会被插值。其次,在调用
open
时缺少一个逗号。守则应改为:

my $file_location = "/network/$custom_directory/$custom_filename";
open(my $file, ">", $file_location) or die $!;
首先,

use strict;
use warnings;
也许会有帮助。其次,变量插值需要双引号字符串:

my $file_location = "/network/$custom_directory/$custom_filename";
第三,您可能需要在print语句中添加一个\n:

print $file "$variable_data\n";
最后,你的公开声明应该是:

open my $file, ">", $file_location or die $!;
首先,

use strict;
use warnings;
也许会有帮助。其次,变量插值需要双引号字符串:

my $file_location = "/network/$custom_directory/$custom_filename";
第三,您可能需要在print语句中添加一个\n:

print $file "$variable_data\n";
最后,你的公开声明应该是:

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

你没有说你的错误是什么

  • 但你漏掉了一个逗号
  • 你也有错误的报价
  • 你也(可能)忘了结尾的新词
  • 你忘了检查关闭是否成功,以免你的文件系统被填满
  • 您可能忘记了二进制模式或编码
这给了你一些类似的东西,带有强制性的序言:

#!/usr/bin/perl

use strict;
use warnings;

my $custom_directory = "something old";
my $custom_filename  = "something new";
my $data             = "something borrowed";

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

open(my $handle, ">", $path)    || die "can't open $path: $!";
binmode($handle);               # for raw; else set the encoding

print $handle "$data\n";

close($handle)                  || die "can't close $path: $!";

你没有说你的错误是什么

  • 但你漏掉了一个逗号
  • 你也有错误的报价
  • 你也(可能)忘了结尾的新词
  • 你忘了检查关闭是否成功,以免你的文件系统被填满
  • 您可能忘记了二进制模式或编码
这给了你一些类似的东西,带有强制性的序言:

#!/usr/bin/perl

use strict;
use warnings;

my $custom_directory = "something old";
my $custom_filename  = "something new";
my $data             = "something borrowed";

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

open(my $handle, ">", $path)    || die "can't open $path: $!";
binmode($handle);               # for raw; else set the encoding

print $handle "$data\n";

close($handle)                  || die "can't close $path: $!";

发生了什么让你“有那种感觉”?告诉我们你的错误是整个脚本?如果是这样的话,那么
$variable\u data
就不会被设置到任何地方。会发生什么情况导致您“获得那种感觉”?向我们显示您得到的错误是整个脚本?如果是,则不会在任何地方设置
$variable\u data
。不,函数的参数不以逗号分隔。它们之间用逗号分隔,也可以用括号分隔。是的,就是这样。双引号公园是有意义的。谢谢否,函数的参数不以逗号分隔。它们之间用逗号分隔,也可以用括号分隔。是的,就是这样。双引号公园是有意义的。谢谢