Perl 中的用户输入 - 在 KomodoEdit 中运行脚本时出现问题
我在 gedit 上编写了这个小代码并运行它:-
#/usr/bin/perl
print "Enter the radius of circle: \n";
$radius = <>;
chomp $radius;
print "radius is: $radius\n";
$circumference = (2*3.141592654) * $radius;
print "Circumference of circle with radius : $radius = $circumference\n";
使用命令行运行良好。在 Komodo 上运行相同的代码编辑:面临一个问题,我希望第一行输出为:- 输入圆的半径:它在屏幕上等待即等待输入,然后按顺序运行所有内容 - 有人可以告诉我为什么它在命令行上运行良好,但在 Komodo 上运行不佳?
将 #/usr/bin/perl 更改为 #!/usr/bin/perl 后的输出:- 还必须声明我的 $radius 和我的 $circumference ------------------ ----------------------------------------------------
12 # same i had to enter 12
Enter the radius of circle:
radius is: 12
Circumference of circle with radius : 12 = 75.398223696
i wrote this tiny code on gedit and ran it :-
#/usr/bin/perl
print "Enter the radius of circle: \n";
$radius = <>;
chomp $radius;
print "radius is: $radius\n";
$circumference = (2*3.141592654) * $radius;
print "Circumference of circle with radius : $radius = $circumference\n";
Runs fine using command line.Ran the same code on Komodo Edit: facing an issue i expect first line as output as :- Enter the radius of circle: whearas it waits on the screen i.e waiting for an input and after that runs everything in sequence -- can someone tell me why it runs fine with command line but not Komodo?
output after changing #/usr/bin/perl to #!/usr/bin/perl:- also had to declare my $radius and my $circumference ----------------------------------------------------------
12 # same i had to enter 12
Enter the radius of circle:
radius is: 12
Circumference of circle with radius : 12 = 75.398223696
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
虽然“使用严格”和拼写正确的 shebang 行总是好的,
两者都不是真正的原因。当你在一个交互式程序中运行一个
非命令行环境下,通常应该关闭 I/O 缓冲。
在 Perl 中,您应该将此行放在代码的顶部:
While 'use strict' and a correctly spelled shebang line are always good things,
neither is the actual cause. When you run an interactive program in a
non-command-line environment, you should usually turn off I/O buffering.
In Perl you should put this line at the top of your code:
我已经使用 Komodo edit 测试了您的脚本,除了下面的一些更正之外,它运行良好。
科莫多输出
I have tested your script using Komodo edit and it works fine other then below few corrections.
Output in Komodo