compile each thing in a separate package, to avoid leakage
[gitmo/Eval-Closure.git] / t / compiling-package.t
1 #!/usr/bin/env perl
2 use strict;
3 use warnings;
4 use Test::More;
5
6 use Eval::Closure;
7
8 {
9     my $code = eval_closure(
10         source => 'no strict "refs"; sub { keys %{__PACKAGE__ . "::"} }',
11     );
12
13     # defining the sub { } creates __ANON__, calling 'no strict' creates BEGIN
14     my @stash_keys = grep { $_ ne '__ANON__' && $_ ne 'BEGIN' } $code->();
15
16     is_deeply([@stash_keys], [], "compiled in an empty package");
17 }
18
19 {
20     # the more common case where you'd run into this is imported subs
21     # for instance, Bread::Board::as vs Moose::Util::TypeConstraints::as
22     my $c1 = eval_closure(
23         source => 'no strict "vars"; sub { ++$foo }',
24     );
25     my $c2 = eval_closure(
26         source => 'no strict "vars"; sub { --$foo }',
27     );
28     is($c1->(), 1);
29     is($c1->(), 2);
30     is($c2->(), -1);
31     is($c2->(), -2);
32 }
33
34 done_testing;