简体   繁体   English

在 Perl 中,如何检查未定义的 OR 空白参数?

[英]In Perl, how do I check for an undefined OR blank parameter?

I have some code like the following:我有一些像下面这样的代码:

if (not defined $id) {
    print "Enter ID number: ";
    chomp ($id = <STDIN>);
    exit 0 if ($id eq ""); # If empty string, exit.
}
if (not defined $name) {
    print "Enter name: ";
    chomp ($name = <STDIN>);
    exit 0 if ($name eq ""); # If empty string, exit.
}
if (not defined $class) {
    print "Enter class: ";
    chomp ($class = <STDIN>);
    exit 0 if ($class eq ""); # If empty string, exit.
}

I'd like to be able to potentially pass only some of them (eg, name and class, but not ID), passing blanks for the unpassed ones, so I thought the following would work:我希望能够只传递其中的一些(例如,名称和类,但不传递 ID),为未传递的传递空白,所以我认为以下方法可行:

if (not defined $id || $id eq "") {
    print "Enter ID number: ";
    chomp ($id = <STDIN>);
    exit 0 if ($id eq ""); # If empty string, exit.
}

But it doesn't seem to work if I pass, eg, the following:但如果我通过,它似乎不起作用,例如,以下内容:

perl "myperlroutine.pl" "" "Mickey Mouse" "Entry"

The command window (I'm running this on Windows) briefly appears and then immediately disappears.命令窗口(我在 Windows 上运行它)短暂出现然后立即消失。

Am I missing something obvious?我错过了一些明显的东西吗?

You are using the low precedence not where you should be using the high precedence !您使用的是低优先级, not您应该使用高优先级的地方! . .

So you are saying:所以你是说:

if ( not( defined $id || $id eq "" ) )

which is like:这就像:

if ( ! defined $id && $id ne '' )

which is always false.这总是错误的。

So do也一样

if ( ! defined $id || $id eq '' )

instead.反而。

With Perl 5.12 and later, length() no longer warns when passed undef, which is incredibly convenient.在 Perl 5.12 及更高版本中,length() 在传递 undef 时不再发出警告,这非常方便。 So you can do simply:所以你可以简单地做:

if ( ! length $id ) {

which is short code, but it could be inefficient if $id could be a long UTF-8 string, where determining the length is hard.这是短代码,但如果$id可能是一个很长的 UTF-8 字符串,那么它可能效率低下,在这种情况下很难确定长度。 There I would be more inclined to do:在那里我更倾向于这样做:

if ( ( $id // '' ) eq '' ) {

But that's not super readable unless you are familiar with that idiom.但是除非您熟悉该习语,否则这不是超级可读的。

You have received good answers explaining exactly what is wrong with your code.您已经收到了很好的答案,可以准确地解释您的代码有什么问题。 But no one has commented on the bigger picture.但没有人对更大的图景发表评论。

Using positional arguments like this is (as you can see) problematic when the arguments become optional.当参数变为可选时,使用这样的位置参数(如您所见)是有问题的。 In this situation, it's a far better idea to use named arguments.在这种情况下,使用命名参数是一个更好的主意。 The standard Perl library comes with two modules ( Getopt::Std and Getopt::Long ) which make it easy to create flexible sets of command-line options.标准 Perl 库带有两个模块( Getopt::StdGetopt::Long ),可以轻松创建灵活的命令行选项集。 I recommend that you look at using one of those.我建议您考虑使用其中之一。

This pretty much covers anything you're looking for:这几乎涵盖了您正在寻找的任何内容:

 if ( not length( $id )) { 
     ...
 }

AKA:又名:

 unless ( length $id ) { 
     ...
 }

If your window is instantly disappearing that usually indicates a compilation error.如果您的窗口立即消失,通常表示编译错误。 You should open the command window and run the script from the command line:您应该打开命令窗口并从命令行运行脚本:

  • Click "start"点击“开始”
  • type "cmd" into the search bar and hit Enter .在搜索栏中键入“cmd”,然后按 Enter

Then run your script from the command line as follows:然后从命令行运行脚本,如下所示:

perl full_path_to_script\script_name.pl

If you do this, the command window should not instantly close.如果这样做,命令窗口不应立即关闭。 Instead, it should give some feedback on why your script is failing to run.相反,它应该就您的脚本无法运行的原因提供一些反馈。 If you still can't figure out why it is failing after doing that, you should post your compilation errors as well and we can take a look at them.如果您在执行此操作后仍然无法弄清楚为什么它会失败,您也应该发布您的编译错误,我们可以查看它们。

Also, ysth is correct .此外, ysth 是正确的 You should be using "!"你应该使用“!” instead of "not".而不是“不”。

Example:例子:

if ((!defined $id) || ($id eq '')) {
    print "Enter ID number: ";
    chomp ($id = <STDIN>);
    exit 0 if ($id eq ""); # If empty string, exit.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM