开发者

How do I return nothing from a subroutine?

I want to validate a condition before doing the next step, but only raise a warning and skip the current value instead of die-ing.

How do I rewri开发者_Python百科te validate_me() subroutine without returning any value?

(Update) please note that the following code works as expected, it just that I want something else instead of returning 1 or 0 from validate_me() that still allow the code to behave the same.

foreach my $file ( @files ) {
    validate_me($foo, $bar, $baz) or next;
    do_something();
}  

sub validate_me {
    my ($foo, $bar) = @_;

    if ( $foo > $bar ) {
        carp("Something goes awol");
        return 0;
    }

    if ( $bar != $baz ) {
        carp("Odd");
        return 0;
    }

    return 1; # success
}

Thanks.


Check the documentation for return. If you want to return nothing, don't give return an argument:

 return;


First, there's no reason to use a label in your loop, a next; by itself will automatically do the next iteration of the enclosing loop.

As to your question, I'm a little confused about what you're trying to accomplish. Your code as written will return zero, which is a false value, from validate_me if any of your tests fail, and that should cause the next iteration of the loop. There's nothing in validate_me that would cause anything to die.


Update:

I want something else instead of returning 1 or 0 from validate_me() that still allow the code to behave the same.

Is this what you mean? See eval.

my @files = qw( a b c d e f );

FILES:
foreach my $file ( @files ) {
    eval { validate_me($file) };
    if (my $ex = $@) {
        warn "'$file' failed validation: $ex";
        next FILES;
    }
    print "$file passed\n";
}

sub validate_me {
    my ($file) = @_;
    die "Invalid filename\n" if 0.5 > rand;
    return; # explicitly return nothing
}

Now, the sane thing to do would be to refactor validate_me into its components:

for my $file ( @files ) {
    if ( is_something_gone_awol($file, $mile) ) {
        warn "Something has gone awol: '$file' and '$mile'\n";
    }
    elsif ( is_something_odd($file, $smile) ) {
        warn "Something is odd: '$file' and '$smile'\n";
    }
    else {
        do_something($file);
    }
}

sub is_something_gone_awol {
    my ($foo, $bar) = 0;
    return $foo gt $bar;
}

sub is_something_odd {
    my ($bar, $baz) = @_;
    return $bar ne $baz;
}


return; will just end the function, no questions asked.

But in case you're wondering, you can also return values by using pointers/references passed into your function as parameters.

void foo(int input, outStruct *output) {
...
output = new outStruct(...); //for example;
return;
};

EDIT I'm a moron, who can't read tags/code. The example is in C/C++, not Perl. OOps.


You can return undef or just return.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