.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Eval::Closure 3pm" .TH Eval::Closure 3pm "2022-11-19" "perl v5.36.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Eval::Closure \- safely and cleanly create closures via string eval .SH "VERSION" .IX Header "VERSION" version 0.14 .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 1 \& use Eval::Closure; \& \& my $code = eval_closure( \& source => \*(Aqsub { $foo++ }\*(Aq, \& environment => { \& \*(Aq$foo\*(Aq => \e1, \& }, \& ); \& \& warn $code\->(); # 1 \& warn $code\->(); # 2 \& \& my $code2 = eval_closure( \& source => \*(Aqsub { $code\->() }\*(Aq, \& ); # dies, $code isn\*(Aqt in scope .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" String eval is often used for dynamic code generation. For instance, \f(CW\*(C`Moose\*(C'\fR uses it heavily, to generate inlined versions of accessors and constructors, which speeds code up at runtime by a significant amount. String eval is not without its issues however \- it's difficult to control the scope it's used in (which determines which variables are in scope inside the eval), and it's easy to miss compilation errors, since eval catches them and sticks them in $@ instead. .PP This module attempts to solve these problems. It provides an \f(CW\*(C`eval_closure\*(C'\fR function, which evals a string in a clean environment, other than a fixed list of specified variables. Compilation errors are rethrown automatically. .SH "FUNCTIONS" .IX Header "FUNCTIONS" .SS "eval_closure(%args)" .IX Subsection "eval_closure(%args)" This function provides the main functionality of this module. It is exported by default. It takes a hash of parameters, with these keys being valid: .IP "source" 4 .IX Item "source" The string to be evaled. It should end by returning a code reference. It can access any variable declared in the \f(CW\*(C`environment\*(C'\fR parameter (and only those variables). It can be either a string, or an arrayref of lines (which will be joined with newlines to produce the string). .IP "environment" 4 .IX Item "environment" The environment to provide to the eval. This should be a hashref, mapping variable names (including sigils) to references of the appropriate type. For instance, a valid value for environment would be \f(CW\*(C`{ \*(Aq@foo\*(Aq => [] }\*(C'\fR (which would allow the generated function to use an array named \f(CW@foo\fR). Generally, this is used to allow the generated function to access externally defined variables (so you would pass in a reference to a variable that already exists). .Sp In perl 5.18 and greater, the environment hash can contain variables with a sigil of \f(CW\*(C`&\*(C'\fR. This will create a lexical sub in the evaluated code (see \&\*(L"The 'lexical_subs' feature\*(R" in feature). Using a \f(CW\*(C`&\*(C'\fR sigil on perl versions before lexical subs were available will throw an error. .IP "alias" 4 .IX Item "alias" If set to true, the coderef returned closes over the variables referenced in the environment hashref. (This feature requires Devel::LexAlias.) If set to false, the coderef closes over a \fIshallow copy\fR of the variables. .Sp If this argument is omitted, Eval::Closure will currently assume false, but this assumption may change in a future version. .IP "description" 4 .IX Item "description" This lets you provide a bit more information in backtraces. Normally, when a function that was generated through string eval is called, that stack frame will show up as \*(L"(eval n)\*(R", where 'n' is a sequential identifier for every string eval that has happened so far in the program. Passing a \f(CW\*(C`description\*(C'\fR parameter lets you override that to something more useful (for instance, Moose overrides the description for accessors to something like \*(L"accessor foo at MyClass.pm, line 123\*(R"). .IP "line" 4 .IX Item "line" This lets you override the particular line number that appears in backtraces, much like the \f(CW\*(C`description\*(C'\fR option. The default is 1. .IP "terse_error" 4 .IX Item "terse_error" Normally, this function appends the source code that failed to compile, and prepends some explanatory text. Setting this option to true suppresses that behavior so you get only the compilation error that Perl actually reported. .SH "BUGS" .IX Header "BUGS" No known bugs. .PP Please report any bugs to GitHub Issues at . .SH "SEE ALSO" .IX Header "SEE ALSO" .IP "\(bu" 4 Class::MOP::Method::Accessor .Sp This module is a factoring out of code that used to live here .SH "SUPPORT" .IX Header "SUPPORT" You can find this documentation for this module with the perldoc command. .PP .Vb 1 \& perldoc Eval::Closure .Ve .PP You can also look for information at: .IP "\(bu" 4 MetaCPAN .Sp .IP "\(bu" 4 Github .Sp .IP "\(bu" 4 \&\s-1RT: CPAN\s0's request tracker .Sp .IP "\(bu" 4 \&\s-1CPAN\s0 Ratings .Sp .SH "NOTES" .IX Header "NOTES" Based on code from Class::MOP::Method::Accessor, by Stevan Little and the Moose Cabal. .SH "AUTHOR" .IX Header "AUTHOR" Jesse Luehrs .SH "COPYRIGHT AND LICENSE" .IX Header "COPYRIGHT AND LICENSE" This software is copyright (c) 2016 by Jesse Luehrs. .PP This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.