Is it possible for a Perl subroutine to force its caller to return?

后端 未结 5 458
一生所求
一生所求 2020-12-20 20:09

If I have Perl module like

 package X;

and an object like

 my $x = X->new ();

Inside X.pm, I

5条回答
  •  春和景丽
    2020-12-20 20:45

    Workaround that works but is not nice to see.

    Use the select statement.

    If you have a function my_func() which you call and then based on the return value you want to do some error processing and then return from the caller you could do:

    ($result = my_func()) == 0 ? return handle_error($result) : 0;
    

    This is a (ugly) one-liner that substitutes:

    $result = my_func();
    if ($result == 0) {
        handle_error($result);
    
        return;
    }
    

    Convenient if you have many function to call and check return value.

提交回复
热议问题