The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Data::Sah::Compiler::perl - Compile Sah schema to Perl code

VERSION

This document describes version 0.46 of Data::Sah::Compiler::perl (from Perl distribution Data-Sah), released on 2015-02-21.

SYNOPSIS

 # see Data::Sah

DESCRIPTION

Derived from Data::Sah::Compiler::Prog.

METHODS

new() => OBJ

Compilation data

This subclass adds the following compilation data ($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:

     require Foo;
     use Bar::Baz;
     use Qux ();
     use Quux ('a', 123);
     no warnings;

$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 Prog's arguments, this class supports these arguments:

$c->add_use($cd, $module, \@imports)

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.

If you use $c->add_use($cd, 'foo'), this code will be generated:

 use foo;

If you use $c->add_use($cd, 'foo', ["'a'", "'b'", "123"]), this code will be generated:

 use foo ('a', 'b', 123);

If you use $c->add_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_no($cd, $module)

This is the counterpart of add_use(), to generate <no foo> statement.

See also: add_use().

$c->add_smartmatch_pragma($cd)

Equivalent to:

 $c->add_use($cd, 'experimental', ["'smartmatch'"]);

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", ...

HOMEPAGE

Please visit the project's homepage at https://metacpan.org/release/Data-Sah.

SOURCE

Source repository is at https://github.com/sharyanto/perl-Data-Sah.

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.

AUTHOR

perlancar <perlancar@cpan.org>

COPYRIGHT AND LICENSE

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.