Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Perl中使用字符串返回false?_Perl_Return - Fatal编程技术网

如何在Perl中使用字符串返回false?

如何在Perl中使用字符串返回false?,perl,return,Perl,Return,如何使用Perl函数返回布尔值和字符串 例如,我使用正则表达式在字符串中查找某些内容。如果找到,则返回true并将值赋给$msg,否则返回false并将另一个值赋给$msg 现在我想通过“return($result,$msg);”返回$result(布尔值)和$msg(字符串),并验证它是否正常工作。我想知道这样做是好还是有更好的方法?我不想让其他人在看我的代码时感到困惑 use constant { true => 1, false => 0 }; my $output = "h

如何使用Perl函数返回布尔值和字符串

例如,我使用正则表达式在字符串中查找某些内容。如果找到,则返回true并将值赋给$msg,否则返回false并将另一个值赋给$msg

现在我想通过“return($result,$msg);”返回$result(布尔值)和$msg(字符串),并验证它是否正常工作。我想知道这样做是好还是有更好的方法?我不想让其他人在看我的代码时感到困惑

use constant { true => 1, false => 0 };
my $output = "hello world";
my $msg;
my $result;

sub is_string_found
{
    if ($output =~ /hello/;)
    {
        $msg = "string is found";
        $result = true;
    }
    else
    {
        $msg = "string is not found";
        $result = false;
    }

    return ($result, $msg);
}

您在返回列表方面所做的一切都很好。但使用散列可能更好:

sub is_string_found {
    my %h; 
    if ($output =~ /hello/)
    {   
        %h = (msg=>"string is found", retval=>1);
    }
    else
    {   
        %h = (msg=>"string is not found", retval=>0);
    }
    return %h; 
}

%d = is_string_found();
print $d{msg}, "\n" if not $d{retval};

您在返回列表方面所做的一切都很好。但使用散列可能更好:

sub is_string_found {
    my %h; 
    if ($output =~ /hello/)
    {   
        %h = (msg=>"string is found", retval=>1);
    }
    else
    {   
        %h = (msg=>"string is not found", retval=>0);
    }
    return %h; 
}

%d = is_string_found();
print $d{msg}, "\n" if not $d{retval};

除了使用常量
true
false
,您的代码还有一些问题:

  • 在函数外部声明
    $msg
    $result
    。最好降低这些变量对函数的可见性。事实上,你甚至不需要它们

  • 通过外部作用域中的变量将参数传递给sub,并以相同的方式返回值。改用子程序参数和返回值

  • 你有一个错误的
    if
    -条件中的code>

你的潜艇可以缩短到

sub is_string_found {
  my ($str) = @_;
  return 1, "string is found" if $str =~ /hello/;
  return 0, "string is not found";
}
然后可以像这样使用它

my ($ok, $message) = is_string_found("hello world");
这是(对于Perl程序员)最好、最明显的解决方案


您还可以使用out参数,以便只返回success值:

sub is_string_found ($\$) {
  my ($str, $out_ref) = @_;
  if ($str =~ /hello/) {
    $$out_ref = "string is found";
    return 1;
  } else {
    $$out_ref = "string is not found";
    return;
  }
}
return
不带值返回标量上下文中的
undef
,即列表上下文中的空列表。然后可以像这样调用它

my $ok = is_string_found "hello world", my $message;

注意:您必须在首次使用sub之前声明它,或者在使用之前定义它,或者将语句
sub is\u string\u found($\$)位于脚本顶部附近


您不必使用原型(也可以使用
@
的别名行为),但我并不特别喜欢这种解决方案

除了使用常量
true
false
,您的代码还有一些问题:

  • 在函数外部声明
    $msg
    $result
    。最好降低这些变量对函数的可见性。事实上,你甚至不需要它们

  • 通过外部作用域中的变量将参数传递给sub,并以相同的方式返回值。改用子程序参数和返回值

  • 你有一个错误的
    if
    -条件中的code>

你的潜艇可以缩短到

sub is_string_found {
  my ($str) = @_;
  return 1, "string is found" if $str =~ /hello/;
  return 0, "string is not found";
}
然后可以像这样使用它

my ($ok, $message) = is_string_found("hello world");
这是(对于Perl程序员)最好、最明显的解决方案


您还可以使用out参数,以便只返回success值:

sub is_string_found ($\$) {
  my ($str, $out_ref) = @_;
  if ($str =~ /hello/) {
    $$out_ref = "string is found";
    return 1;
  } else {
    $$out_ref = "string is not found";
    return;
  }
}
return
不带值返回标量上下文中的
undef
,即列表上下文中的空列表。然后可以像这样调用它

my $ok = is_string_found "hello world", my $message;

注意:您必须在首次使用sub之前声明它,或者在使用之前定义它,或者将语句
sub is\u string\u found($\$)位于脚本顶部附近


您不必使用原型(也可以使用
@
的别名行为),但我并不特别喜欢这种解决方案

如果消息和结果之间存在一对一的对应关系,那么最好只返回布尔值,并在别处处理消息--一个辅助函数,如果它被大量使用,也就是说,
true
false
在perl中并不存在。我可能不会让查找字符串的函数成为决定是否找到字符串时显示内容的实体。通常,您只需要让它返回真/假(或1/0或其他)并让与用户打交道的调用者决定字符串的显示内容。谢谢您的评论。但有时上游无法获得足够的信息来处理不同的场景,例如,在上面的代码中,如果未找到“hello”,则设置$msg=“hello is NOT found”,否则如果未找到“world”,则设置$msg=“world is NOT found”,对于这两种情况,都返回false。如果此函数只返回true/false,那么上游无法获取true/false的原因。在这种情况下,你的意思是我需要一个getter/setter函数来将$msg传递给上游函数吗?如果消息和结果之间存在一对一的对应关系,那么最好只返回布尔值,并在其他地方处理消息——如果使用过多,则使用一个辅助函数。也就是说,
true
false
在perl中并不是这样的。我可能不会让查找字符串的函数成为决定是否找到字符串时显示内容的实体。通常,您只需要让它返回真/假(或1/0或其他)并让与用户打交道的调用者决定字符串的显示内容。谢谢您的评论。但有时上游无法获得足够的信息来处理不同的场景,例如,在上面的代码中,如果未找到“hello”,则设置$msg=“hello is NOT found”,否则如果未找到“world”,则设置$msg=“world is NOT found”,对于这两种情况,都返回false。如果此函数只返回true/false,那么上游无法获取true/false的原因。在这种情况下,您的意思是我需要一个getter/setter函数来将$msg传递给上游函数吗?