Return from parent sub in Perl

Posted by JS Bangs on Stack Overflow See other posts from Stack Overflow or by JS Bangs
Published on 2010-05-22T18:15:30Z Indexed on 2010/05/22 18:20 UTC
Read the original article Hit count: 165

Filed under:
|
|

I want to write a subroutine which causes the caller to return under certain conditions. This is meant to be used as a shortcut for validating input to a function. What I have so far is:

sub needs($$) {
    my ($condition, $message) = @_;

    if (not $condition) {
        print "$message\n";
        # would like to return from the *parent* here
    }

    return $condition;
}

sub run_find {
    my $arg = shift @_;
    needs $arg, "arg required" or return;
    needs exists $lang{$arg}, "No such language: $arg" or return;

    # etc.
}

The advantage of returning from the caller in needs would then be to avoid having to write the repetitive or return inside run_find and similar functions.

© Stack Overflow or respective owner

Related posts about perl

Related posts about scope