NAME
Data::Sah::Compiler::perl - Compile Sah schema to Perl code
VERSION
This document describes version 0.916 of Data::Sah::Compiler::perl (from Perl distribution Data-Sah), released on 2024-02-16.
SYNOPSIS
# see Data::Sah
DESCRIPTION
Derived from Data::Sah::Compiler::Prog.
VARIABLES
$PP => bool
Set default for pp
compile argument. Takes precedence over environment DATA_SAH_PP
.
$CORE => bool
Set default for core
compile argument. Takes precedence over environment DATA_SAH_CORE
.
$CORE_OR_PP => bool
Set default for core_or_pp
compile argument. Takes precedence over environment DATA_SAH_CORE_OR_PP
.
$NO_MODULES => bool
Set default for no_modules
compile argument. Takes precedence over environment DATA_SAH_NO_MODULES
.
DEVELOPER NOTES
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", ...
METHODS
new() => OBJ
Compilation data
This subclass adds the following compilation data ($cd
).
Keys which contain compilation result:
$c->comment($cd, @args) => STR
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.
$c->compile(%args) => RESULT
Aside from arguments known by the base class (Data::Sah::Compiler::Prog), this class supports these arguments:
pp
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.
core
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.
core_or_pp
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.
$c->add_runtime_use($cd, $module [, \@import_terms ])
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
.
If you use $c->add_runtime_use($cd, 'foo')
, this code will be generated:
use foo;
If you use $c->add_runtime_use($cd, 'foo', ["'a'", "'b'", "123"])
, this code will be generated:
use foo ('a', 'b', 123);
If you use $c->add_runtime_use($cd, 'foo', [])
, this code will be generated:
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:
if (blah) {
no warnings;
...
}
$c->add_runtime_no($cd, $module [, \@import_terms ])
This is the counterpart of add_runtime_use()
, to generate no foo
statement.
See also: add_runtime_use()
.
$c->add_sun_module($cd)
Add Scalar::Util::Numeric module, or Scalar::Util::Numeric::PP when pp
compile argument is true.
ENVIRONMENT
DATA_SAH_PP => bool
Set default for pp
compile argument.
DATA_SAH_CORE => bool
Set default for core
compile argument.
DATA_SAH_CORE_OR_PP => bool
Set default for core_or_pp
compile argument.
DATA_SAH_NO_MODULES => bool
Set default for no_modules
compile argument.
HOMEPAGE
Please visit the project's homepage at https://metacpan.org/release/Data-Sah.
SOURCE
Source repository is at https://github.com/perlancar/perl-Data-Sah.
AUTHOR
perlancar <perlancar@cpan.org>
CONTRIBUTING
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.
COPYRIGHT AND LICENSE
This software is copyright (c) 2024, 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.
BUGS
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.