【发布时间】:2011-03-22 18:01:25
【问题描述】:
我无法让下面的脚本使用 FILEHANDLE 写入文件 data.txt。这两个文件都在同一个文件夹中,所以这不是问题。自从我开始使用 Perl 以来,我注意到要运行脚本,我必须使用完整路径:c:\programs\scriptname.pl 以及输入文件的相同方法。我认为这可能是问题所在,并在下面尝试了这种语法,但也没有用...
open(WRITE, ">c:\programs\data.txt") || die "Unable to open file data.txt: $!";
这是我的脚本。我检查了语法,直到它让我发疯并且看不到问题。任何帮助将不胜感激!。我也纳闷,为什么die函数没有启动。
#!c:\strawberry\perl\bin\perl.exe
#strict
#diagnostics
#warnings
#obtain info in variables to be written to data.txt
print("What is your name?");
$name = <STDIN>;
print("How old are you?");
$age = <STDIN>;
print("What is your email address?");
$email = <STDIN>;
#data.txt is in the same file as this file.
open(WRITE, ">data.txt") || die "Unable to open file data.txt: $!";
#print information to data.txt
print WRITE "Hi, $name, you are \s $age and your email is \s $email";
#close the connection
close(WRITE);
我是如何解决这个问题的。
我在 c: 驱动器上安装了 Strawberry Perl perl.exe,通过安装程序安装并使用带有文件夹的安装程序也在 c 上包含我的脚本,这意味着我无法红色/写入文件(定向或使用函数,即打开的函数),我总是必须使用完整路径来启动脚本。在建议将解释器安装在原来的位置并将我的脚本文件移动到桌面后,我解决了这个问题(将 OS 命令留在脚本的第一行,因为解释器仍然在最初的位置)。现在我可以一键运行脚本,通过 CMD 提示符读取/写入并附加到文件,并轻松使用 Perl 函数。
【问题讨论】:
-
错误信息是什么意思?
-
它什么也没说。没有一个 - 脚本运行,询问 3 个问题,然后停止。
-
试试
print WRITE "Hi, $name, you are \s $age and your email is \s $email" or die "Unable to write: $!" -
不,非常感谢,但这也没有补救。