… | |
… | |
6 | |
6 | |
7 | use Coro::Cont; |
7 | use Coro::Cont; |
8 | |
8 | |
9 | # multiply all hash keys by 2 |
9 | # multiply all hash keys by 2 |
10 | my $cont = csub { |
10 | my $cont = csub { |
11 | result $_*2; |
11 | yield $_*2; |
12 | result $_; |
12 | yield $_; |
13 | }; |
13 | }; |
14 | my %hash2 = map &$csub, &hash1; |
14 | my %hash2 = map &$csub, &hash1; |
15 | |
15 | |
16 | # dasselbe in grün (as we germans say) |
16 | # dasselbe in grün (as we germans say) |
17 | sub mul2 : Cont { |
17 | sub mul2 : Cont { |
18 | result $_[0]*2; |
18 | yield $_[0]*2; |
19 | result $_[0]; |
19 | yield $_[0]; |
20 | } |
20 | } |
21 | |
21 | |
22 | my %hash2 = map mul2($_), &hash1; |
22 | my %hash2 = map mul2($_), &hash1; |
23 | |
23 | |
24 | =head1 DESCRIPTION |
24 | =head1 DESCRIPTION |
… | |
… | |
35 | use Coro::Specific; |
35 | use Coro::Specific; |
36 | |
36 | |
37 | use base 'Exporter'; |
37 | use base 'Exporter'; |
38 | |
38 | |
39 | $VERSION = 0.10; |
39 | $VERSION = 0.10; |
40 | @EXPORT = qw(csub result); |
40 | @EXPORT = qw(csub yield); |
41 | |
41 | |
42 | { |
42 | { |
43 | my @csub; |
43 | my @csub; |
44 | |
44 | |
45 | # this way of handling attributes simply is NOT scalable ;() |
45 | # this way of handling attributes simply is NOT scalable ;() |
… | |
… | |
94 | |
94 | |
95 | sub csub(&) { |
95 | sub csub(&) { |
96 | my $code = $_[0]; |
96 | my $code = $_[0]; |
97 | my $prev = new Coro::State; |
97 | my $prev = new Coro::State; |
98 | |
98 | |
99 | # the fol |
|
|
100 | my $coro = new Coro::State sub { |
99 | my $coro = new Coro::State sub { |
101 | # we do this superfluous switch just to |
100 | # we do this superfluous switch just to |
102 | # avoid the parameter passing problem |
101 | # avoid the parameter passing problem |
103 | # on the first call |
102 | # on the first call |
104 | &result; |
103 | &yield; |
105 | &$code while 1; |
104 | &$code while 1; |
106 | }; |
105 | }; |
107 | |
106 | |
108 | # call it once |
107 | # call it once |
109 | push @$$return, [$coro, $prev]; |
108 | push @$$return, [$coro, $prev]; |
… | |
… | |
114 | &Coro::State::transfer($prev, $coro, 0); |
113 | &Coro::State::transfer($prev, $coro, 0); |
115 | wantarray ? @_ : $_[0]; |
114 | wantarray ? @_ : $_[0]; |
116 | }; |
115 | }; |
117 | } |
116 | } |
118 | |
117 | |
119 | =item @_ = result [list] |
118 | =item @_ = yield [list] |
120 | |
119 | |
121 | Return the given list/scalar as result of the continuation. Also returns |
120 | Return the given list/scalar as result of the continuation. Also returns |
122 | the new arguments given to the subroutine on the next call. |
121 | the new arguments given to the subroutine on the next call. |
123 | |
122 | |
124 | =cut |
123 | =cut |
125 | |
124 | |
126 | # implemented in Coro/State.xs |
125 | # implemented in Coro/State.xs |
127 | #sub result { |
126 | #sub yield(@) { |
128 | # &Coro::State::transfer(@{pop @$$return}, 0); |
127 | # &Coro::State::transfer(@{pop @$$return}, 0); |
129 | # wantarray ? @_ : $_[0]; |
128 | # wantarray ? @_ : $_[0]; |
130 | #} |
129 | #} |
131 | |
130 | |
132 | 1; |
131 | 1; |