1Eval::TypeTiny(3) User Contributed Perl Documentation Eval::TypeTiny(3)
2
3
4
6 Eval::TypeTiny - utility to evaluate a string of Perl code in a clean
7 environment
8
10 This module is covered by the Type-Tiny stability policy.
11
13 This module is used by Type::Tiny to compile coderefs from strings of
14 Perl code, and hashrefs of variables to close over.
15
16 Functions
17 By default this module exports one function, which works much like the
18 similarly named function from Eval::Closure:
19
20 "eval_closure(source => $source, environment => \%env, %opt)"
21
22 Other functions can be imported on request:
23
24 "set_subname( $fully_qualified_name, $coderef )"
25 Works like the similarly named function from Sub::Util, but will
26 fallback to doing nothing if neither Sub::Util nor Sub::Name are
27 available. Also will cowardly refuse the set the name of a coderef
28 a second time if it's already named it.
29
30 "type_to_coderef( $type, %options )"
31 Turns a Type::Tiny object into a coderef, suitable for installing
32 into a symbol table to create a function like "ArrayRef" or "Int".
33 (Actually should work for any object which provides
34 "is_parameterizable", "parameterize", and "qualified_name" methods,
35 such as Type::Coercion.)
36
37 $options{post_method} can be a string of Perl indicating a method
38 to call on the type constraint before returning it. For example
39 '->moose_type'.
40
41 $options{description} can be a description of the coderef which may
42 be shown in stack traces, etc.
43
44 The coderef will be named using "set_subname" unless
45 $options{anonymous} is true.
46
47 If $type is undef, then it is assumed that the type constraint
48 hasn't been defined yet but will later, yet you still want a
49 function now. $options{type_library} and $options{type_name} will
50 be used to find the type constraint when the function gets called.
51
52 Constants
53 The following constants may be exported, but are not by default.
54
55 "HAS_LEXICAL_SUBS"
56 Boolean indicating whether Eval::TypeTiny has support for lexical
57 subs. (This feature requires Perl 5.18.)
58
59 "ALIAS_IMPLEMENTATION"
60 Returns a string indicating what implementation of "alias => 1" is
61 being used. Eval::TypeTiny will automatically choose the best
62 implementation. This constant can be matched against the
63 "IMPLEMENTAION_*" constants.
64
65 "IMPLEMENTATION_NATIVE"
66 If "ALIAS_IMPLEMENTATION eq IMPLEMENTATION_NATIVE" then
67 Eval::TypeTiny is currently using Perl 5.22's native alias feature.
68 This requires Perl 5.22.
69
70 "IMPLEMENTATION_DEVEL_LEXALIAS"
71 If "ALIAS_IMPLEMENTATION eq IMPLEMENTATION_DEVEL_LEXALIAS" then
72 Eval::TypeTiny is currently using Devel::LexAlias to provide
73 aliases.
74
75 "IMPLEMENTATION_PADWALKER"
76 If "ALIAS_IMPLEMENTATION eq IMPLEMENTATION_PADWALKER" then
77 Eval::TypeTiny is currently using PadWalker to provide aliases.
78
79 "IMPLEMENTATION_TIE"
80 If "ALIAS_IMPLEMENTATION eq IMPLEMENTATION_TIE" then Eval::TypeTiny
81 is using the fallback implementation of aliases using "tie". This
82 is the slowest implementation, and may cause problems in certain
83 edge cases, like trying to alias already-tied variables, but it's
84 the only way to implement "alias => 1" without a recent version of
85 Perl or one of the two optional modules mentioned above.
86
87 "NICE_PROTOTYPES"
88 If this is true, then type_to_coderef will give parameterizable
89 type constraints the slightly nicer prototype of "(;$)" instead of
90 the default "(;@)". This allows constructs like:
91
92 ArrayRef[Int] | HashRef[Int]
93
94 ... to "just work".
95
97 The evaluation is performed in the presence of strict, but the absence
98 of warnings. (This is different to Eval::Closure which enables warnings
99 for compiled closures.)
100
101 The feature pragma is not active in the evaluation environment, so the
102 following will not work:
103
104 use feature qw(say);
105 use Eval::TypeTiny qw(eval_closure);
106
107 my $say_all = eval_closure(
108 source => 'sub { say for @_ }',
109 );
110 $say_all->("Hello", "World");
111
112 The feature pragma does not "carry over" into the stringy eval. It is
113 of course possible to import pragmas into the evaluated string as part
114 of the string itself:
115
116 use Eval::TypeTiny qw(eval_closure);
117
118 my $say_all = eval_closure(
119 source => 'sub { use feature qw(say); say for @_ }',
120 );
121 $say_all->("Hello", "World");
122
124 Please report any bugs to
125 <https://github.com/tobyink/p5-type-tiny/issues>.
126
128 Eval::Closure, Error::TypeTiny::Compilation.
129
131 Toby Inkster <tobyink@cpan.org>.
132
134 This software is copyright (c) 2013-2014, 2017-2023 by Toby Inkster.
135
136 This is free software; you can redistribute it and/or modify it under
137 the same terms as the Perl 5 programming language system itself.
138
140 THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
141 WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
142 MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
143
144
145
146perl v5.38.0 2023-07-21 Eval::TypeTiny(3)