File Coverage

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