| 1 | package Sah::Schema::language::code::alpha3; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY | |||||||||
| 4 | 1 | our $DATE = '2020-05-27'; # DATE | |||||||||
| 5 | 1 | our $DIST = 'Sah-Schemas-Language'; # DIST | |||||||||
| 6 | 1 | our $VERSION = '0.004'; # VERSION | |||||||||
| 7 | |||||||||||
| 8 | 1 | ▶ | use Locale::Codes::Language_Codes (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | my $codes = [sort ( | ||||||||||
| 11 | 1 | keys(%{ $Locale::Codes::Data{'language'}{'code2id'}{'alpha-3'} }), | |||||||||
| 12 | )]; | ||||||||||
| 13 | 1 | ▶ | die "Can't extract language codes from Locale::Codes::Language_Codes" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 14 | unless @$codes; | ||||||||||
| 15 | |||||||||||
| 16 | 1 | our $schema = [str => { | |||||||||
| 17 | summary => 'Language code (alpha-3)', | ||||||||||
| 18 | description => <<'_', | ||||||||||
| 19 | |||||||||||
| 20 | Accept only current (not retired) codes. Only alpha-3 codes are accepted. | ||||||||||
| 21 | |||||||||||
| 22 | _ | ||||||||||
| 23 | match => '\A[a-z]{3}\z', | ||||||||||
| 24 | in => $codes, | ||||||||||
| 25 | 'x.perl.coerce_rules' => ['From_str::to_lower'], | ||||||||||
| 26 | |||||||||||
| 27 | examples => [ | ||||||||||
| 28 | {value=>"", valid=>0}, | ||||||||||
| 29 | {value=>"id", valid=>0, summary=>"Indonesian (2 letter, rejected)"}, | ||||||||||
| 30 | {value=>"IND", valid=>1, validated_value=>"ind", summary=>"Indonesian (3 letter)"}, | ||||||||||
| 31 | {value=>"qqq", valid=>0, summary=>"Unknown language code"}, | ||||||||||
| 32 | ], | ||||||||||
| 33 | |||||||||||
| 34 | }, {}]; | ||||||||||
| 35 | |||||||||||
| 36 | 1 | 1; | |||||||||
| 37 | # ABSTRACT: Language code (alpha-3) | ||||||||||
| 38 | |||||||||||
| 39 | __END__ | ||||||||||
| 40 | |||||||||||
| 41 | =pod | ||||||||||
| 42 | |||||||||||
| 43 | =encoding UTF-8 | ||||||||||
| 44 | |||||||||||
| 45 | =head1 NAME | ||||||||||
| 46 | |||||||||||
| 47 | Sah::Schema::language::code::alpha3 - Language code (alpha-3) | ||||||||||
| 48 | |||||||||||
| 49 | =head1 VERSION | ||||||||||
| 50 | |||||||||||
| 51 | This document describes version 0.004 of Sah::Schema::language::code::alpha3 (from Perl distribution Sah-Schemas-Language), released on 2020-05-27. | ||||||||||
| 52 | |||||||||||
| 53 | =head1 SYNOPSIS | ||||||||||
| 54 | |||||||||||
| 55 | To check data against this schema (requires L<Data::Sah>): | ||||||||||
| 56 | |||||||||||
| 57 | use Data::Sah qw(gen_validator); | ||||||||||
| 58 | my $validator = gen_validator("language::code::alpha3*"); | ||||||||||
| 59 | say $validator->($data) ? "valid" : "INVALID!"; | ||||||||||
| 60 | |||||||||||
| 61 | # Data::Sah can also create validator that returns nice error message string | ||||||||||
| 62 | # and/or coerced value. Data::Sah can even create validator that targets other | ||||||||||
| 63 | # language, like JavaScript. All from the same schema. See its documentation | ||||||||||
| 64 | # for more details. | ||||||||||
| 65 | |||||||||||
| 66 | To validate function parameters against this schema (requires L<Params::Sah>): | ||||||||||
| 67 | |||||||||||
| 68 | use Params::Sah qw(gen_validator); | ||||||||||
| 69 | |||||||||||
| 70 | sub myfunc { | ||||||||||
| 71 | my @args = @_; | ||||||||||
| 72 | state $validator = gen_validator("language::code::alpha3*"); | ||||||||||
| 73 | $validator->(\@args); | ||||||||||
| 74 | ... | ||||||||||
| 75 | } | ||||||||||
| 76 | |||||||||||
| 77 | To specify schema in L<Rinci> function metadata and use the metadata with | ||||||||||
| 78 | L<Perinci::CmdLine> to create a CLI: | ||||||||||
| 79 | |||||||||||
| 80 | # in lib/MyApp.pm | ||||||||||
| 81 | package MyApp; | ||||||||||
| 82 | our %SPEC; | ||||||||||
| 83 | $SPEC{myfunc} = { | ||||||||||
| 84 | v => 1.1, | ||||||||||
| 85 | summary => 'Routine to do blah ...', | ||||||||||
| 86 | args => { | ||||||||||
| 87 | arg1 => { | ||||||||||
| 88 | summary => 'The blah blah argument', | ||||||||||
| 89 | schema => ['language::code::alpha3*'], | ||||||||||
| 90 | }, | ||||||||||
| 91 | ... | ||||||||||
| 92 | }, | ||||||||||
| 93 | }; | ||||||||||
| 94 | sub myfunc { | ||||||||||
| 95 | my %args = @_; | ||||||||||
| 96 | ... | ||||||||||
| 97 | } | ||||||||||
| 98 | 1; | ||||||||||
| 99 | |||||||||||
| 100 | # in myapp.pl | ||||||||||
| 101 | package main; | ||||||||||
| 102 | use Perinci::CmdLine::Any; | ||||||||||
| 103 | Perinci::CmdLine::Any->new(url=>'MyApp::myfunc')->run; | ||||||||||
| 104 | |||||||||||
| 105 | # in command-line | ||||||||||
| 106 | % ./myapp.pl --help | ||||||||||
| 107 | myapp - Routine to do blah ... | ||||||||||
| 108 | ... | ||||||||||
| 109 | |||||||||||
| 110 | % ./myapp.pl --version | ||||||||||
| 111 | |||||||||||
| 112 | % ./myapp.pl --arg1 ... | ||||||||||
| 113 | |||||||||||
| 114 | Sample data: | ||||||||||
| 115 | |||||||||||
| 116 | "" # INVALID | ||||||||||
| 117 | |||||||||||
| 118 | "id" # INVALID (Indonesian (2 letter, rejected)) | ||||||||||
| 119 | |||||||||||
| 120 | "IND" # valid, becomes "ind" | ||||||||||
| 121 | |||||||||||
| 122 | "qqq" # INVALID (Unknown language code) | ||||||||||
| 123 | |||||||||||
| 124 | =head1 DESCRIPTION | ||||||||||
| 125 | |||||||||||
| 126 | Accept only current (not retired) codes. Only alpha-3 codes are accepted. | ||||||||||
| 127 | |||||||||||
| 128 | =head1 HOMEPAGE | ||||||||||
| 129 | |||||||||||
| 130 | Please visit the project's homepage at L<https://metacpan.org/release/Sah-Schemas-Language>. | ||||||||||
| 131 | |||||||||||
| 132 | =head1 SOURCE | ||||||||||
| 133 | |||||||||||
| 134 | Source repository is at L<https://github.com/perlancar/perl-Sah-Schemas-Language>. | ||||||||||
| 135 | |||||||||||
| 136 | =head1 BUGS | ||||||||||
| 137 | |||||||||||
| 138 | Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=Sah-Schemas-Language> | ||||||||||
| 139 | |||||||||||
| 140 | When submitting a bug or request, please include a test-file or a | ||||||||||
| 141 | patch to an existing test-file that illustrates the bug or desired | ||||||||||
| 142 | feature. | ||||||||||
| 143 | |||||||||||
| 144 | =head1 SEE ALSO | ||||||||||
| 145 | |||||||||||
| 146 | L<Sah::Schema::language::code::alpha2> | ||||||||||
| 147 | |||||||||||
| 148 | L<Sah::Schema::language::code> | ||||||||||
| 149 | |||||||||||
| 150 | =head1 AUTHOR | ||||||||||
| 151 | |||||||||||
| 152 | perlancar <perlancar@cpan.org> | ||||||||||
| 153 | |||||||||||
| 154 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 155 | |||||||||||
| 156 | This software is copyright (c) 2020, 2019, 2018 by perlancar@cpan.org. | ||||||||||
| 157 | |||||||||||
| 158 | This is free software; you can redistribute it and/or modify it under | ||||||||||
| 159 | the same terms as the Perl 5 programming language system itself. | ||||||||||
| 160 | |||||||||||
| 161 | =cut | ||||||||||