File Coverage

blib/lib/Sah/Schema/perl/unqualified_funcname.pm
Criterion Covered Total %
statement 6 6 100.0
branch n/a
condition n/a
subroutine 2 2 100.0
pod n/a
total 8 8 100.0


line stmt bran cond sub pod time code
1             package Sah::Schema::perl::unqualified_funcname;
2              
3 1     1   45098 use strict;
  1         3  
  1         30  
4 1     1   5 use warnings;
  1         3  
  1         110  
5              
6             our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
7             our $DATE = '2022-12-16'; # DATE
8             our $DIST = 'Sah-Schemas-Perl'; # DIST
9             our $VERSION = '0.046'; # VERSION
10              
11             our $schema = [str => {
12             summary => 'Perl function name which must not be qualified with a package name, e.g. subname',
13             description => <<'_',
14              
15             Currently function name is restricted to this regex:
16              
17             \A[A-Za-z_][A-Za-z_0-9]*\z
18              
19             This schema includes syntax validity check only; it does not check whether the
20             function actually exists.
21              
22             This schema includes syntax validity check only; it does not check whether the
23             function actually exists.
24              
25             _
26             match => '\A[A-Za-z_]([A-Za-z_0-9]+)*\z',
27              
28             # TODO: provide convenience by providing list of core function names etc
29             #'x.completion' => 'perl_funcname',
30              
31             }];
32              
33             1;
34             # ABSTRACT: Perl function name which must not be qualified with a package name, e.g. subname
35              
36             __END__
37              
38             =pod
39              
40             =encoding UTF-8
41              
42             =head1 NAME
43              
44             Sah::Schema::perl::unqualified_funcname - Perl function name which must not be qualified with a package name, e.g. subname
45              
46             =head1 VERSION
47              
48             This document describes version 0.046 of Sah::Schema::perl::unqualified_funcname (from Perl distribution Sah-Schemas-Perl), released on 2022-12-16.
49              
50             =head1 SYNOPSIS
51              
52             =head2 Using with Data::Sah
53              
54             To check data against this schema (requires L<Data::Sah>):
55              
56             use Data::Sah qw(gen_validator);
57             my $validator = gen_validator("perl::unqualified_funcname*");
58             say $validator->($data) ? "valid" : "INVALID!";
59              
60             The above schema returns a boolean result (true if data is valid, false if
61             otherwise). To return an error message string instead (empty string if data is
62             valid, a non-empty error message otherwise):
63              
64             my $validator = gen_validator("perl::unqualified_funcname", {return_type=>'str_errmsg'});
65             my $errmsg = $validator->($data);
66              
67             Often a schema has coercion rule or default value, so after validation the
68             validated value is different. To return the validated (set-as-default, coerced,
69             prefiltered) value:
70              
71             my $validator = gen_validator("perl::unqualified_funcname", {return_type=>'str_errmsg+val'});
72             my $res = $validator->($data); # [$errmsg, $validated_val]
73              
74             Data::Sah can also create validator that returns a hash of detailed error
75             message. Data::Sah can even create validator that targets other language, like
76             JavaScript, from the same schema. Other things Data::Sah can do: show source
77             code for validator, generate a validator code with debug comments and/or log
78             statements, generate human text from schema. See its documentation for more
79             details.
80              
81             =head2 Using with Params::Sah
82              
83             To validate function parameters against this schema (requires L<Params::Sah>):
84              
85             use Params::Sah qw(gen_validator);
86              
87             sub myfunc {
88             my @args = @_;
89             state $validator = gen_validator("perl::unqualified_funcname*");
90             $validator->(\@args);
91             ...
92             }
93              
94             =head2 Using with Perinci::CmdLine::Lite
95              
96             To specify schema in L<Rinci> function metadata and use the metadata with
97             L<Perinci::CmdLine> (L<Perinci::CmdLine::Lite>) to create a CLI:
98              
99             # in lib/MyApp.pm
100             package
101             MyApp;
102             our %SPEC;
103             $SPEC{myfunc} = {
104             v => 1.1,
105             summary => 'Routine to do blah ...',
106             args => {
107             arg1 => {
108             summary => 'The blah blah argument',
109             schema => ['perl::unqualified_funcname*'],
110             },
111             ...
112             },
113             };
114             sub myfunc {
115             my %args = @_;
116             ...
117             }
118             1;
119              
120             # in myapp.pl
121             package
122             main;
123             use Perinci::CmdLine::Any;
124             Perinci::CmdLine::Any->new(url=>'/MyApp/myfunc')->run;
125              
126             # in command-line
127             % ./myapp.pl --help
128             myapp - Routine to do blah ...
129             ...
130              
131             % ./myapp.pl --version
132              
133             % ./myapp.pl --arg1 ...
134              
135              
136             =head2 Using with Type::Tiny
137              
138             To create a type constraint and type library from a schema:
139              
140             package My::Types {
141             use Type::Library -base;
142             use Type::FromSah qw( sah2type );
143              
144             __PACKAGE__->add_type(
145             sah2type('$sch_name*', name=>'PerlUnqualifiedFuncname')
146             );
147             }
148              
149             use My::Types qw(PerlUnqualifiedFuncname);
150             PerlUnqualifiedFuncname->assert_valid($data);
151              
152             =head1 DESCRIPTION
153              
154             Currently function name is restricted to this regex:
155              
156             \A[A-Za-z_][A-Za-z_0-9]*\z
157              
158             This schema includes syntax validity check only; it does not check whether the
159             function actually exists.
160              
161             This schema includes syntax validity check only; it does not check whether the
162             function actually exists.
163              
164             =head1 HOMEPAGE
165              
166             Please visit the project's homepage at L<https://metacpan.org/release/Sah-Schemas-Perl>.
167              
168             =head1 SOURCE
169              
170             Source repository is at L<https://github.com/perlancar/perl-Sah-Schemas-Perl>.
171              
172             =head1 SEE ALSO
173              
174             L<Sah::Schema::perl::funcname>
175              
176             L<Sah::Schema::perl::qualified_funcname>
177              
178             =head1 AUTHOR
179              
180             perlancar <perlancar@cpan.org>
181              
182             =head1 CONTRIBUTING
183              
184              
185             To contribute, you can send patches by email/via RT, or send pull requests on
186             GitHub.
187              
188             Most of the time, you don't need to build the distribution yourself. You can
189             simply modify the code, then test via:
190              
191             % prove -l
192              
193             If you want to build the distribution (e.g. to try to install it locally on your
194             system), you can install L<Dist::Zilla>,
195             L<Dist::Zilla::PluginBundle::Author::PERLANCAR>,
196             L<Pod::Weaver::PluginBundle::Author::PERLANCAR>, and sometimes one or two other
197             Dist::Zilla- and/or Pod::Weaver plugins. Any additional steps required beyond
198             that are considered a bug and can be reported to me.
199              
200             =head1 COPYRIGHT AND LICENSE
201              
202             This software is copyright (c) 2022, 2021, 2020, 2019, 2018, 2017, 2016 by perlancar <perlancar@cpan.org>.
203              
204             This is free software; you can redistribute it and/or modify it under
205             the same terms as the Perl 5 programming language system itself.
206              
207             =head1 BUGS
208              
209             Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=Sah-Schemas-Perl>
210              
211             When submitting a bug or request, please include a test-file or a
212             patch to an existing test-file that illustrates the bug or desired
213             feature.
214              
215             =cut