无法打开要读取的文件,因为文件名以 'r' 开头
Not able to open a file to read since file name starts with 'r'
我的文件名为 "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() 行。我尝试了以下方法,
- 做到了
my $passfile = "$jobDir\/rootpass";
转义转义字符
- 做到了
my $passfile = "$jobDir//rootpass";
转义 'r' 所以它不会认为我在文件名 return 中有一个字符
如何读取变量$jobDir
中包含的目录名下名为rootpass
的文件?
很多对原问题的好评,但你也:
- 不应使用全局文件句柄
- 应该使用三参数 open
- 正在检查文件 是否存在 ,但给出了无法读取的错误消息。如果文件存在但不可读,您的脚本将无法运行,并且不会给出有用的错误消息。
这是您的脚本的现代 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'";
这一行
my $passfile = "$jobDir/\rootpass";
将放置一个回车符-return 字符——十六进制 0D——其中 \r
在字符串中。你大概的意思是
my $passfile = "$jobDir/rootpass";
行
open ROOTPASS, '$passfile';
将尝试打开一个名为 $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.
我的文件名为 "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() 行。我尝试了以下方法,
- 做到了
my $passfile = "$jobDir\/rootpass";
转义转义字符 - 做到了
my $passfile = "$jobDir//rootpass";
转义 'r' 所以它不会认为我在文件名 return 中有一个字符
如何读取变量$jobDir
中包含的目录名下名为rootpass
的文件?
很多对原问题的好评,但你也:
- 不应使用全局文件句柄
- 应该使用三参数 open
- 正在检查文件 是否存在 ,但给出了无法读取的错误消息。如果文件存在但不可读,您的脚本将无法运行,并且不会给出有用的错误消息。
这是您的脚本的现代 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'";
这一行
my $passfile = "$jobDir/\rootpass";
将放置一个回车符-return 字符——十六进制 0D——其中 \r
在字符串中。你大概的意思是
my $passfile = "$jobDir/rootpass";
行
open ROOTPASS, '$passfile';
将尝试打开一个名为 $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.