| 1 | package App::Pmhack; | ||||||||||||||||||||||||||||
| 2 | 2 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 2 | $App::Pmhack::VERSION = '0.002'; | |||||||||||||||||||||||||||
| 4 | } | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | # ABSTRACT: Hack on installed Perl modules | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use Perl6::Export::Attrs; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use English qw($OSNAME); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use Carp qw(carp croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 2 | ▶ | use File::Copy qw(copy); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 2 | ▶ | use Module::Load qw(); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 2 | ▶ | use Module::Locate qw(); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 2 | ▶ | use File::Path qw(); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 2 | ▶ | use Params::Util qw(); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 2 | ▶ | use Try::Tiny qw(try catch); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 1 | ▶ | sub pmhack :Export | ||||||||||||||||||||||||||
|
Subroutine
pmhack: called CC=11 SCAR=28.3
Pod
documented
| |||||||||||||||||||||||||||||
| 22 | { | ||||||||||||||||||||||||||||
| 23 | 1 | ▶ | my $module_name = Params::Util::_CLASS(shift) or croak "Please supply a valid module name"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | 1 | ▶ | my $hacklib = $ENV{PERL5HACKLIB} or croak "PERL5HACKLIB environment variable not set, aborting"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | my $target_filename = File::Spec->catfile($hacklib, Module::Locate::mod_to_path($module_name)); | |||||||||||||||||||||||||||
| 35 | 1 | my ($target_volume, $target_dir, $target_basename) | |||||||||||||||||||||||||||
| 36 | = File::Spec->splitpath( $target_filename ); | ||||||||||||||||||||||||||||
| 37 | 1 | my $target_path = File::Spec->catdir($target_volume, $target_dir); | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | # skip PERL5HACKLIB when searching for modules | ||||||||||||||||||||||||||||
| 40 | 1 | my @inc_filenames = Module::Locate::locate($module_name); | |||||||||||||||||||||||||||
| 41 | 1 | @inc_filenames = grep { $_ ne $target_filename } @inc_filenames; | |||||||||||||||||||||||||||
| 42 | 1 | ▶ | @inc_filenames ? my $inc_filename = shift @inc_filenames : croak "Cannot find source for $module_name"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | # create all necessary directories | ||||||||||||||||||||||||||||
| 45 | 1 | ▶ | unless ( -e $target_path && -d $target_path ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
-e $target_path and -d $target_path
MC/DC0%0/2
-e $target_path and -d $target_path
-e $target_path
-d $target_path
Truth table33%1/3
A and B
A= -e $target_pathB= -d $target_path
| |||||||||||||||||||||||||||||
| 46 | { | ||||||||||||||||||||||||||||
| 47 | 1 | ▶ | File::Path::make_path($target_path) or croak "Could not create path: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | # copy, overwriting if necessary | ||||||||||||||||||||||||||||
| 51 | 1 | ▶ | open (my $target_fh, '>', $target_filename) or croak "Could not open target $target_filename for writing: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 1 | ▶ | copy ($inc_filename, $target_fh) or croak "Copy failed: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 1 | ▶ | close ($target_fh) or carp "Could not close target filehandle"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | # on Win32, unset the READONLY attribute | ||||||||||||||||||||||||||||
| 56 | 1 | ▶ | if ($OSNAME eq 'MSWin32') | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | { | ||||||||||||||||||||||||||||
| 58 | try | ||||||||||||||||||||||||||||
| 59 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 60 | 0 | Module::Load::load('Win32::File'); | |||||||||||||||||||||||||||
| 61 | 0 | Win32::File::SetAttributes($target_filename, Win32::File::NORMAL()); | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | catch | ||||||||||||||||||||||||||||
| 64 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 65 | 0 | carp "Failed removing read-only attributes, make sure you have Win32::File installed"; | |||||||||||||||||||||||||||
| 66 | 0 | }; | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 1 | return $target_filename; | |||||||||||||||||||||||||||
| 71 | 2 | ▶ | } | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 2 | 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<pmhack> 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 <pshangov at yahoo dot com> | ||||||||||||||||||||||||||||
| 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 | |||||||||||||||||||||||||||||