Commit | Line | Data |
cc7c20a0 |
1 | use Config; |
2 | BEGIN { |
a6d0da2f |
3 | unless ($Config{useithreads}) { |
4 | print "1..0 # SKIP your perl does not support ithreads\n"; |
5 | exit 0; |
cc7c20a0 |
6 | } |
7 | } |
a6d0da2f |
8 | use threads; |
9 | use strictures 1; |
10 | use Test::More; |
cc7c20a0 |
11 | |
12 | use Sub::Quote; |
13 | |
14 | my $one = quote_sub my $one_code = q{ |
15 | BEGIN { $::EVALED{'one'} = 1 } |
16 | 42 |
17 | }; |
18 | |
19 | my $two = quote_sub q{ |
20 | BEGIN { $::EVALED{'two'} = 1 } |
21 | 3 + $x++ |
22 | } => { '$x' => \do { my $x = 0 } }; |
23 | |
a6d0da2f |
24 | is(threads->create(sub { |
cc7c20a0 |
25 | my $quoted = quoted_from_sub($one); |
a6d0da2f |
26 | $quoted && $quoted->[1]; |
27 | })->join, $one_code, 'able to retrieve quoted sub in thread'); |
cc7c20a0 |
28 | |
29 | my $u_one = unquote_sub $one; |
30 | |
31 | is(threads->create(sub { $one->() })->join, 42, 'One (quoted version)'); |
32 | |
33 | is(threads->create(sub { $u_one->() })->join, 42, 'One (unquoted version)'); |
34 | |
35 | my $r = threads->create(sub { |
36 | my @r; |
37 | push @r, $two->(); |
38 | push @r, unquote_sub($two)->(); |
39 | push @r, $two->(); |
40 | \@r; |
41 | })->join; |
42 | |
43 | is($r->[0], 3, 'Two in thread (quoted version)'); |
44 | is($r->[1], 4, 'Two in thread (unquoted version)'); |
45 | is($r->[2], 5, 'Two in thread (quoted version again)'); |
46 | |
47 | done_testing; |