What are the Perl equivalents of return and continue keywords in C?

后端 未结 3 744
梦毁少年i
梦毁少年i 2021-02-03 23:47

I am getting this error when I use this code

sub search {
    my ($a, @a_list) = @_;
    foreach (@a_list) {
        if($_ == $a) return TRUE;
        # continue         


        
3条回答
  •  北海茫月
    2021-02-04 00:50

    Perl does not have built in constants for true and false. The canonical value for true is 1 and for false is () (which is an empty list in list context, and undef in scalar context).

    A more idomatic way to write your code would be:

    sub search {
        my $key = shift;
        foreach (@_) {
            return 1 if $_ == $key;
        }
        return ();
    }
    

提交回复
热议问题