{"id":7036,"date":"2022-12-20T19:34:43","date_gmt":"2022-12-20T22:34:43","guid":{"rendered":"http:\/\/lode.uno\/linux-man\/index.php\/2022\/12\/20\/evalclosure-man3\/"},"modified":"2022-12-20T19:34:43","modified_gmt":"2022-12-20T22:34:43","slug":"evalclosure-man3","status":"publish","type":"post","link":"https:\/\/lode.uno\/linux-man\/2022\/12\/20\/evalclosure-man3\/","title":{"rendered":"Eval::Closure (man3)"},"content":{"rendered":"<h1 align=\"center\">Eval::Closure<\/h1>\n<p> <a href=\"#NAME\">NAME<\/a><br \/> <a href=\"#VERSION\">VERSION<\/a><br \/> <a href=\"#SYNOPSIS\">SYNOPSIS<\/a><br \/> <a href=\"#DESCRIPTION\">DESCRIPTION<\/a><br \/> <a href=\"#FUNCTIONS\">FUNCTIONS<\/a><br \/> <a href=\"#BUGS\">BUGS<\/a><br \/> <a href=\"#SEE ALSO\">SEE ALSO<\/a><br \/> <a href=\"#SUPPORT\">SUPPORT<\/a><br \/> <a href=\"#NOTES\">NOTES<\/a><br \/> <a href=\"#AUTHOR\">AUTHOR<\/a><br \/> <a href=\"#COPYRIGHT AND LICENSE\">COPYRIGHT AND LICENSE<\/a> <\/p>\n<hr>\n<h2>NAME <a name=\"NAME\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">Eval::Closure \u2212 safely and cleanly create closures via string eval<\/p>\n<h2>VERSION <a name=\"VERSION\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">version 0.14<\/p>\n<h2>SYNOPSIS <a name=\"SYNOPSIS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">use Eval::Closure; <br \/> my $code = eval_closure( <br \/> source => &#8216;sub { $foo++ }&#8217;, <br \/> environment => { <br \/> &#8216;$foo&#8217; => 1, <br \/> }, <br \/> ); <br \/> warn $code\u2212>(); # 1 <br \/> warn $code\u2212>(); # 2 <br \/> my $code2 = eval_closure( <br \/> source => &#8216;sub { $code\u2212>() }&#8217;, <br \/> ); # dies, $code isn&#8217;t in scope<\/p>\n<h2>DESCRIPTION <a name=\"DESCRIPTION\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">String eval is often used for dynamic code generation. For instance, &#8220;Moose&#8221; 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 \u2212 it\u2019s difficult to control the scope it\u2019s used in (which determines which variables are in scope inside the eval), and it\u2019s easy to miss compilation errors, since eval catches them and sticks them in $@ instead.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">This module attempts to solve these problems. It provides an &#8220;eval_closure&#8221; function, which evals a string in a clean environment, other than a fixed list of specified variables. Compilation errors are rethrown automatically.<\/p>\n<h2>FUNCTIONS <a name=\"FUNCTIONS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>eval_closure(%args)<\/b> <br \/> 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: <br \/> source<\/p>\n<p style=\"margin-left:17%;\">The string to be evaled. It should end by returning a code reference. It can access any variable declared in the &#8220;environment&#8221; 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).<\/p>\n<p style=\"margin-left:11%;\">environment<\/p>\n<p style=\"margin-left:17%;\">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 &#8220;{ &#8216;@foo&#8217; => [] }&#8221; (which would allow the generated function to use an array named @foo). 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).<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">In perl 5.18 and greater, the environment hash can contain variables with a sigil of &#8220;&#038;&#8221;. This will create a lexical sub in the evaluated code (see &#8220;The \u2019lexical_subs\u2019 feature&#8221; in feature). Using a &#8220;&#038;&#8221; sigil on perl versions before lexical subs were available will throw an error.<\/p>\n<p style=\"margin-left:11%;\">alias<\/p>\n<p style=\"margin-left:17%;\">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 <i>shallow copy<\/i> of the variables.<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">If this argument is omitted, Eval::Closure will currently assume false, but this assumption may change in a future version.<\/p>\n<p style=\"margin-left:11%;\">description<\/p>\n<p style=\"margin-left:17%;\">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 &#8220;(eval n)&#8221;, where \u2019n\u2019 is a sequential identifier for every string eval that has happened so far in the program. Passing a &#8220;description&#8221; parameter lets you override that to something more useful (for instance, Moose overrides the description for accessors to something like &#8220;accessor foo at MyClass.pm, line 123&#8221;).<\/p>\n<p style=\"margin-left:11%;\">line<\/p>\n<p style=\"margin-left:17%;\">This lets you override the particular line number that appears in backtraces, much like the &#8220;description&#8221; option. The default is 1.<\/p>\n<p style=\"margin-left:11%;\">terse_error<\/p>\n<p style=\"margin-left:17%;\">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.<\/p>\n<h2>BUGS <a name=\"BUGS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">No known bugs.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Please report any bugs to GitHub Issues at <https:\/\/github.com\/doy\/eval\u2212closure\/issues>.<\/p>\n<h2>SEE ALSO <a name=\"SEE ALSO\"><\/a> <\/h2>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p style=\"margin-top: 1em\">\u2022<\/p>\n<\/td>\n<td width=\"5%\"><\/td>\n<td width=\"43%\">\n<p style=\"margin-top: 1em\">Class::MOP::Method::Accessor<\/p>\n<\/td>\n<td width=\"40%\"> <\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:17%; margin-top: 1em\">This module is a factoring out of code that used to live here<\/p>\n<h2>SUPPORT <a name=\"SUPPORT\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">You can find this documentation for this module with the perldoc command.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">perldoc Eval::Closure<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">You can also look for information at:<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p style=\"margin-top: 1em\">\u2022<\/p>\n<\/td>\n<td width=\"5%\"><\/td>\n<td width=\"12%\">\n<p style=\"margin-top: 1em\">MetaCPAN<\/p>\n<\/td>\n<td width=\"71%\"> <\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:17%; margin-top: 1em\"><https:\/\/metacpan.org\/release\/Eval\u2212Closure><\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p style=\"margin-top: 1em\">\u2022<\/p>\n<\/td>\n<td width=\"5%\"><\/td>\n<td width=\"9%\">\n<p style=\"margin-top: 1em\">Github<\/p>\n<\/td>\n<td width=\"74%\"> <\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:17%; margin-top: 1em\"><https:\/\/github.com\/doy\/eval\u2212closure><\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p style=\"margin-top: 1em\">\u2022<\/p>\n<\/td>\n<td width=\"5%\"><\/td>\n<td width=\"40%\">\n<p style=\"margin-top: 1em\"><small>RT: CPAN<\/small> \u2019s request tracker<\/p>\n<\/td>\n<td width=\"43%\"> <\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:17%; margin-top: 1em\"><http:\/\/rt.cpan.org\/NoAuth\/Bugs.html?Dist=Eval\u2212Closure><\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p style=\"margin-top: 1em\">\u2022<\/p>\n<\/td>\n<td width=\"5%\"><\/td>\n<td width=\"18%\">\n<p style=\"margin-top: 1em\"><small>CPAN<\/small> Ratings<\/p>\n<\/td>\n<td width=\"65%\"> <\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:17%; margin-top: 1em\"><http:\/\/cpanratings.perl.org\/d\/Eval\u2212Closure><\/p>\n<h2>NOTES <a name=\"NOTES\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">Based on code from Class::MOP::Method::Accessor, by Stevan Little and the Moose Cabal.<\/p>\n<h2>AUTHOR <a name=\"AUTHOR\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">Jesse Luehrs <doy@tozt.net><\/p>\n<h2>COPYRIGHT AND LICENSE <a name=\"COPYRIGHT AND LICENSE\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">This software is copyright (c) 2016 by Jesse Luehrs.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">This is free software; you can redistribute it and\/or modify it under the same terms as the Perl 5 programming language system itself.<\/p>\n<hr>\n","protected":false},"excerpt":{"rendered":"<p>  Eval::Closure \u2212 safely and cleanly create closures via string eval <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[3384,3007],"class_list":["post-7036","post","type-post","status-publish","format-standard","hentry","category-sin-categoria","tag-evalclosure","tag-man3"],"gutentor_comment":0,"_links":{"self":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/7036","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/comments?post=7036"}],"version-history":[{"count":0,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/7036\/revisions"}],"wp:attachment":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/media?parent=7036"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/categories?post=7036"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/tags?post=7036"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}