📄 ch12_20.htm
字号:
<HTML><HEAD><TITLE>Recipe 12.19. Program: Finding Versions and Descriptions of Installed Modules (Perl Cookbook)</TITLE><METANAME="DC.title"CONTENT="Perl Cookbook"><METANAME="DC.creator"CONTENT="Tom Christiansen & Nathan Torkington"><METANAME="DC.publisher"CONTENT="O'Reilly & Associates, Inc."><METANAME="DC.date"CONTENT="1999-07-02T01:42:06Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-243-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch12_01.htm"TITLE="12. Packages, Libraries, and Modules"><LINKREL="prev"HREF="ch12_19.htm"TITLE="12.18. Example: Module Template"><LINKREL="next"HREF="ch13_01.htm"TITLE="13. Classes, Objects, and Ties"></HEAD><BODYBGCOLOR="#FFFFFF"><img alt="Book Home" border="0" src="gifs/smbanner.gif" usemap="#banner-map" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Perl Cookbook"><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><p><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch12_19.htm"TITLE="12.18. Example: Module Template"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 12.18. Example: Module Template"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"><ACLASS="chapter"REL="up"HREF="ch12_01.htm"TITLE="12. Packages, Libraries, and Modules"></A></FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="chapter"HREF="ch13_01.htm"TITLE="13. Classes, Objects, and Ties"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 13. Classes, Objects, and Ties"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch12-chap12_program_1">12.19. Program: Finding Versions and Descriptions of Installed Modules</A></H2><PCLASS="para"><ACLASS="indexterm"NAME="ch12-idx-1000005323-0"></A><ACLASS="indexterm"NAME="ch12-idx-1000005323-1"></A>Perl is shipped with many modules. Even more can be found on CPAN. The following program prints out the names, versions, and descriptions of all modules installed on your system. It uses standard modules like File::Find and includes several techniques described in this chapter.</P><PCLASS="para">To run it, type:</P><PRECLASS="programlisting">% pmdesc</PRE><PCLASS="para">It prints a list of modules and their descriptions:</P><PRECLASS="programlisting"><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>FileHandle (2.00) - supply object methods for filehandles</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>IO::File (1.06021) - supply object methods for filehandles</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>IO::Select (1.10) - OO interface to the select system call</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>IO::Socket (1.1603) - Object interface to socket communications</I></CODE></B></CODE><BCLASS="emphasis.bold">...</B></PRE><PCLASS="para">With the <BCLASS="emphasis.bold">-v</B> flag, <EMCLASS="emphasis">pmdesc</EM> provides the names of the directories the files are in:</P><PRECLASS="programlisting">% pmdesc -v<CODECLASS="userinput"><B><CODECLASS="replaceable"><I><<<Modules from /usr/lib/perl5/i686-linux/5.00404>>></I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>FileHandle (2.00) - supply object methods for filehandles</I></CODE></B></CODE><BCLASS="emphasis.bold"> ...</B></PRE><PCLASS="para">The <BCLASS="emphasis.bold">-w</B> flag warns if a module doesn't come with a pod description, and <BCLASS="emphasis.bold">-s</B> sorts the module list within each directory.</P><PCLASS="para">The program is given in <ACLASS="xref"HREF="ch12_20.htm#ch12-39484"TITLE="pmdesc">Example 12.3</A>.</P><DIVCLASS="example"><H4CLASS="example"><ACLASS="title"NAME="ch12-39484">Example 12.3: pmdesc</A></H4><PRECLASS="programlisting">#!/usr/bin/perl -w# <ACLASS="indexterm"NAME="ch12-idx-1000005374-0"></A>pmdesc - describe pm files# tchrist@perl.comuse strict;use File::Find qw(find);use Getopt::Std qw(getopts);use Carp;use vars ( q!$opt_v!, # give debug info q!$opt_w!, # warn about missing descs on modules q!$opt_a!, # include relative paths q!$opt_s!, # sort output within each directory);$| = 1;getopts('wvas') or die "bad usage";@ARGV = @INC unless @ARGV;# Globals. wish I didn't really have to do this.use vars ( q!$Start_Dir!, # The top directory find was called with q!%Future!, # topdirs find will handle later);my $Module;# install an output filter to sort my module list, if wanted.if ($opt_s) { if (open(ME, "-|")) { $/ = ''; while (<ME>) { chomp; print join("\n", sort split /\n/), "\n"; } exit; }}MAIN: { my %visited; my ($dev,$ino); @Future{@ARGV} = (1) x @ARGV; foreach $Start_Dir (@ARGV) { delete $Future{$Start_Dir}; print "\n<<Modules from $Start_Dir>>\n\n" if $opt_v; next unless ($dev,$ino) = stat($Start_Dir); next if $visited{$dev,$ino}++; next unless $opt_a || $Start_Dir =~ m!^/!; find(\&wanted, $Start_Dir); } exit;}# calculate module name from file and directorysub modname { local $_ = $File::Find::name; if (index($_, $Start_Dir . '/') == 0) { substr($_, 0, 1+length($Start_Dir)) = ''; } s { / } {::}gx; s { \.p(m|od)$ } {}x; return $_;}# decide if this is a module we wantsub wanted { if ( $Future{$File::Find::name} ) { warn "\t(Skipping $File::Find::name, qui venit in futuro.)\n" if 0 and $opt_v; $File::Find::prune = 1; return; } return unless /\.pm$/ && -f; $Module = &modname; # skip obnoxious modules if ($Module =~ /^CPAN(\Z|::)/) { warn("$Module -- skipping because it misbehaves\n"); return; } my $file = $_; unless (open(POD, "< $file")) { warn "\tcannot open $file: $!"; # if $opt_w; return 0; } $: = " -:"; local $/ = ''; local $_; while (<POD>) { if (/=head\d\s+NAME/) { chomp($_ = <POD>); s/^.*?-\s+//s; s/\n/ /g; #write; my $v; if (defined ($v = getversion($Module))) { print "$Module ($v) "; } else { print "$Module "; } print "- $_\n"; return 1; } } warn "\t(MISSING DESC FOR $File::Find::name)\n" if $opt_w; return 0;}# run Perl to load the module and print its verson number, redirecting# errors to /dev/nullsub getversion { my $mod = shift; my $vers = `$^X -m$mod -e 'print \$${mod}::VERSION' 2>/dev/null`; $vers =~ s/^\s*(.*?)\s*$/$1/; # remove stray whitespace return ($vers || undef);}format =^<<<<<<<<<<<<<<<<<~~^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<$Module, $_.<ACLASS="indexterm"NAME="ch12-idx-1000005325-0"></A><ACLASS="indexterm"NAME="ch12-idx-1000005325-1"></A><ACLASS="indexterm"NAME="ch12-idx-1000005325-2"></A></PRE></DIV><ICLASS="comment"></I></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch12_19.htm"TITLE="12.18. Example: Module Template"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 12.18. Example: Module Template"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="book"HREF="index.htm"TITLE="Perl Cookbook"><IMGSRC="../gifs/txthome.gif"ALT="Perl Cookbook"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="chapter"HREF="ch13_01.htm"TITLE="13. Classes, Objects, and Ties"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 13. Classes, Objects, and Ties"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">12.18. Example: Module Template</TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="index"HREF="index/index.htm"TITLE="Book Index"><IMGSRC="../gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228">13. Classes, Objects, and Ties</TD></TR></TABLE><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><FONTSIZE="-1"></DIV<!-- LIBRARY NAV BAR --> <img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p> <a href="copyrght.htm">Copyright © 2002</a> O'Reilly & Associates. All rights reserved.</font> </p> <map name="library-map"> <area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map> </BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -