确切的错误:
$ ./script.pl file.txt
Can't open file.txt: No such file or directory at ./script.pl line 17.
Use of uninitialized value in chomp at ./script.pl line 17.
Username: Password:
我正在编写一个脚本,该脚本从命令行获取文件名,然后将其输出写入其中:
#!/usr/bin/perl
use warnings;
use strict;
use Term::ReadKey;
my @array;
my $user;
my $pass;
# get login info
print "Username: ";
chomp($user = <>); # line 17
print "Password: ";
ReadMode 2;
chomp($pass = <>);
ReadMode 0;
print " \n";
# ...
# connect to database, and save the info in "@array"
# ...
# save the array to a file
if (defined($ARGV[0])) {
open (MYFILE, ">".$ARGV[0]) or die "Can't open ".$ARGV[0].": $!\n";
foreach (@array) {
print MYFILE $_."\n";
}
close (MYFILE);
# otherwise, print the names to the screen
} else {
foreach (@array) {
print $_."\n";
}
}
但是,如果我替换ARGV[0]
为"file.txt"
或类似的东西,打印到文件就可以了。如果我不提供文件名,则脚本可以正常工作。我的预感是打印语句干扰了 iostream 缓冲区,但我不知道如何修复它。