【问题标题】:filehandle - won't write to a file文件句柄 - 不会写入文件
【发布时间】: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: $!"
  • 不,非常感谢,但这也没有补救。

标签: perl file-io


【解决方案1】:

反斜杠在双引号字符串中具有特殊含义。尝试转义反斜杠。

open(WRITE, ">c:\\programs\\data.txt") || die ...;

或者,由于您没有插入变量,请切换到单引号。

open(WRITE, '>c:\programs\data.txt') || die ...;

还值得使用开放式和词法文件句柄的三参数版本。

open(my $write_fh, '>', 'c:\programs\data.txt') || die ...;

【讨论】:

    【解决方案2】:

    你必须使用“/”来保证可移植性,所以:open(WRITE, "&gt;c:/programs/data.txt") 注意:我假设c:/programs 文件夹存在

    【讨论】:

    • 是的,两个文件都在同一个文件夹中(否则我知道这是一个问题)。非常感谢,虽然这仍然不起作用!我也纳闷,为什么die函数没有启动。
    • 我认为文件名以c: 开头会自动阻止它的可移植性。你应该说使用/\\ 让它正常工作。如果您想讨论可移植性,您应该讨论使用File::SpecFile::Spec::Functions 以及CwdFindBin
    • 不,这也没有补救措施。谢谢。无论如何都非常感谢。
    【解决方案3】:

    你可能想试试FindBin

    use strict;
    use warnings;
    use autodie; # open will now die on failure
    
    use FindBin;
    use File::Spec::Functions 'catfile';
    my $filename = catfile $FindBin::Bin, 'data.txt';
    
    #obtain info in variables to be written to data.txt
    print("What is your name?"); my $name = <STDIN>;
    print("How old are you?"); my $age = <STDIN>;
    print("What is your email address?"); my $email = <STDIN>;
    
    {
      open( my $fh, '>', $filename );
      print {$fh} "Hi, $name, you are $age, and your email is $email\n";
      close $fh;
    }
    

    【讨论】:

      【解决方案4】:

      如果您在尝试打印到 data.txt 时遇到访问问题,您可以将该行更改为:

      print WRITE "Hi, $name, you are \s $age and your email is \s $email" || die $!;
      

      获取更多信息。只读文件将导致此错误消息:

      Unable to open file data.txt: Permission denied at perl.pl line 12, <STDIN> line 3.
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-02-19
        • 1970-01-01
        • 1970-01-01
        • 2012-04-14
        • 2019-03-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多