{
    "mode": "perldoc",
    "parameter": "Eval::Closure",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/Eval%3A%3AClosure/json",
    "generated": "2026-06-11T21:47:01Z",
    "synopsis": "use Eval::Closure;\nmy $code = evalclosure(\nsource      => 'sub { $foo++ }',\nenvironment => {\n'$foo' => \\1,\n},\n);\nwarn $code->(); # 1\nwarn $code->(); # 2\nmy $code2 = evalclosure(\nsource => 'sub { $code->() }',\n); # dies, $code isn't in scope",
    "sections": {
        "NAME": {
            "content": "Eval::Closure - safely and cleanly create closures via string eval\n",
            "subsections": []
        },
        "VERSION": {
            "content": "version 0.14\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "use Eval::Closure;\n\nmy $code = evalclosure(\nsource      => 'sub { $foo++ }',\nenvironment => {\n'$foo' => \\1,\n},\n);\n\nwarn $code->(); # 1\nwarn $code->(); # 2\n\nmy $code2 = evalclosure(\nsource => 'sub { $code->() }',\n); # dies, $code isn't in scope\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "String eval is often used for dynamic code generation. For instance, \"Moose\" uses it heavily, to\ngenerate inlined versions of accessors and constructors, which speeds code up at runtime by a\nsignificant amount. String eval is not without its issues however - it's difficult to control\nthe scope it's used in (which determines which variables are in scope inside the eval), and it's\neasy to miss compilation errors, since eval catches them and sticks them in $@ instead.\n\nThis module attempts to solve these problems. It provides an \"evalclosure\" function, which\nevals a string in a clean environment, other than a fixed list of specified variables.\nCompilation errors are rethrown automatically.\n",
            "subsections": []
        },
        "FUNCTIONS": {
            "content": "evalclosure(%args)\nThis function provides the main functionality of this module. It is exported by default. It\ntakes a hash of parameters, with these keys being valid:\n\nsource\nThe string to be evaled. It should end by returning a code reference. It can access any\nvariable declared in the \"environment\" parameter (and only those variables). It can be\neither a string, or an arrayref of lines (which will be joined with newlines to produce the\nstring).\n\nenvironment\nThe environment to provide to the eval. This should be a hashref, mapping variable names\n(including sigils) to references of the appropriate type. For instance, a valid value for\nenvironment would be \"{ '@foo' => [] }\" (which would allow the generated function to use an\narray named @foo). Generally, this is used to allow the generated function to access\nexternally defined variables (so you would pass in a reference to a variable that already\nexists).\n\nIn perl 5.18 and greater, the environment hash can contain variables with a sigil of \"&\".\nThis will create a lexical sub in the evaluated code (see \"The 'lexicalsubs' feature\" in\nfeature). Using a \"&\" sigil on perl versions before lexical subs were available will throw\nan error.\n\nalias\nIf set to true, the coderef returned closes over the variables referenced in the environment\nhashref. (This feature requires Devel::LexAlias.) If set to false, the coderef closes over a\n*shallow copy* of the variables.\n\nIf this argument is omitted, Eval::Closure will currently assume false, but this assumption\nmay change in a future version.\n\ndescription\nThis lets you provide a bit more information in backtraces. Normally, when a function that\nwas generated through string eval is called, that stack frame will show up as \"(eval n)\",\nwhere 'n' is a sequential identifier for every string eval that has happened so far in the\nprogram. Passing a \"description\" parameter lets you override that to something more useful\n(for instance, Moose overrides the description for accessors to something like \"accessor foo\nat MyClass.pm, line 123\").\n\nline\nThis lets you override the particular line number that appears in backtraces, much like the\n\"description\" option. The default is 1.\n\nterseerror\nNormally, this function appends the source code that failed to compile, and prepends some\nexplanatory text. Setting this option to true suppresses that behavior so you get only the\ncompilation error that Perl actually reported.\n",
            "subsections": []
        },
        "BUGS": {
            "content": "No known bugs.\n\nPlease report any bugs to GitHub Issues at <https://github.com/doy/eval-closure/issues>.\n",
            "subsections": []
        },
        "SEE ALSO": {
            "content": "*   Class::MOP::Method::Accessor\n\nThis module is a factoring out of code that used to live here\n",
            "subsections": []
        },
        "SUPPORT": {
            "content": "You can find this documentation for this module with the perldoc command.\n\nperldoc Eval::Closure\n\nYou can also look for information at:\n\n*   MetaCPAN\n\n<https://metacpan.org/release/Eval-Closure>\n\n*   Github\n\n<https://github.com/doy/eval-closure>\n\n*   RT: CPAN's request tracker\n\n<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Eval-Closure>\n\n*   CPAN Ratings\n\n<http://cpanratings.perl.org/d/Eval-Closure>\n",
            "subsections": []
        },
        "NOTES": {
            "content": "Based on code from Class::MOP::Method::Accessor, by Stevan Little and the Moose Cabal.\n",
            "subsections": []
        },
        "AUTHOR": {
            "content": "Jesse Luehrs <doy@tozt.net>\n",
            "subsections": []
        },
        "COPYRIGHT AND LICENSE": {
            "content": "This software is copyright (c) 2016 by Jesse Luehrs.\n\nThis is free software; you can redistribute it and/or modify it under the same terms as the Perl\n5 programming language system itself.\n",
            "subsections": []
        }
    },
    "summary": "Eval::Closure - safely and cleanly create closures via string eval",
    "flags": [],
    "examples": [],
    "see_also": []
}