【发布时间】:2016-02-16 14:15:45
【问题描述】:
我用一个子程序编写了这个简单的程序。 当我把它放在子程序中时,它的行为很奇怪。
#!/usr/bin/perl -w
use strict ;
sub get_name {
print "what is your name ?\n" ;
my $name = <STDIN> ;
chomp($name) ;
print "Hello $name\n";
}
get_name ;
这是结果
$ ./name_print
casper
what is your name ?
Hello casper
它从不打印你的姓名字符串。 它等待我输入一些标准输入,然后打印“你的名字是什么字符串”。它无序执行。 很奇怪。
【问题讨论】:
-
我无法重现您的行为。在我的系统上,它会打印第一个问题。
-
它对我有用。它首先打印问题,然后当我输入名称时,它会打印带有名称的 hello 消息。
-
你在哪个 shell 中运行它?使用 \r\n 而不是 \n 有什么不同吗?
标签: perl subroutine