| 1 | package Sah::Schema::code_from_str; | ||
| 2 | |||
| 3 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | 1 | our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY | |
| 6 | 1 | our $DATE = '2024-06-10'; # DATE | |
| 7 | 1 | our $DIST = 'Sah-SchemaBundle-Code'; # DIST | |
| 8 | 1 | our $VERSION = '0.004'; # VERSION | |
| 9 | |||
| 10 | 1 | our $schema = [ | |
| 11 | code => { | ||
| 12 | summary => 'Coderef from eval\`ed string', | ||
| 13 | description => <<'MARKDOWN', | ||
| 14 | |||
| 15 | This schema accepts coderef or string which will be eval'ed to coderef. Note | ||
| 16 | that this means allowing your user to provide arbitrary Perl code for you to | ||
| 17 | execute! Make sure first and foremost that security-wise this is acceptable in | ||
| 18 | your case. | ||
| 19 | |||
| 20 | By default `eval()` is performed in the `main` namespace and without stricture | ||
| 21 | or warnings. See the parameterized version <pm:Sah::PSchema::code_from_str> if | ||
| 22 | you want to customize the `eval()`. | ||
| 23 | |||
| 24 | What's the difference between this schema and `str_or_code` (from | ||
| 25 | <pm:Sah::Schemas::Str>)? Both this schema and `str_or_code` accept string, but | ||
| 26 | this schema will directly compile any input string while `str_or_code` will only | ||
| 27 | convert string to code if it is in the form of `sub { ... }`. In other words, | ||
| 28 | this schema will always produce coderef, while `str_or_code` can produce strings | ||
| 29 | also. | ||
| 30 | |||
| 31 | MARKDOWN | ||
| 32 | |||
| 33 | prefilters => [ ['Code::eval'=>{}] ], | ||
| 34 | |||
| 35 | examples => [ | ||
| 36 | ], | ||
| 37 | }, | ||
| 38 | ]; | ||
| 39 | |||
| 40 | 1 | 1; | |
| 41 | # ABSTRACT: Coderef from string | ||
| 42 | |||
| 43 | __END__ | ||