![]() |
![]()
| ![]() |
![]()
NAMEData::Sah::Coerce - Coercion rules for Data::Sah VERSIONThis document describes version 0.054 of Data::Sah::Coerce (from Perl distribution Data-Sah-Coerce), released on 2023-10-24. SYNOPSISuse Data::Sah::Coerce qw(gen_coercer); # a utility routine: gen_coercer my $c = gen_coercer( type => 'date', coerce_to => 'DateTime', coerce_rules => ['From_str::natural'], # explicitly enable a rule, etc. See Data::Sah::CoerceCommon's get_coerce_rules() for detailed syntax # return_type => 'str+val', # default is 'val' ); my $val = $c->(123); # unchanged, 123 my $val = $c->(1463307881); # becomes a DateTime object my $val = $c->("2016-05-15"); # becomes a DateTime object my $val = $c->("2016foo"); # unchanged, "2016foo" DESCRIPTIONThis distribution contains a standard set of coercion rules for Data::Sah. It is separated from the "Data-Sah" distribution and can be used independently. A coercion rule is put in "Data::Sah::Coerce::$COMPILER::To_$TARGET_TYPE::From_$SOURCE_TYPE::DESCRIPTION" module, for example: Data::Sah::Coerce::perl::To_date::From_float::epoch for converting date from integer (Unix epoch) or Data::Sah::Coerce::perl::To_date::From_str::iso8601 for converting date from ISO8601 strings like "2016-05-15". Basically, a coercion rule will provide an expression ("expr_match") that evaluates to true when data can be coerced, and an expression ("expr_coerce") to actually coerce/convert data to the target type. This rule can be combined with other rules to form the final coercion code. The module must contain "meta" subroutine which must return a hashref that has the following keys ("*" marks that the key is required):
The module must also contain "coerce" subroutine which must generate the code for coercion. The subroutine must accept a hash of arguments ("*" indicates required arguments):
The "coerce" subroutine must return a hashref with the following keys ("*" indicates required keys):
Basically, the "coerce" subroutine must generates a code that accepts a non-undef data and must convert this data to the desired type/format under the right condition. The code to match the right condition must be put in "expr_match" and the code to convert data must be put in "expr_coerce". Program/library that uses Data::Sah::Coerce can collect rules from the rule modules then compose them into the final code, something like (in pseudocode): if (data is undef) { return undef; } elsif (data matches expr-match-from-rule1) { return expr-coerce-from-rule1; } elsif (data matches expr-match-from-rule2) { return expr-coerce-from-rule1; ... } else { # does not match any expr-match return original data; } VARIABLES$Log_Coercer_Code => bool (default: from ENV or 0)If set to true, will log the generated coercer code (currently using Log::ger at trace level). To see the log message, e.g. to the screen, you can use something like: % TRACE=1 perl -MLog::ger::LevelFromEnv -MLog::ger::Output=Screen \ -MData::Sah::Coerce=gen_coercer -E'my $c = gen_coercer(...)' FUNCTIONSgen_coercerUsage: gen_coercer(%args) -> any Generate coercer code. This is mostly for testing. Normally the coercion rules will be used from Data::Sah. This function is not exported by default, but exportable. Arguments ('*' denotes required arguments):
Return value: (any) ENVIRONMENTLOG_SAH_COERCER_CODE => boolSet default for $Log_Coercer_Code. HOMEPAGEPlease visit the project's homepage at <https://metacpan.org/release/Data-Sah-Coerce>. SOURCESource repository is at <https://github.com/perlancar/perl-Data-Sah-Coerce>. SEE ALSOData::Sah::CoerceCommon for detailed syntax of coerce rules (explicitly including/excluding rules etc). Data::Sah Data::Sah::CoerceJS App::SahUtils, including coerce-with-sah to conveniently test coercion from the command-line. AUTHORperlancar <perlancar@cpan.org> CONTRIBUTINGTo 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 LICENSEThis software is copyright (c) 2023, 2022, 2021, 2020, 2019, 2018, 2017, 2016 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. BUGSPlease report any bugs or feature requests on the bugtracker website <https://rt.cpan.org/Public/Dist/Display.html?Name=Data-Sah-Coerce> 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.
|