Data::Sah::Compiler::perl - Compile Sah schema to Perl code
This document describes version 0.74 of Data::Sah::Compiler::perl (from Perl distribution Data-Sah), released on 2015-10-18.
# see Data::Sah
Derived from Data::Sah::Compiler::Prog.
This subclass adds the following compilation data ($cd).
$cd
Keys which contain compilation result:
module_statements => HASH
This hash, keyed by module name, lets the Perl compiler differentiate on the different statements to use when loading modules, e.g.:
{ "Foo" => undef, # or does not exist "Bar::Baz" => ['use'], "Qux" => ['use', []], "Quux" => ['use', ["'a'", 123]], "warnings" => ['no'], }
will lead to these codes (in the order specified by $cd->{modules}, BTW) being generated:
$cd->{modules}
require Foo; use Bar::Baz; use Qux (); use Quux ('a', 123); no warnings;
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 Prog's arguments, this class supports these arguments:
pp => bool (default: 0)
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: 0)
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: 0)
If set to true, will stick to using only core or PP modules in the generated code.
This is like add_module(), but indicate that $module needs to be use-d in the generated code (for example, Perl pragmas). Normally if add_module() is used, the generated code will use require.
add_module()
$module
use
require
If you use $c->add_use($cd, 'foo'), this code will be generated:
$c->add_use($cd, 'foo')
use foo;
If you use $c->add_use($cd, 'foo', ["'a'", "'b'", "123"]), this code will be generated:
$c->add_use($cd, 'foo', ["'a'", "'b'", "123"])
use foo ('a', 'b', 123);
If you use $c->add_use($cd, 'foo', []), this code will be generated:
$c->add_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_use(), to generate no foo statement.
add_use()
no foo
See also: add_use().
Equivalent to:
$c->add_use($cd, 'experimental', ["'smartmatch'"]);
Add Scalar::Util::Numeric module, or Scalar::Util::Numeric::PP when pp compile argument is true.
pp
Set default for pp compile argument. Takes precedence over environment DATA_SAH_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
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.
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", ...
Please visit the project's homepage at https://metacpan.org/release/Data-Sah.
Source repository is at https://github.com/sharyanto/perl-Data-Sah.
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.
perlancar <perlancar@cpan.org>
This software is copyright (c) 2015 by 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.
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.