Schema/language/code.pm

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