In a coroutine/continuationless language, return never evaluates to a value. It only transfers control to the caller one level up the call stack. You can see this for Perl by looking at the following code:

Q:\>perl -wle "my $foo; sub f { $foo = return 'bar' }; f(); print $foo +" Use of uninitialized value in print at -e line 1. Q:\>perl -wle "my $foo='x'; sub f { $foo = return 'bar' }; f(); print +$foo" x

In neither case, $foo was changed, because return does not have a "expression value" in Perl.

If Perl was a language that had real continuations (Coro left aside), you could invent in that alternate universe a value to be returned from return 'bar', which would modify the flow of control:

sub f { my $position_of_caller = return 3; # f() will be '3', eventually my $alternative = return 5; # f() will be '5', eventually print "Now we return a value:"; $alternative->(); # return 5 to caller # will we get here? Possibly, through the "yield" call. CONTINUATION: print "We are continuing here?"; }; f(); yield; # transfers control to the label CONTINUATION

Coro has something like this, and having different strains of execution throughout your program allows you for example to have something like threads except without the locking problems. You won't get true parallelism through it, and playing with bare continuations can bet quite complex soon.

An interesting question is for example what would/should happen if you call f() twice without an intervening yield; statement.


In reply to Re^7: 'return:' instead of 'return' by Corion
in thread 'return:' instead of 'return' by Boldra

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.