summaryrefslogtreecommitdiffstats
path: root/Aufgabe7/pmdesc3.pl
blob: 63e8105dab50a3c7928825d8fcbcebfece9cf9cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#!/usr/bin/perl -w
#===================================================================================
#
#         FILE:  pmdesc3
#
#     SYNOPSIS:  Find versions and descriptions of installed perl Modules and PODs
#
#  DESCRIPTION:  See POD below.
#
#      CREATED:  15.06.2004 22:12:41 CEST
#     REVISION:  23.10.2004 
#                - Module versions are checked with a regex; avoids unprintable
#                  characters (original version) and doubling of entries.
#                - new option -v
#                28.10.2004 
#                - Function get_module_description completely new written.
#                29.01.2005 
#                - Look for a description in the DESCRIPTION section if no
#                  NAME section is found.
#                16.04.2005
#                - Adaption for MS Windows.
#         TODO:  Replace UNIX sort pipe.
#                 
#===================================================================================

package pmdesc3;

$VERSION=1.2.2;                   # update POD at the end of this file

require 5.6.1;

use strict;
use Carp;
use ExtUtils::MakeMaker;
use File::Find           qw(find);
use Getopt::Std          qw(getopts);

my  $MaxDescLength= 150;          # Maximum length for the description field:
                                  # prevents slurping in big amount of faulty docs.

my  $rgx_version  = q/\A\d+(\.\w+)*\Z/; # regex for module versions 

#===  FUNCTION  ====================================================================
#         NAME:  usage
#===================================================================================
sub usage {
  my  $searchdirs = " "x12;
  $searchdirs .= join( "\n"." "x12, sort { length $b <=> length $a } @INC ) . "\n";
  print <<EOT;
Usage:   pmdesc3 [-h] [-s] [-t ddd] [-v dd] [--] [dir [dir [dir [...]]]]
Options:  -h         print this message
          -s         sort output (not under Windows)
          -t ddd     name column has width ddd (1-3 digits); default 36
          -v  dd     version column has width ddd (1-3 digits); default 10
          If no directories given, searches:
$searchdirs
EOT
  exit;
}

#===  FUNCTION  ====================================================================
#         NAME:  get_module_name
#===================================================================================
sub get_module_name {
  my ($path, $relative_to) = @_;

  local $_ = $path;
  s!\A\Q$relative_to\E/?!!;
  s! \.p(?:m|od) \z!!x;
  s!/!::!g;

  return $_;
}

#===  FUNCTION  ====================================================================
#         NAME:  get_module_description
#===================================================================================
sub get_module_description 
{
  my  $desc;
  my  ($INFILE_file_name) = @_;               # input file name

  undef $/;                                   # undefine input record separator

  open ( INFILE, '<', $INFILE_file_name )
    or die "$0 : failed to open input file $INFILE_file_name : $!\n";

  my  $file = <INFILE>;                       # slurp mode
  close ( INFILE );                           # close input file

  $file =~  s/\cM\cJ/\cJ/g;                   # remove DOS line ends 
  $file =~  m/\A=head1\s+NAME(.*?)\n=\w+/s;   # file starts with '=head1' (PODs)
  $desc = $1;

  if ( ! defined $desc  )
  {
    $file =~  m/\n=head1\s+NAME(.*?)\n=\w+/s; # '=head1' is embedded
    $desc = $1;
  }

  if ( ! defined $desc  )
  {
    $file =~  m/\n=head1\s+DESCRIPTION(.*?)\n=\w+/s; # '=head1' is embedded
    $desc = $1;
  }

  if ( defined $desc )
  {
    $desc =~ s/\A[ \t\n]*//s;                 # remove leading whitespaces
    $desc =~ s/\n\s+\n/\n\n/sg;               # make true empty lines
    $desc =~ s/\n\n.*$//s;                    # discard all trailing paragraphs
    $desc =~ s/\A.*?\s+-+\s+//s;              # discard leading module name
    $desc =~ s/\n/ /sg;                       # join lines
    $desc =~ s/\s+/ /g;                       # squeeze whitespaces
    $desc =~ s/\s*$//g;                       # remove trailing whitespaces
    $desc =  substr $desc, 0, $MaxDescLength; # limited length
  }
  return $desc;
}

#===  FUNCTION  ====================================================================
#         NAME:  get_module_version
#===================================================================================
sub get_module_version {
  local $_;                                   # MM->parse_version is naughty
  my $vers_code = MM->parse_version($File::Find::name) || '';
  $vers_code = undef unless $vers_code =~ /$rgx_version/;
  return $vers_code;
}

#===  FUNCTION  ====================================================================
#         NAME:  MAIN
#===================================================================================

my %visited;

$|++;

#---------------------------------------------------------------------------
#  process options and command line arguments
#---------------------------------------------------------------------------
my  %options;

getopts("hst:v:", \%options)         or $options{h}=1;

my  @args = @ARGV;
@ARGV = @INC unless @ARGV;

usage() if $options{h};                               #  option -h  :  usage

#---------------------------------------------------------------------------
#  option -t : width of the module name column
#---------------------------------------------------------------------------
usage() if $options{t} && $options{t}!~/^\d{1,3}$/;   # width 1-3 digits

$options{t} = "36" unless $options{t};

#---------------------------------------------------------------------------
#  option -v : width of the version column
#---------------------------------------------------------------------------
usage() if $options{v} && $options{v}!~/^\d{1,2}$/;   # width 1-2 digits

$options{v} = "10" unless $options{v};

#---------------------------------------------------------------------------
#  option -s  :  install an output filter to sort the module list
#---------------------------------------------------------------------------
if ($options{s}) {
	usage() if $^O eq "MSWin32";
  if (open(ME, "-|")) {
    $/ = "";
    while (<ME>) {
      chomp;
      print join("\n", sort split /\n/), "\n";
    }
    exit;
  }
}

