1Eval::Closure(3)      User Contributed Perl Documentation     Eval::Closure(3)
2
3
4

NAME

6       Eval::Closure - safely and cleanly create closures via string eval
7

VERSION

9       version 0.05
10

SYNOPSIS

12         use Eval::Closure;
13
14         my $code = eval_closure(
15             source      => 'sub { $foo++ }',
16             environment => {
17                 '$foo' => \1,
18             },
19         );
20
21         warn $code->(); # 1
22         warn $code->(); # 2
23
24         my $code2 = eval_closure(
25             source => 'sub { $code->() }',
26         ); # dies, $code isn't in scope
27

DESCRIPTION

29       String eval is often used for dynamic code generation. For instance,
30       "Moose" uses it heavily, to generate inlined versions of accessors and
31       constructors, which speeds code up at runtime by a significant amount.
32       String eval is not without its issues however - it's difficult to
33       control the scope it's used in (which determines which variables are in
34       scope inside the eval), and it can be quite slow, especially if doing a
35       large number of evals.
36
37       This module attempts to solve both of those problems. It provides an
38       "eval_closure" function, which evals a string in a clean environment,
39       other than a fixed list of specified variables. It also caches the
40       result of the eval, so that doing repeated evals of the same source,
41       even with a different environment, will be much faster (but note that
42       the description is part of the string to be evaled, so it must also be
43       the same (or non-existent) if caching is to work properly).
44

FUNCTIONS

46   eval_closure(%args)
47       This function provides the main functionality of this module. It is
48       exported by default. It takes a hash of parameters, with these keys
49       being valid:
50
51       source
52           The string to be evaled. It should end by returning a code
53           reference. It can access any variable declared in the "environment"
54           parameter (and only those variables). It can be either a string, or
55           an arrayref of lines (which will be joined with newlines to produce
56           the string).
57
58       environment
59           The environment to provide to the eval. This should be a hashref,
60           mapping variable names (including sigils) to references of the
61           appropriate type. For instance, a valid value for environment would
62           be "{ '@foo' => [] }" (which would allow the generated function to
63           use an array named @foo). Generally, this is used to allow the
64           generated function to access externally defined variables (so you
65           would pass in a reference to a variable that already exists).
66
67       description
68           This lets you provide a bit more information in backtraces.
69           Normally, when a function that was generated through string eval is
70           called, that stack frame will show up as "(eval n)", where 'n' is a
71           sequential identifier for every string eval that has happened so
72           far in the program. Passing a "description" parameter lets you
73           override that to something more useful (for instance, Moose
74           overrides the description for accessors to something like "accessor
75           foo at MyClass.pm, line 123").
76
77       line
78           This lets you override the particular line number that appears in
79           backtraces, much like the "description" option. The default is 1.
80
81       terse_error
82           Normally, this function appends the source code that failed to
83           compile, and prepends some explanatory text. Setting this option to
84           true suppresses that behavior so you get only the compilation error
85           that Perl actually reported.
86

BUGS

88       No known bugs.
89
90       Please report any bugs through RT: email "bug-eval-closure at
91       rt.cpan.org", or browse to
92       http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Eval-Closure
93       <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Eval-Closure>.
94

SUPPORT

96       You can find this documentation for this module with the perldoc
97       command.
98
99           perldoc Eval::Closure
100
101       You can also look for information at:
102
103       ·   AnnoCPAN: Annotated CPAN documentation
104
105           http://annocpan.org/dist/Eval-Closure
106           <http://annocpan.org/dist/Eval-Closure>
107
108       ·   CPAN Ratings
109
110           http://cpanratings.perl.org/d/Eval-Closure
111           <http://cpanratings.perl.org/d/Eval-Closure>
112
113       ·   RT: CPAN's request tracker
114
115           http://rt.cpan.org/NoAuth/Bugs.html?Dist=Eval-Closure
116           <http://rt.cpan.org/NoAuth/Bugs.html?Dist=Eval-Closure>
117
118       ·   Search CPAN
119
120           http://search.cpan.org/dist/Eval-Closure
121           <http://search.cpan.org/dist/Eval-Closure>
122

AUTHOR

124       Jesse Luehrs <doy at tozt dot net>
125
126       Based on code from Class::MOP::Method::Accessor, by Stevan Little and
127       the Moose Cabal.
128

SEE ALSO

130       ·   Class::MOP::Method::Accessor
131
132           This module is a factoring out of code that used to live here
133
135       This software is copyright (c) 2011 by Jesse Luehrs.
136
137       This is free software; you can redistribute it and/or modify it under
138       the same terms as the Perl 5 programming language system itself.
139
140
141
142perl v5.12.3                      2011-04-25                  Eval::Closure(3)
Impressum