This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
(perl #127834) remove . from the end of @INC if complex modules are loaded
[perl5.git] / cpan / ExtUtils-MakeMaker / bin / instmodsh
1 #!/usr/bin/perl -w
2
3 BEGIN { pop @INC if $INC[-1] eq '.' }
4 use strict;
5 use IO::File;
6 use ExtUtils::Packlist;
7 use ExtUtils::Installed;
8
9 use vars qw($Inst @Modules);
10
11
12 =head1 NAME
13
14 instmodsh - A shell to examine installed modules
15
16 =head1 SYNOPSIS
17
18     instmodsh
19
20 =head1 DESCRIPTION
21
22 A little interface to ExtUtils::Installed to examine installed modules,
23 validate your packlists and even create a tarball from an installed module.
24
25 =head1 SEE ALSO
26
27 ExtUtils::Installed
28
29 =cut
30
31
32 my $Module_Help = <<EOF;
33 Available commands are:
34    f [all|prog|doc]   - List installed files of a given type
35    d [all|prog|doc]   - List the directories used by a module
36    v                  - Validate the .packlist - check for missing files
37    t <tarfile>        - Create a tar archive of the module
38    h                  - Display module help
39    q                  - Quit the module
40 EOF
41
42 my %Module_Commands = (
43                        f => \&list_installed,
44                        d => \&list_directories,
45                        v => \&validate_packlist,
46                        t => \&create_archive,
47                        h => \&module_help,
48                       );
49
50 sub do_module($) {
51     my ($module) = @_;
52
53     print($Module_Help);
54     MODULE_CMD: while (1) {
55         print("$module cmd? ");
56
57         my $reply = <STDIN>; chomp($reply);
58         my($cmd) = $reply =~ /^(\w)\b/;
59
60         last if $cmd eq 'q';
61
62         if( $Module_Commands{$cmd} ) {
63             $Module_Commands{$cmd}->($reply, $module);
64         }
65         elsif( $cmd eq 'q' ) {
66             last MODULE_CMD;
67         }
68         else {
69             module_help();
70         }
71     }
72 }
73
74
75 sub list_installed {
76     my($reply, $module) = @_;
77
78     my $class = (split(' ', $reply))[1];
79     $class = 'all' unless $class;
80
81     my @files;
82     if (eval { @files = $Inst->files($module, $class); }) {
83         print("$class files in $module are:\n   ",
84               join("\n   ", @files), "\n");
85     }
86     else {
87         print($@);
88     }
89 };
90
91
92 sub list_directories {
93     my($reply, $module) = @_;
94
95     my $class = (split(' ', $reply))[1];
96     $class = 'all' unless $class;
97
98     my @dirs;
99     if (eval { @dirs = $Inst->directories($module, $class); }) {
100         print("$class directories in $module are:\n   ",
101               join("\n   ", @dirs), "\n");
102     }
103     else {
104         print($@);
105     }
106 }
107
108
109 sub create_archive {
110     my($reply, $module) = @_;
111
112     my $file = (split(' ', $reply))[1];
113
114     if( !(defined $file and length $file) ) {
115         print "No tar file specified\n";
116     }
117     elsif( eval { require Archive::Tar } ) {
118         Archive::Tar->create_archive($file, 0, $Inst->files($module));
119     }
120     else {
121         my($first, @rest) = $Inst->files($module);
122         system('tar', 'cvf', $file, $first);
123         for my $f (@rest) {
124             system('tar', 'rvf', $file, $f);
125         }
126         print "Can't use tar\n" if $?;
127     }
128 }
129
130
131 sub validate_packlist {
132     my($reply, $module) = @_;
133
134     if (my @missing = $Inst->validate($module)) {
135         print("Files missing from $module are:\n   ",
136               join("\n   ", @missing), "\n");
137     }
138     else {
139         print("$module has no missing files\n");
140     }
141 }
142
143 sub module_help {
144     print $Module_Help;
145 }
146
147
148
149 ##############################################################################
150
151 sub toplevel()
152 {
153 my $help = <<EOF;
154 Available commands are:
155    l            - List all installed modules
156    m <module>   - Select a module
157    q            - Quit the program
158 EOF
159 print($help);
160 while (1)
161    {
162    print("cmd? ");
163    my $reply = <STDIN>; chomp($reply);
164    CASE:
165       {
166       $reply eq 'l' and do
167          {
168          print("Installed modules are:\n   ", join("\n   ", @Modules), "\n");
169          last CASE;
170          };
171       $reply =~ /^m\s+/ and do
172          {
173          do_module((split(' ', $reply))[1]);
174          last CASE;
175          };
176       $reply eq 'q' and do
177          {
178          exit(0);
179          };
180       # Default
181          print($help);
182       }
183    }
184 }
185
186
187 ###############################################################################
188
189 $Inst = ExtUtils::Installed->new();
190 @Modules = $Inst->modules();
191 toplevel();
192
193 ###############################################################################