![]() |
![]()
| ![]() |
![]()
NAMEData::Sah::Filter - Filtering for Data::Sah VERSIONThis document describes version 0.025 of Data::Sah::Filter (from Perl distribution Data-Sah-Filter), released on 2024-07-17. SYNOPSISuse Data::Sah::Filter qw(gen_filter); # a utility routine: gen_filter my $c = gen_filter( filter_names => ['Str::ltrim', 'Str::rtrim'], ); my $val = $c->("foo"); # unchanged, "foo" my $val = $c->(" foo "); # "foo" Another example: my $c = gen_filter( filter_names => [ ['Str::remove_comment' => {style=>'shell'}] ], #filter_names => ['Str::remove_comment=style,shell'], # same as above ); DESCRIPTIONThis distribution contains a standard set of filter rules for Data::Sah (to be used in "prefilters" and "postfilters" clauses). It is separated from the "Data-Sah" distribution and can be used independently. A filter rule is put in "Data::Sah::Filter::$COMPILER::$CATEGORY:$DESCRIPTION" module, for example: Data::Sah::Filter::perl::Str::trim for trimming whitespace at the beginning and end of string. Basically, a filter rule will provide an expression (in "expr_filter") in the target language (e.g. Perl, JavaScript, or others) to convert one data to another. Multiple filter rules can be combined to form the final filtering code. This code can be used by "Data::Sah" when generating validator code from Sah schema, or can be used directly. Some projects which use filtering rules directly include: App::orgadb (which lets users specify filters from the command-line). meta()The filter rule module must contain "meta" subroutine which must return a hashref (DefHash) that has the following keys ("*" marks that the key is required):
filter()The filter rule module must also contain "filter" subroutine which must generate the code for filtering. The subroutine must accept a hash of arguments and will be passed these:
The "filter" subroutine must return a hashref with the following keys ("*" indicates required keys):
Basically, the "filter" subroutine must generate a code that accepts a non-undef data and must convert this data to the desired value. Program/library that uses Data::Sah::Filter can collect rules from the rule modules then compose them into the final code, something like (in pseudo-Perl code): if (!defined $data) { return undef; } else { $data = expr-filter-from-rule1($data); $data = expr-filter-from-rule2($data); ... return $data; } Filter modules included in this distribution
VARIABLES$Log_Filter_Code => bool (default: from ENV or 0)If set to true, will log the generated filter 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::Filter=gen_filter -E'my $c = gen_filter(...)' FUNCTIONSgen_filterUsage: gen_filter(%args) -> any Generate filter code. This is mostly for testing. Normally the filter 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_FILTER_CODE => boolSet default for $Log_Filter_Code. HOMEPAGEPlease visit the project's homepage at <https://metacpan.org/release/Data-Sah-Filter>. SOURCESource repository is at <https://github.com/perlancar/perl-Data-Sah-Filter>. SEE ALSOData::Sah Data::Sah::FilterJS App::SahUtils, including filter-with-sah to conveniently test filter from the command-line. Data::Sah::Coerce. Filtering works very similarly to coercion in the Data::Sah framework (see l<Data::Sah::Coerce>) but is simpler and composited differently to form the final filtering code. Mainly, input data will be passed to all filtering expressions. 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) 2024, 2023, 2022, 2020 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-Filter> 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.
|