#---------------------------------------------------------------------------
#  process 
#---------------------------------------------------------------------------
# 
# :WARNING:15.04.2005:Mn: under Windows descending into subdirs will be
# suppressed by the the preprocessing part of the following call to find
# :TODO:16.04.2005:Mn: remove code doubling
# 
if ( $^O ne "MSWin32" ) {                       # ----- UNIX, Linux, ...

	for my $inc_dir (sort { length $b <=> length $a } @ARGV) {
		find({
				wanted => sub {
					return unless /\.p(?:m|od)\z/ && -f;

					#---------------------------------------------------------------------
					#  return from function if there exists a pod-file for this module
					#---------------------------------------------------------------------
					my $pod = $_;
					my $pm  = $_;
					if ( m/\.pm\z/ )
					{
						$pod  =~ s/\.pm\z/\.pod/; 
						return if -f $pod;
					}

					my $module  = get_module_name($File::Find::name, $inc_dir);
					my $version;
					if ( /\.pod\z/ )
					{
						$pm =~ s/\.pod\z/\.pm/; 
						#-------------------------------------------------------------------
						#  try to find the version from the pm-file
						#-------------------------------------------------------------------
						if ( -f $pm )
						{
							local $_;
							$version = MM->parse_version($pm) || "";
							$version = undef unless $version =~ /$rgx_version/;
						}
					}
					else
					{
						$version = get_module_version($_);
					}
					my $desc = get_module_description($_);

					$version = defined $version ? " ($version)" : " (n/a)";
					$desc    = defined $desc    ? " $desc"      : " <description not available>";

					printf("%-${options{t}}s%-${options{v}}s%-s\n", $module, $version, $desc ); 

				},

				preprocess => sub {
					my ($dev, $inode) = stat $File::Find::dir or return;
					$visited{"$dev:$inode"}++ ? () : @_;
				},
			},
			$inc_dir);
	}
}
else {                                          # ----- MS Windows
	for my $inc_dir (sort { length $b <=> length $a } @ARGV) {
		find({
				wanted => sub {
					return unless /\.p(?:m|od)\z/ && -f;

					#---------------------------------------------------------------------
					#  return from function if there exists a pod-file for this module
					#---------------------------------------------------------------------
					my $pod = $_;
					my $pm  = $_;
					if ( m/\.pm\z/ )
					{
						$pod  =~ s/\.pm\z/\.pod/; 
						return if -f $pod;
					}

					my $module  = get_module_name($File::Find::name, $inc_dir);
					my $version;
					if ( /\.pod\z/ )
					{
						$pm =~ s/\.pod\z/\.pm/; 
						#-------------------------------------------------------------------
						#  try to find the version from the pm-file
						#-------------------------------------------------------------------
						if ( -f $pm )
						{
							local $_;
							$version = MM->parse_version($pm) || "";
							$version = undef unless $version =~ /$rgx_version/;
						}
					}
					else
					{
						$version = get_module_version($_);
					}
					my $desc = get_module_description($_);

					$version = defined $version ? " ($version)" : " (n/a)";
					$desc    = defined $desc    ? " $desc"      : " <description not available>";

					printf("%-${options{t}}s%-${options{v}}s%-s\n", $module, $version, $desc ); 

				},
			},
			$inc_dir);
	}
}

#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
#  Modul Documentation
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

=head1 NAME
  
pmdesc3 - Find versions and descriptions of installed perl modules and PODs

=head1 SYNOPSIS

pmdesc3

pmdesc3 ~/perllib

=head1 DESCRIPTION

  pmdesc3 [-h] [-s] [-t ddd] [-v dd] [--] [dir [dir [dir [...]]]]

  OPTIONS:  -h     : print help message; show search path
            -s     : sort output (not under Windows)
            -t ddd : name column has width ddd (1-3 digits); default 36
            -v  dd : version column has width dd (1-2 digits); default 10

Find versions and descriptions of installed Perl Modules and PODs.
If no directories given, searches @INC .
The first column of the output (see below) can be used as module name or
FAQ-name for perldoc.

Some modules are split into a pm-file and an accompanying pod-file.
The version number is always taken from the pm-file.

The description found will be cut down to a length of at most
150 characters (prevents slurping in big amount of faulty docs).

=over 2

=item Output

The output looks like that:

   ...
IO::Socket         (1.28)  Object interface to socket communications
IO::Socket::INET   (1.27)  Object interface for AF_INET domain sockets
IO::Socket::UNIX   (1.21)  Object interface for AF_UNIX domain sockets
IO::Stty           (n/a)   <description not available>
IO::Tty            (1.02)  Low-level allocate a pseudo-Tty, import constants.
IO::Tty::Constant  (n/a)   Terminal Constants (autogenerated)
   ...

The three parts module name, version and description are separated
by at least one blank.
  
=back

=head1 REQUIREMENTS

ExtUtils::MakeMaker, File::Find, Getopt::Std


=head1 AUTHORS

  Tom Christiansen, tchrist@perl.com (pmdesc)
  Aristotle, http://qs321.pair.com/~monkads/ (pmdesc2)
  Fritz Mehner, mehner@fh-swf.de (pmdesc3)

=head1 NOTES

pmdesc3 is based on pmdesc2 (Aristotle, http://qs321.pair.com/~monkads/).
pmdesc3 adds extensions and bugfixes.

pmdesc2 is based on pmdesc (Perl Cookbook, 1. Ed., recipe 12.19).
pmdesc2 is at least one magnitude faster than pmdesc.

=head1 VERSION

1.2.2

=cut