我的 Perl 脚本出了什么问题?

发布于 2024-11-28 23:30:45 字数 226 浏览 1 评论 0原文

下面是我的代码,基本上,如果答案是“Y”,那么脚本会运行一条消息,如果是其他内容,则脚本会关闭。

#! usr/bin/perl
print "Do you wish to run Program? [Y/N]:";
$answer = <>;
if($answer == "Y") {
 print "COOOL\n";
} else {
 system "exit"
}

Below is my code, basically if the answer is "Y" then the script runs a message if it's something else then it closes.

#! usr/bin/perl
print "Do you wish to run Program? [Y/N]:";
$answer = <>;
if($answer == "Y") {
 print "COOOL\n";
} else {
 system "exit"
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(7

私野 2024-12-05 23:30:46

您正在使用数字 == 来比较字符串。

您可能想使用“eq”:

if($answer eq "Y") {
    print "COOOL\n";
} else {
     system "exit"
}

正如其他人建议的那样,您需要删除末尾的换行符。使用chomp

You are using a numerical == to compare your strings.

You probably want to use "eq":

if($answer eq "Y") {
    print "COOOL\n";
} else {
     system "exit"
}

And as others have suggested you'll want to remove the newline at the end. Use chomp.

奢华的一滴泪 2024-12-05 23:30:46

除了 chomp/chop 和 eq== 之外,您还需要记住答案的大小写。您正在测试大写“Y”,我敢打赌您正在输入小写“y”,并且它们不相等。我建议使用:

 if (($answer eq 'y') || ($answer eq 'Y')) {

或使用uc

Besides the chomp/chop and eq vs ==, you also need to keep in mind the case of the answer. You are testing for UPPERCASE 'Y', I'm willing to bet you are entering lowercase 'y' and they are not equal. I would suggest using:

 if (($answer eq 'y') || ($answer eq 'Y')) {

or use uc.

ゝ偶尔ゞ 2024-12-05 23:30:45

如果你问的话,Perl 会准确地告诉你问题是什么。只需将“使用警告”添加到您的代码中即可。

#!/usr/bin/perl
use warnings;

print "Do you wish to run Program? [Y/N]:";
$answer = <>;
if($answer == "Y") {
 print "COOOL\n";
} else {
 system "exit"
}

然后运行它,给出:

$ ./y
Do you wish to run Program? [Y/N]:Y
Argument "Y" isn't numeric in numeric eq (==) at ./y line 6, <> line 1.
Argument "Y\n" isn't numeric in numeric eq (==) at ./y line 6, <> line 1.
COOOL

如果您还添加“使用诊断”,那就更好了。

$ ./y
Do you wish to run Program? [Y/N]:Y
Argument "Y" isn't numeric in numeric eq (==) at ./y line 7, <> line 1 (#1)
    (W numeric) The indicated string was fed as an argument to an operator
    that expected a numeric value instead.  If you're fortunate the message
    will identify which operator was so unfortunate.

Argument "Y\n" isn't numeric in numeric eq (==) at ./y line 7, <> line 1 (#1)
COOOL

如果让 Perl 帮助您发现错误,那么用 Perl 编程就会容易得多。

Perl will tell you exactly what the problem is, if you ask it. Just add "use warnings" to your code.

#!/usr/bin/perl
use warnings;

print "Do you wish to run Program? [Y/N]:";
$answer = <>;
if($answer == "Y") {
 print "COOOL\n";
} else {
 system "exit"
}

Then running it, gives:

$ ./y
Do you wish to run Program? [Y/N]:Y
Argument "Y" isn't numeric in numeric eq (==) at ./y line 6, <> line 1.
Argument "Y\n" isn't numeric in numeric eq (==) at ./y line 6, <> line 1.
COOOL

It's even better if you add "use diagnostics" as well.

$ ./y
Do you wish to run Program? [Y/N]:Y
Argument "Y" isn't numeric in numeric eq (==) at ./y line 7, <> line 1 (#1)
    (W numeric) The indicated string was fed as an argument to an operator
    that expected a numeric value instead.  If you're fortunate the message
    will identify which operator was so unfortunate.

Argument "Y\n" isn't numeric in numeric eq (==) at ./y line 7, <> line 1 (#1)
COOOL

Programming in Perl is far easier if you let Perl help you find your errors.

睡美人的小仙女 2024-12-05 23:30:45

删除换行符。 == 用于数字相等,对于字符串,您需要 eq

chomp($answer);
if($answer eq "Y") {

Remove newline. == is for numerical equality, for string you need eq.

chomp($answer);
if($answer eq "Y") {
公布 2024-12-05 23:30:45

当您想知道发生了什么时,请开始跟踪您的输入。确保它是您所认为的那样:

#!/usr/bin/perl
use strict;
use warnings;

print "Do you wish to run Program? [Y/N]:";
$answer = <>;

print "Answer is [$answer]\n";

由于您在变量周围放置了大括号,因此您会注意到任何额外的空格。您应该在 $answer 中看到额外的内容:

Answer is [Y
]

这是您需要采取措施来处理该问题的线索。

而且,严格警告可以帮助您在问题出现之前发现问题。

When you wonder what's going on, start tracing your input. Ensure it is what you think it is:

#!/usr/bin/perl
use strict;
use warnings;

print "Do you wish to run Program? [Y/N]:";
$answer = <>;

print "Answer is [$answer]\n";

Since you put the braces around the variable, you'll notice any extra whitespace. You should see extra stuff in $answer:

Answer is [Y
]

That's your clue that you need to do something to handle that.

And, strict and warnings help you find problems before they are problems.

别把无礼当个性 2024-12-05 23:30:45

也许使用 Term::Prompt 或 IO::Prompt 会更好。不要重新发明轮子:)

use IO::Prompt;
prompt -yn, 'Do you wish to run Program?' or exit;

Probably it will be better to use Term::Prompt or IO::Prompt. Don't reinvent the wheel :)

use IO::Prompt;
prompt -yn, 'Do you wish to run Program?' or exit;
贱贱哒 2024-12-05 23:30:45

您有换行符,chomp $answer
$answer eq "Y"

You have newline character, chomp $answer
and $answer eq "Y"

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