File Coverage

blib/lib/Perinci/CmdLine/Any.pm
Criterion Covered Total %
statement 25 29 86.2
branch 9 12 75.0
condition n/a
subroutine 3 3 100.0
pod 0 1 0.0
total 37 45 82.2


line stmt bran cond sub pod time code
1              
2             use strict;
3 1     1   112584 # IFUNBUILT
  1         9  
  1         298  
4             # use warnings;
5             # END IFUNBUILT
6              
7             our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
8             our $DATE = '2022-10-19'; # DATE
9             our $DIST = 'Perinci-CmdLine-Any'; # DIST
10             our $VERSION = '0.154'; # VERSION
11              
12             my %Opts = (
13             -prefer_lite => 1,
14             );
15              
16             my ($class, %args) = @_;
17             $Opts{$_} = $args{$_} for keys %args;
18 1     1   8 }
19 1         89  
20             my $class = shift;
21              
22             my @mods;
23 3     3 0 274293 my $env = $ENV{PERINCI_CMDLINE_ANY};
24             if ($env) {
25 3         7 if ($env eq 'classic') {
26 3         7 $env = 'Perinci::CmdLine::Classic';
27 3 100       12 } elsif ($env eq 'lite') {
    50          
28 2 100       9 $env = 'Perinci::CmdLine::Lite';
    50          
29 1         5 }
30             @mods = ($env);
31 0         0 } elsif ($Opts{-prefer_lite}) {
32             @mods = qw(Perinci::CmdLine::Lite Perinci::CmdLine::Classic);
33 2         6 } else {
34             @mods = qw(Perinci::CmdLine::Classic Perinci::CmdLine::Lite);
35 1         4 }
36              
37 0         0 for my $i (1..@mods) {
38             my $mod = $mods[$i-1];
39             (my $modpm = "$mod.pm") =~ s!::!/!g;
40 3         11 if ($i == @mods) {
41 3         8 require $modpm;
42 3         25 return $mod->new(@_);
43 3 100       10 } else {
44 2         35 my $res;
45 2         40 eval {
46             require $modpm;
47 1         2 $res = $mod->new(@_);
48 1         2 };
49 1         1289 if ($@) {
50 1         56618 warn "Can't load $mod: $@";
51             next;
52 1 50       21358 } else {
53 0         0 return $res;
54 0         0 }
55             }
56 1         12 }
57             }
58              
59             1;
60             # ABSTRACT: Choose Perinci::CmdLine implementation (::Lite or ::Classic)
61              
62              
63             =pod
64              
65             =encoding UTF-8
66              
67             =head1 NAME
68              
69             Perinci::CmdLine::Any - Choose Perinci::CmdLine implementation (::Lite or ::Classic)
70              
71             =head1 VERSION
72              
73             This document describes version 0.154 of Perinci::CmdLine::Any (from Perl distribution Perinci-CmdLine-Any), released on 2022-10-19.
74              
75             =head1 SYNOPSIS
76              
77             In your command-line script (this will pick ::Lite first):
78              
79             #!perl
80             use Perinci::CmdLine::Any;
81             Perinci::CmdLine::Any->new(url => '/Package/func')->run;
82              
83             In your command-line script (this will pick ::Classic first, and falls back to
84             ::Lite):
85              
86             #!perl
87             use Perinci::CmdLine::Any -prefer_lite=>0;
88             Perinci::CmdLine::Any->new(url => '/Package/func')->run;
89              
90             =head1 DESCRIPTION
91              
92             This module lets you use L<Perinci::CmdLine::Lite> or
93             L<Perinci::CmdLine::Classic>.
94              
95             If you want to force using a specific class, you can set the
96             C<PERINCI_CMDLINE_ANY> environment variable, e.g. the command below will only
97             try to use Perinci::CmdLine::Classic:
98              
99             % PERINCI_CMDLINE_ANY=Perinci::CmdLine::Classic yourapp.pl
100             % PERINCI_CMDLINE_ANY=classic yourapp.pl
101              
102             If you want to prefer to Perinci::CmdLine::Classic (but user will still be able
103             to override using C<PERINCI_CMDLINE_ANY>):
104              
105             use Perinci::CmdLine::Any -prefer_lite => 0;
106              
107             =for Pod::Coverage ^(new)$
108              
109             =head1 ENVIRONMENT
110              
111             =head2 PERINCI_CMDLINE_ANY => str
112              
113             Either specify module name, or C<lite> or C<classic>.
114              
115             =head1 HOMEPAGE
116              
117             Please visit the project's homepage at L<https://metacpan.org/release/Perinci-CmdLine-Any>.
118              
119             =head1 SOURCE
120              
121             Source repository is at L<https://github.com/perlancar/perl-Perinci-CmdLine-Any>.
122              
123             =head1 SEE ALSO
124              
125             L<Perinci::CmdLine::Lite>, L<Perinci::CmdLine::Classic>
126              
127             Another alternative backend, but not available through Perinci::CmdLine::Any
128             since it works by generating script instead: L<Perinci::CmdLine::Inline>
129              
130             =head1 AUTHOR
131              
132             perlancar <perlancar@cpan.org>
133              
134             =head1 CONTRIBUTOR
135              
136             =for stopwords Steven Haryanto
137              
138             Steven Haryanto <stevenharyanto@gmail.com>
139              
140             =head1 CONTRIBUTING
141              
142              
143             To contribute, you can send patches by email/via RT, or send pull requests on
144             GitHub.
145              
146             Most of the time, you don't need to build the distribution yourself. You can
147             simply modify the code, then test via:
148              
149             % prove -l
150              
151             If you want to build the distribution (e.g. to try to install it locally on your
152             system), you can install L<Dist::Zilla>,
153             L<Dist::Zilla::PluginBundle::Author::PERLANCAR>,
154             L<Pod::Weaver::PluginBundle::Author::PERLANCAR>, and sometimes one or two other
155             Dist::Zilla- and/or Pod::Weaver plugins. Any additional steps required beyond
156             that are considered a bug and can be reported to me.
157              
158             =head1 COPYRIGHT AND LICENSE
159              
160             This software is copyright (c) 2022, 2021, 2019, 2017, 2016, 2015, 2014 by perlancar <perlancar@cpan.org>.
161              
162             This is free software; you can redistribute it and/or modify it under
163             the same terms as the Perl 5 programming language system itself.
164              
165             =head1 BUGS
166              
167             Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=Perinci-CmdLine-Any>
168              
169             When submitting a bug or request, please include a test-file or a
170             patch to an existing test-file that illustrates the bug or desired
171             feature.
172              
173             =cut