由于文件名以'r'开头,因此无法打开要读取的文件

时间:2016-02-29 11:03:26

标签: perl file perl-module

我的文件名是“rootpass”,我试图像这样阅读,

Unsuccessful stat on filename containing newline at ReadFile.pl line 106. 

我收到这样的错误,

my $passfile = "$jobDir\/rootpass";

106是if()行。我试过以下,

  1. 做了my $passfile = "$jobDir//rootpass"; 逃避逃脱的问题
  2. 做了rootpass 逃避'r'所以它不会认为我在文件名中有一个返回字符
  3. 如何在变量$jobDir中包含的目录名下读取名称为using UnityEngine; using System.Collections; public class ChangeImage : MonoBehaviour { public Texture[] frames; public int CurrentFrame; void OnMouseDown() { if (GlobalVar.PlayClip == false){ GlobalVar.PlayClip = true; } else { GlobalVar.PlayClip = false; } } public void Start() { frames = Resources.LoadAll<Texture>(""); } // Update is called once per frame void Update () { if(GlobalVar.PlayClip == true){ CurrentFrame %= frames.Length; CurrentFrame++; Debug.Log ("Current Frame is " + CurrentFrame); GetComponent<Renderer>().material.mainTexture = frames[CurrentFrame]; } } } 的文件?

2 个答案:

答案 0 :(得分:0)

对原始问题有很多好评,但你也是:

  • 不应该使用全局文件句柄
  • 应该使用三参数打开
  • 正在检查文件是否存在,但是会提供错误信息,表明它不可读。如果文件存在但不可读,则您的脚本无法正常工作,而不会提供有用的错误消息。

这是您脚本的现代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'";

答案 1 :(得分:0)

这一行

my $passfile = "$jobDir/\rootpass";

将放置一个回车字符 - 十六进制0D-其中\r在字符串中。你想要的意思是

my $passfile = "$jobDir/rootpass";

该行

open ROOTPASS, '$passfile';

将尝试打开一个名为-wordrally的文件 - $passfile。你想要

open ROOTPASS, $passfile;

或者,更好

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

以下是摘要

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";

输出

file name = /path/to/jobdir/rootpass
Failed to open "/path/to/jobdir/rootpass" for input: No such file or directory at E:\Perl\source\rootpass.pl line 9.