我编写了Perl
模块,通过<stdin>
从用户处获取用户名,并按下图打印,
use strict;
use warnings;
{
package Insert;
sub Insert_DATA
{
my $User_Name;
print "Please Enter the User Name: ";
chomp ( $User_Name = <stdin> );
print $User_Name , "\n";
}
}
return 1;
但是当我执行这段代码时,它会给出这样的错误,
readline() on unopened filehandle stdin at Insert_Data.pm line 26.
Use of uninitialized value $User_Name in chomp at Insert_Data.pm line 26.
Use of uninitialized value $User_Name in print at Insert_Data.pm line 27.
所以请帮助解决这个问题。
感谢。
答案 0 :(得分:9)
perl启动时打开的文件句柄是STDIN
或main::stdin
,但您已更改包并尝试从Insert::stdin
读取,但不存在
如果您改为编写<STDIN>
或<main::stdin>
,那么它将按预期工作
但是你可能不应该在包子程序中做顶级IO,我建议你积累主程序中的所有数据并将其作为参数传递给Insert::Insert_DATA()
我会写这样的东西
use strict;
use warnings 'all';
use Insert;
print "Please Enter the User Name: ";
chomp(my $user_name = <STDIN>);
insert_data($user_name);
package Insert;
use strict;
use warnings 'all';
use Exporter 'import';
our @EXPORT = 'insert_data';
sub insert_data {
my ($user_name) = @_;
print $user_name, "\n";
}
1;