Perl 无法打开要读取的文件,因为文件名以';r';

Perl 无法打开要读取的文件,因为文件名以';r';,perl,file,perl-module,Perl,File,Perl Module,我的文件名是“rootpass”,我正试着这样读它 my $rootpass; my $passfile = "$jobDir/\rootpass"; print "file name = $passfile\n"; if( -e $passfile) { open ROOTPASS, '$passfile'; $rootpass = <ROOTPASS>; print "$rootpass\n"; } else { print "No rea

我的文件名是“rootpass”,我正试着这样读它

my $rootpass;
my $passfile = "$jobDir/\rootpass";
print "file name = $passfile\n";
if( -e $passfile)
{    
    open ROOTPASS, '$passfile';
    $rootpass = <ROOTPASS>;
    print "$rootpass\n";
}
else
{
    print "No read permissions on password file $passfile\n";
    exit 1;
}
Unsuccessful stat on filename containing newline at ReadFile.pl line 106. 
106是if()行。我试过以下方法

  • 使之成为我的$passfile=“$jobDir\/rootpass” 逃逸
  • 使之成为我的$passfile=“$jobDir//rootpass” 转义'r',这样它就不会认为我在文件名中有一个返回字符

  • 如何读取变量
    $jobDir
    中包含的目录名下名为
    rootpass
    的文件?

    对原始问题有很多好的评论,但您也:

    • 不应该使用全局文件句柄
    • 应该使用三个参数打开
    • 正在检查该文件是否存在,但给出不可读的错误消息。如果文件存在但不可读,脚本将无法工作,并且不会给出有用的错误消息
    下面是脚本的现代perl版本:

    use 5.012;      # enables "use strict;" and modern features like "say"
    use warnings;
    use autodie;    # dies on I/O errors, with a useful error message
    
    my $jobDir = '/path/to/job/directory';
    
    my $passfile = "$jobDir/rootpass";
    say "filename: '$passfile'";
    
    # No need for explicit error handling, "use autodie" takes care of this.
    open (my $fh, '<', $passfile);
    my $rootpass = <$fh>; chomp($rootpass);  # Gets rid of newline
    say "password: '$rootpass'";
    
    使用5.012;#启用“严格使用”和“说”等现代功能
    使用警告;
    使用autodie;#在I/O错误时死亡,并显示有用的错误消息
    my$jobDir='/path/to/job/directory';
    my$passfile=“$jobDir/rootpass”;
    说“文件名:'$passfile'”;
    #不需要显式的错误处理,“使用autodie”可以解决这个问题。
    打开(我的$fh,这一行

    my $passfile = "$jobDir/\rootpass";
    
    将在字符串中的
    \r
    处放置一个回车字符hex 0D。您可能的意思是

    my $passfile = "$jobDir/rootpass";
    
    线路

    open ROOTPASS, '$passfile';
    
    将尝试打开一个名为字面上-
    $passfile
    的文件

    open ROOTPASS, $passfile;
    
    或者,更好

    open my $pass_fh, '<', $passfile or die $!;
    

    你为什么认为你首先需要避开
    r
    ?这很可能是因为你在阅读时忘记了
    chomp
    。因为我收到错误,说名称中有新行,当我打印文件名时,这是输出,file name=/home/test/files/10.6.502.0 ootpass uncipled stat on filen在ReadFile.pl第106行包含换行符的ame。对密码文件/home/test/files/10.6.502.0 ootpass没有读取权限。/home/test/files/10.6.502.0是该文件的路径。当ootpass添加换行符时,rootpass将在新行中打印。
    \r
    是新行。删除斜杠。同时删除
    '
    周围的
    e> 。它根本不需要引用,单引号也不做变量扩展。你不需要逃避
    r
    。你可以按原样使用它。比如,
    use VERSION
    语句对它的功能不是很透明,通常需要注释来解释它为什么会存在。如果你想要限制和
    ,那么你应该编写
    use strict
    use feature'say'
    ,尽管如果你确实知道你的程序需要哪个版本的Perl,那么为该版本添加
    use version
    也是很有用的。例如,
    use autodie
    直到Perl v5.14.1才可用,所以
    use v5.14.1
    状态更新比获取无法定位的诊断信息更有用。pm
    message不要过于关注现代功能评论。你现在可能会觉得非常更新,但除非你定期修改整个代码库,否则它们不会“现代”很久(事实上,“现代”一词不是有点过时吗?!)而且你的代码看起来会很傻。仅仅是
    #为了限制和“说”
    就足够了。对
    autodie
    没有任何评论是必要的,当然
    #摆脱新行是臃肿的。很抱歉,我正在写一篇不应该写的代码评论。我希望它有用。谢谢你博罗丁!这真的很有用!
    
    use strict;
    use warnings;
    
    my $jobdir   = '/path/to/jobdir';
    my $passfile = "$jobdir/rootpass";
    
    print "file name = $passfile\n";
    
    open my $pass_fh, '<', $passfile or die qq{Failed to open "$passfile" for input: $!};
    my $rootpass = <$pass_fh>;
    print "$rootpass\n";