Perl 中的用户输入 - 在 KomodoEdit 中运行脚本时出现问题

发布于 2024-08-30 03:46:16 字数 742 浏览 2 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

往日 2024-09-06 03:46:16

虽然“使用严格”和拼写正确的 shebang 行总是好的,
两者都不是真正的原因。当你在一个交互式程序中运行一个
非命令行环境下,通常应该关闭 I/O 缓冲。
在 Perl 中,您应该将此行放在代码的顶部:

$| = 1;

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:

$| = 1;
扬花落满肩 2024-09-06 03:46:16

我已经使用 Komodo edit 测试了您的脚本,除了下面的一些更正之外,它运行良好。

#!/usr/bin/perl -w
use strict;

print "Enter the radius of circle: \n";
my $radius = <>;
chomp $radius;
print "radius is: $radius\n";
my $circumference = (2*3.141592654) * $radius;
print "Circumference of circle with radius : $radius = $circumference\n";

科莫多输出

Enter the radius of circle: 
5
radius is: 5
Circumference of circle with radius : 5 = 31.41592654

I have tested your script using Komodo edit and it works fine other then below few corrections.

#!/usr/bin/perl -w
use strict;

print "Enter the radius of circle: \n";
my $radius = <>;
chomp $radius;
print "radius is: $radius\n";
my $circumference = (2*3.141592654) * $radius;
print "Circumference of circle with radius : $radius = $circumference\n";

Output in Komodo

Enter the radius of circle: 
5
radius is: 5
Circumference of circle with radius : 5 = 31.41592654
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文