File Coverage

blib/lib/vendorlib.pm
Criterion Covered Total %
statement 26 27 96.3
branch 8 12 66.6
condition 2 3 66.6
subroutine 4 4 100.0
pod n/a
total 40 46 86.9


line stmt bran cond sub pod time code
1             package vendorlib;
2              
3 1     1   25639 use strict;
  1         3  
  1         35  
4 1     1   5 use warnings;
  1         2  
  1         33  
5 1     1   5 use Config;
  1         6  
  1         420  
6              
7             =head1 NAME
8              
9             vendorlib - Use Only Core and Vendor Libraries in @INC
10              
11             =cut
12              
13             our $VERSION = '0.11';
14              
15             =head1 SYNOPSIS
16              
17             #!/usr/bin/perl
18              
19             use vendorlib;
20             use strict;
21             use warnings;
22             use SomeModule; # will only search in core and vendor paths
23             ...
24              
25             =head1 DESCRIPTION
26              
27             In a system distribution such as Debian, it may be advisable for Perl programs
28             to ignore the user's CPAN-installed modules and only use the
29             distribution-provided modules to avoid possible breakage with newer and
30             unpackaged versions of modules.
31              
32             To that end, this pragma will replace your C<@INC> with only the core and vendor
33             C<@INC> paths, ignoring site_perl and C<$ENV{PERL5LIB}> entirely.
34              
35             It is recommended that you put C<use vendorlib;> as the first statement in your
36             program, before even C<use strict;> and C<use warnings;>.
37              
38             =cut
39              
40             sub import {
41 3 50   3   277665 my @paths = (($^O ne 'MSWin32' ? ('/etc/perl') : ()), @Config{qw/
42             vendorarch
43             vendorlib
44             archlib
45             privlib
46             /});
47              
48             # This grep MUST BE on copies of the paths to not trigger Config overload
49             # magic.
50 3         4614 @paths = grep $_, @paths;
51              
52             # remove duplicates
53 3         7 my @result;
54 3         15 while (my $path = shift @paths) {
55 11 50 66     51 if (@paths && $path eq $paths[0]) {
56             # ignore
57             }
58             else {
59 11         32 push @result, $path;
60             }
61             }
62 3         7 @paths = @result;
63              
64             # fixup slashes for @INC on Win32
65 3 50       13 if ($^O eq 'MSWin32') {
66 0         0 s{\\}{/}g for @paths;
67             }
68              
69             # expand tildes
70 3 50       10 if ($^O ne 'MSWin32') {
71 3         7 for my $path (@paths) {
72 11 100       61 if ($path =~ m{^~/+}) {
    100          
73 1         1348 my $home = (getpwuid($<))[7];
74 1         17 $path =~ s|^~/+|${home}/|;
75             }
76             elsif (my ($user) = $path =~ /^~(\w+)/) {
77 1         211 my $home = (getpwnam($user))[7];
78 1         41 $path =~ s|^~${user}/+|${home}/|;
79             }
80             }
81             }
82              
83             # remove any directories that don't actually exist
84             # this will also remove /etc/perl on non-Debian systems
85 3         362 @paths = grep -d, @paths;
86              
87 3         44 @INC = @paths;
88             }
89              
90             =head1 BUGS
91              
92             Please report any bugs or feature requests to C<bug-vendorlib at rt.cpan.org>,
93             or through the web interface at
94             L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=vendorlib>. I will be
95             notified, and then you'll automatically be notified of progress on your bug as I
96             make changes.
97              
98              
99             =head1 SUPPORT
100              
101             You can find documentation for this module with the perldoc command.
102              
103             perldoc vendorlib
104              
105             You can also look for information at:
106              
107             =over 4
108              
109             =item * RT: CPAN's request tracker
110              
111             L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=vendorlib>
112              
113             =item * AnnoCPAN: Annotated CPAN documentation
114              
115             L<http://annocpan.org/dist/vendorlib>
116              
117             =item * CPAN Ratings
118              
119             L<http://cpanratings.perl.org/d/vendorlib>
120              
121             =item * Search CPAN
122              
123             L<http://search.cpan.org/dist/vendorlib/>
124              
125             =back
126              
127             =head1 ACKNOWLEDGEMENTS
128              
129             mxey and jawnsy on oftc #debian-perl helped to hash out the design for this.
130              
131             ribasushi reviewed the initial version and pointed out that @INC order matters.
132              
133             =head1 AUTHOR
134              
135             Rafael Kitover, C<< <rkitover at cpan.org> >>
136              
137             =head1 LICENSE AND COPYRIGHT
138              
139             Copyright 2011 Rafael Kitover.
140              
141             This program is free software; you can redistribute it and/or modify it
142             under the terms of either: the GNU General Public License as published
143             by the Free Software Foundation; or the Artistic License.
144              
145             See http://dev.perl.org/licenses/ for more information.
146              
147             =cut
148              
149             1; # End of vendorlib