(No, it's not spam)
Sometimes in a subroutine I want to be able to make the
caller return too.
Pop an extra level off the call stack.
Something like:
sub try_again {
my $msg = shift;
log($msg);
# ..do more stuff..
return 1 if $ok;
return; # would be double return
}
sub go {
do_it() or try_again("It didn't work") or return;
...
}
go();
This case is probably better handled with exceptions
but it got me wondering about the "double return", is
it possible? eg.
# no return in the outer sub
sub go { do_it or try_again; ... }
Some possible approaches:
- Source filter adding a return after the call
- Goto? I don't think it'd work. Clean up'd be skipped
- Exceptions... well that's slightly different
- Continuations. a) not in Perl b) I don't understand them
- Reverse the calls using block prototypes a la Error.pm?
- An XS/Inline helped return N
It should be obvious that this is just curiosity
not serious code or a whinge about having to type 'return'
Thanks
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.