File Coverage

lib/App/Pmhack.pm
Criterion Covered Total %
statement 53 57 92.9
branch 9 18 50.0
condition 1 3 33.3
subroutine 14 16 87.5
pod 1 1 100.0
total 78 95 82.1


line stmt bran cond sub pod time code
1             package App::Pmhack;
2             BEGIN {
3 2     2   109563 $App::Pmhack::VERSION = '0.002';
4             }
5              
6             # ABSTRACT: Hack on installed Perl modules
7              
8 2     2   27 use strict;
  2         4  
  2         58  
9 2     2   11 use warnings;
  2         3  
  2         48  
10              
11 2     2   1807 use Perl6::Export::Attrs;
  2         26122  
  2         16  
12 2     2   2021 use English qw($OSNAME);
  2         6515  
  2         9  
13 2     2   589 use Carp qw(carp croak);
  2         3  
  2         108  
14 2     2   1593 use File::Copy qw(copy);
  2         8664  
  2         150  
15 2     2   2982 use Module::Load qw();
  2         2416  
  2         44  
16 2     2   1609 use Module::Locate qw();
  2         22470  
  2         51  
17 2     2   58 use File::Path qw();
  2         5  
  2         48  
18 2     2   1886 use Params::Util qw();
  2         5662  
  2         54  
19 2     2   1361 use Try::Tiny qw(try catch);
  2         1789  
  2         877  
20              
21             sub pmhack :Export
22             {
23 1 50   1 1 874 my $module_name = Params::Util::_CLASS(shift) or croak "Please supply a valid module name";
24 1 50       20 my $hacklib = $ENV{PERL5HACKLIB} or croak "PERL5HACKLIB environment variable not set, aborting";
25            
26             # SAMPLE VALUES
27             # =============
28             # $module_name: Some::Interesting::Module
29             # $inc_filename: /usr/lib/perl5/Some/Interesting/Module.pm
30             # $hacklib: /usr/supermario/pmhacklib
31             # $target_filename: /usr/supermario/pmhacklib/Some/Interesting/Module.pm
32             # $target_path: /usr/supermario/pmhacklib/Some/Interesting
33              
34 1         9 my $target_filename = File::Spec->catfile($hacklib, Module::Locate::mod_to_path($module_name));
35 1         38 my ($target_volume, $target_dir, $target_basename)
36             = File::Spec->splitpath( $target_filename );
37 1         6 my $target_path = File::Spec->catdir($target_volume, $target_dir);
38              
39             # skip PERL5HACKLIB when searching for modules
40 1         5 my @inc_filenames = Module::Locate::locate($module_name);
41 1         340 @inc_filenames = grep { $_ ne $target_filename } @inc_filenames;
  1         4  
42 1 50       5 @inc_filenames ? my $inc_filename = shift @inc_filenames : croak "Cannot find source for $module_name";
43            
44             # create all necessary directories
45 1 50 33     24 unless ( -e $target_path && -d $target_path )
46             {
47 1 50       299 File::Path::make_path($target_path) or croak "Could not create path: $!";
48             }
49              
50             # copy, overwriting if necessary
51 1 50       80 open (my $target_fh, '>', $target_filename) or croak "Could not open target $target_filename for writing: $!";
52 1 50       7 copy ($inc_filename, $target_fh) or croak "Copy failed: $!";
53 1 50       233 close ($target_fh) or carp "Could not close target filehandle";
54              
55             # on Win32, unset the READONLY attribute
56 1 50       6 if ($OSNAME eq 'MSWin32')
57             {
58             try
59             {
60 0     0   0 Module::Load::load('Win32::File');
61 0         0 Win32::File::SetAttributes($target_filename, Win32::File::NORMAL());
62             }
63             catch
64             {
65 0     0   0 carp "Failed removing read-only attributes, make sure you have Win32::File installed";
66 0         0 };
67            
68             }
69            
70 1         4 return $target_filename;
71 2     2   13 }
  2         4  
  2         17  
72              
73             1;
74              
75             =head1 NAME
76              
77             App::Pmhack
78              
79             =head1 ABSTRACT
80              
81             Hack on installed Perl modules
82              
83             =head1 SYNOPSIS
84              
85             use App::Pmhack qw(pmhack);
86             my $new_location = pmhack('Some::Module::Name');
87              
88             =head1 DESCRIPTION
89              
90             This module is used internally by teh C utility.
91              
92             =head1 FUNCTIONS
93              
94             =head2 pmhack
95              
96             Given a perl module name, finds the module in @INC, copies it into a directory specified in C<$ENV{PERL5HACKLIB}> and returns the resulting filename.
97              
98             =head1 AUTHOR
99              
100             Peter Shangov
101              
102             =head1 COPYRIGHT AND LICENSE
103              
104             This software is copyright (c) 2010 by Peter Shangov.
105              
106             This is free software; you can redistribute it and/or modify it under the
107             same terms as the Perl 5 programming language system itself.
108