File Coverage

blib/lib/lazy.pm
Criterion Covered Total %
statement 36 43 83.7
branch 4 8 50.0
condition 9 18 50.0
subroutine 8 9 88.8
pod n/a
total 57 78 73.0


line stmt bran cond sub pod time code
1             package lazy;
2              
3 12     12   491514 use strict;
  12         108  
  12         438  
4 12     12   96 use warnings;
  12         30  
  12         726  
5              
6             our $VERSION = '0.000005';
7              
8 12     12   6594 use App::cpm 0.974; # CLI has no $VERSION
  12         1896  
  12         432  
9 12     12   7116 use App::cpm::CLI;
  12         3577758  
  12         612  
10              
11             # Cargo-culted from App::cpm::CLI
12             # Adding pass_through so that we don't have to keep up with all possible options
13 12         102 use Getopt::Long qw(
14             :config
15             no_auto_abbrev
16             no_ignore_case
17             bundling
18             pass_through
19 12     12   126 );
  12         30  
20              
21 12     12   9576 use Module::Loaded qw( is_loaded );
  12         8694  
  12         5340  
22              
23             sub import {
24 12     12   132 shift;
25 12         48 my @args = @_;
26              
27 12         24 my $is_global;
28             my $local_lib;
29              
30             {
31 12         30 local @ARGV = @args;
  12         30  
32              
33             # Stolen from App::cpm::CLI::parse_options()
34 12         72 GetOptions(
35             'L|local-lib-contained=s' => \$local_lib,
36             'g|global' => \$is_global,
37             );
38             }
39              
40             # Generally assume a global install, which makes the invocation as
41             # simple as:
42              
43             # perl -Mlazy foo.pl
44              
45             # However, if we're already using local::lib and --global has not been
46             # explicitly set and no local::lib has been explicitly set, let's try
47             # to DTRT and use the correct local::lib.
48              
49             # This allows us to do something like:
50             # perl -Mlocal::lib -Mlazy foo.pl
51              
52             # This may or may not be a good idea.
53              
54             # Allowing --local-lib-contained to be passed is mostly useful for
55             # testing. For real world cases, the user should specify the
56             # local::lib via local::lib itself.
57              
58             # perl -Mlocal::lib=my_local_lib -Mlazy foo.pl
59              
60 12 50 66     4662 if ( ( !$is_global && !$local_lib ) && is_loaded('local::lib') ) {
    100 66        
      66        
61 0         0 my @paths = local::lib->new->active_paths;
62 0         0 my $path = shift @paths;
63 0 0       0 if ($path) {
64 0         0 push @args, ( '-L', $path );
65 0         0 _print_msg_about_local_lib($path);
66             }
67             }
68              
69             # Assume a global install if local::lib is not in use or has not been
70             # explicitly invoked.
71              
72             elsif ( !$is_global && !$local_lib ) {
73 6         198 push @args, ('-g');
74             }
75              
76 12         84 my $cpm = App::cpm::CLI->new;
77              
78             # Push the hook onto @INC and then re-add all of @INC again. This way, if
79             # we got to the hook and tried to install, we can re-try @INC to see if the
80             # module can now be used.
81              
82             push @INC, sub {
83 12     12   19860 shift;
84              
85             # Don't try to install if we're called inside an eval
86 12         126 my @caller = caller(1);
87             return
88 12 50 33     210 if ( ( $caller[3] && $caller[3] =~ m{eval} )
      33        
      33        
89             || ( $caller[1] && $caller[1] =~ m{eval} ) );
90              
91 12         36 my $name = shift;
92 12         42 $name =~ s{/}{::}g;
93 12         30 $name =~ s{\.pm\z}{};
94              
95 12         84 $cpm->run( 'install', @args, $name );
96 2         4295113 return 1;
97 12         948 }, @INC;
98             }
99              
100             sub _print_msg_about_local_lib {
101 0     0     my $path = shift;
102              
103 0           print <<"EOF";
104              
105             ********
106              
107             You haven't included any arguments for App::cpm via lazy, but you've
108             loaded local::lib, so we're going to install all modules into:
109              
110             $path
111              
112             If you do not want to do this, you can explicitly invoke a global install via:
113              
114             perl -Mlazy=-g path/to/script.pl
115              
116             or, from inside your code:
117              
118             use lazy qw( -g );
119              
120             If you would like to install to a different local lib:
121              
122             perl -Mlocal::lib=my_local_lib -Mlazy path/to/script.pl
123              
124             or, from inside your code:
125              
126             use local::lib qw( my_local_lib );
127             use lazy;
128              
129             ********
130              
131             EOF
132             }
133              
134             1;
135              
136             # ABSTRACT: Lazily install missing Perl modules
137              
138             __END__