Data::Sah::Compiler::perl - Compile Sah schema to Perl code
This document describes version 0.913 of Data::Sah::Compiler::perl (from Perl distribution Data-Sah), released on 2022-09-30.
# see Data::Sah
Derived from Data::Sah::Compiler::Prog.
Set default for pp compile argument. Takes precedence over environment DATA_SAH_PP.
pp
DATA_SAH_PP
Set default for core compile argument. Takes precedence over environment DATA_SAH_CORE.
core
DATA_SAH_CORE
Set default for core_or_pp compile argument. Takes precedence over environment DATA_SAH_CORE_OR_PP.
core_or_pp
DATA_SAH_CORE_OR_PP
Set default for no_modules compile argument. Takes precedence over environment DATA_SAH_NO_MODULES.
no_modules
DATA_SAH_NO_MODULES
To generate expression code that says "all subexpression must be true", you can do:
!defined(List::Util::first(sub { blah($_) }, "value", ...))
This is a bit harder to read than:
!grep { !blah($_) } "value", ...
but has the advantage of the ability to shortcut on the first item that fails.
Similarly, to say "at least one subexpression must be true":
defined(List::Util::first(sub { blah($_) }, "value", ...))
which can shortcut in contrast to:
grep { blah($_) } "value", ...
This subclass adds the following compilation data ($cd).
$cd
Keys which contain compilation result:
Generate a comment. For example, in perl compiler:
$c->comment($cd, "123"); # -> "# 123\n"
Will return an empty string if compile argument comment is set to false.
comment
Aside from arguments known by the base class (Data::Sah::Compiler::Prog), this class supports these arguments:
Bool, default false. If set to true, will avoid the use of XS modules in the generated code and will opt instead to use pure-perl modules.
Bool, default false. If set to true, will avoid the use of non-core modules in the generated code and will opt instead to use core modules.
Bool, default false. If set to true, will stick to using only core or PP modules in the generated code.
whitelist_modules
Array of str. When pp/core/core_or_pp option is set to true, the use of non-appropriate modules will cause failure. However, you can pass a list of modules that are allowed nevertheless.
This is like add_runtime_module(), but indicate that $module needs to be use-d in the generated code (for example, Perl pragmas). Normally if add_runtime_module() is used, the generated code will use require.
add_runtime_module()
$module
use
require
If you use $c->add_runtime_use($cd, 'foo'), this code will be generated:
$c->add_runtime_use($cd, 'foo')
use foo;
If you use $c->add_runtime_use($cd, 'foo', ["'a'", "'b'", "123"]), this code will be generated:
$c->add_runtime_use($cd, 'foo', ["'a'", "'b'", "123"])
use foo ('a', 'b', 123);
If you use $c->add_runtime_use($cd, 'foo', []), this code will be generated:
$c->add_runtime_use($cd, 'foo', [])
use foo ();
The generated statement will be added at the top (top-level lexical scope) and duplicates are ignored. To generate multiple and lexically-scoped use and no statements, e.g. like below, currently you can generate them manually:
no
if (blah) { no warnings; ... }
This is the counterpart of add_runtime_use(), to generate no foo statement.
add_runtime_use()
no foo
See also: add_runtime_use().
Add Scalar::Util::Numeric module, or Scalar::Util::Numeric::PP when pp compile argument is true.
Set default for pp compile argument.
Set default for core compile argument.
Set default for core_or_pp compile argument.
Set default for no_modules compile argument.
Please visit the project's homepage at https://metacpan.org/release/Data-Sah.
Source repository is at https://github.com/perlancar/perl-Data-Sah.
perlancar <perlancar@cpan.org>
To contribute, you can send patches by email/via RT, or send pull requests on GitHub.
Most of the time, you don't need to build the distribution yourself. You can simply modify the code, then test via:
% prove -l
If you want to build the distribution (e.g. to try to install it locally on your system), you can install Dist::Zilla, Dist::Zilla::PluginBundle::Author::PERLANCAR, Pod::Weaver::PluginBundle::Author::PERLANCAR, and sometimes one or two other Dist::Zilla- and/or Pod::Weaver plugins. Any additional steps required beyond that are considered a bug and can be reported to me.
This software is copyright (c) 2022, 2021, 2020, 2019, 2018, 2017, 2016, 2015, 2014, 2013, 2012 by perlancar <perlancar@cpan.org>.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
Please report any bugs or feature requests on the bugtracker website https://rt.cpan.org/Public/Dist/Display.html?Name=Data-Sah
When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.
To install Data::Sah, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Data::Sah
CPAN shell
perl -MCPAN -e shell install Data::Sah
For more information on module installation, please visit the detailed CPAN module installation guide.