home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / perl / WAIT.pm < prev    next >
Encoding:
Perl POD Document  |  2000-03-23  |  11.4 KB  |  379 lines

  1. #                              -*- Mode: Perl -*- 
  2. # $Basename: WAIT.pm $
  3. # $Revision: 1.6 $
  4. # Author          : Ulrich Pfeifer
  5. # Created On      : Fri Jan 31 11:30:46 1997
  6. # Last Modified By: Ulrich Pfeifer
  7. # Last Modified On: Thu Mar 23 21:19:20 2000
  8. # Language        : CPerl
  9. # Update Count    : 145
  10. # Status          : Unknown, Use with caution!
  11. # (C) Copyright 1997, Ulrich Pfeifer, all rights reserved.
  12.  
  13. package CPAN::WAIT;
  14. use ExtUtils::MakeMaker; # MM->catfile
  15.  
  16. # try to avoid 'use'ing CPAN.pm
  17. # code stolen from CPAN::Config::load ;
  18.  
  19. eval {require CPAN::Config;};       # We eval because of some
  20.                                     # MakeMaker problems
  21. unless ($CPAN::dot_cpan++){
  22.   unshift @INC, MM->catdir($ENV{HOME},".cpan");
  23.   eval {require CPAN::MyConfig;};   # where you can override
  24.                                     # system wide settings
  25.   shift @INC;
  26. }
  27.  
  28.  
  29.  
  30. require WAIT::Client;
  31. require FileHandle;
  32. use vars qw($VERSION $DEBUG $TIMEOUT);
  33.  
  34. # $Format: "\$\V\E\R\S\I\O\N = '$ModuleVersion$';"$ MM_Unix bug
  35. $VERSION = '0.27';
  36. $TIMEOUT = 20;                  # Set this to some larger value if you
  37.                                 # have a slow connection.
  38.  
  39. sub _open_connection () {
  40.   my ($host, $port, $con);
  41.   
  42.   # Make sure that there is a wait server to try
  43.   unless ($CPAN::Config->{'wait_list'}) {
  44.     $CPAN::Config->{'wait_list'} = ['wait://ls6.informatik.uni-dortmund.de'];
  45.   }
  46.   
  47.   # Try direct connection
  48.   my $server;
  49.  SERVER:
  50.   for $server (@{$CPAN::Config->{'wait_list'}}) {
  51.     warn "CPAN::WAIT $VERSION checking $server\n" if $DEBUG;
  52.     if ($server =~ m(^wait://([^:]+)(?::(\d+))?)) {
  53.       ($host, $port) = ($1, $2 || 1404);
  54.       # Constructor is inherited from Net::NNTP
  55.       $con = WAIT::Client->new($host, Port => $port, Timeout => $TIMEOUT)
  56.         unless $DEBUG and $DEBUG =~ /force proxy/;
  57.       last SERVER if $con;
  58.     }
  59.   }
  60.   
  61.   # Try connection via an http proxy
  62.   unless ($con) {
  63.     warn "Could not connect to the WAIT server at $host port $port\n"
  64.       unless $DEBUG and $DEBUG =~ /force proxy/;
  65.     
  66.     if ($CPAN::Config->{'http_proxy'}) {
  67.       print "Trying your http proxy $CPAN::Config->{'http_proxy'}\n";
  68.     SERVER:
  69.       for $server (@{$CPAN::Config->{'wait_list'}}) {
  70.         if ($server =~ m(^wait://([^:]+)(?::(\d+))?)) {
  71.           ($host, $port) = ($1, $2 || 1404);
  72.           $con = WAIT::Client::HTTP->new($host,
  73.                                          Port  => $port,
  74.                                          Proxy => $CPAN::Config->{'http_proxy'},
  75.                                          Timeout => $TIMEOUT);
  76.           last SERVER if $con;
  77.         }
  78.       }
  79.       warn "No luck with your proxy either. Giving up\n"
  80.         unless $con;
  81.     } else {
  82.       warn "You did not tell the CPAN module about an http proxy.\n" .
  83.         "I could use such a beast instead of a direct connection.\n";
  84.     }
  85.   }
  86.   
  87.   # We had no luck.
  88.   warn "No searching available!\n" unless $con;
  89.   
  90.   return $con;
  91. }
  92.  
  93. my $con;
  94. # Temporary file for retrieved documents
  95. my $tmp = MM->catfile($CPAN::Config->{'cpan_home'}, 'w4c.pod');
  96.  
  97. # run a search
  98. sub wq {
  99.   my $self = shift;
  100.   my $result;
  101.   local ($") = ' ';
  102.  
  103.   $con ||= _open_connection || return;
  104.   print "Searching for '@_'\n";
  105.   unless ($result = $con->search(@_)) {
  106.     print "Your query contains a syntax error.\n";
  107.     $self->wh('wq');
  108.   } else {
  109.     print $con->message;
  110.     print @{$result};
  111.     print "Type 'wr <number>' or 'wd <number>' to examine the results\n";
  112.   }
  113.   $result;
  114. }
  115.  
  116. # display hit record
  117. sub wr {
  118.   my $self = shift;
  119.   my $hit  = shift;
  120.   my $result;
  121.  
  122.   if (@_ or !$hit) {
  123.     print "USAGE: wr <hit-number>\n";
  124.   } else {
  125.     $con ||= _open_connection || return;
  126.     print "fetching info on hit number $hit\n";
  127.     $result = $con->info($hit);
  128.     print @$result;
  129.   }
  130.   $result;
  131. }
  132.  
  133. # display hit document
  134. sub wd {
  135.   my $self = shift;
  136.   my $hit  = shift;
  137.   my $result;
  138.  
  139.   if (@_ or !$hit) {
  140.     print "USAGE: wd <hit-number>\n";
  141.     return;
  142.   } 
  143.   $con ||= _open_connection || return;
  144.   print "Get hit number $hit ...";
  145.   my $text  = $con->get($hit);
  146.   my $lines = ($text)?@$text:'no';
  147.   print " done\nGot $lines lines\nRunning perldoc on it ...\n";
  148.   
  149.   # perldoc does not read STDIN; so we need a temp file
  150.   {
  151.     my $fh = new FileHandle ">$tmp";
  152.     $fh->print(@{$text});
  153.   }
  154.  
  155.   # is system available every were ??
  156.   system $^X, '-S', 'perldoc', $tmp
  157.     and warn "Could not run '$^X -S perldoc $tmp': $?\n"
  158.       and system $Config{'pager'}, $tmp
  159.         and warn "Could not run '$Config{'pager'} $tmp': $?\n"
  160.           and print @$text;
  161.   $text;
  162. }
  163.  
  164. sub wl {
  165.   my $self = shift;
  166.   my $hits = shift;
  167.   
  168.   if (@_) {
  169.     print "USAGE: wl <maximum-hit-count>\n";
  170.     return;
  171.   }
  172.   $con ||= _open_connection || return;
  173.   print "Setting maximum hit count to $hits\n";
  174.   $con->hits($hits);
  175. }
  176.  
  177. my %HELP =
  178.   (
  179.    'h' => q{
  180. 'wh'           displays a short summary of commands available via the WAIT
  181.                plugin.
  182. 'wh <command>' displays information about a the command given as argument
  183.    },
  184.    'q' => q{
  185. Here are some query examples:
  186.  
  187. information retrieval               free text query 
  188. information or retrieval            same as above 
  189. des=information retrieval           `information' must be in the description 
  190. des=(information retrieval)         one of them in description 
  191. des=(information or retrieval)      same as above 
  192. des=(information and retrieval)     both of them in description 
  193. des=(information not retrieval)     `information' in description and
  194.                                     `retrieval' not in description 
  195. des=(information system*)           wild-card search
  196. au=ilia                             author names may be misspelled
  197.  
  198. You can build arbitary boolean combination of the above examples.
  199. The following fields are known: 
  200.  
  201.   'synopsis', 'name', 'bugs', 'author', 'example', 'description',
  202.   'environment'
  203.  
  204. Field names may be abbreviated.
  205. },
  206.    'r' => q{
  207. 'wr <hit-number>'  displays the record of the selected hit. Records look
  208.                    like this:
  209.  
  210.    source          authors/id/CHIPS/perl5.003_24.tar.gz
  211.    headline        perl - Practical Extraction and Report Language 
  212.    size            12786
  213.    docid           data/perl/pod/perl.pod
  214.  
  215. 'source'   is the patch relative to http://www.perl.org/CPAN/.
  216. 'headline' is the contents of the 'NAME' section of the POD document
  217. 'size'     is the size of the POD document (not the size of the tar archive)!
  218. 'docid'    is the path the POS document is stored in. It should be the
  219.            path in the tar archive minus the version number + a 'data'
  220.            prefix.
  221.    },
  222.    'l' => q{
  223. Since answers to queries are sorted by decreasing probability of relevance,
  224. you will probably be interested only in the first few hits. To limit the amout
  225. of network traffic, the WAIT server only returns the best 10 hits per default.
  226. You can change this limit with 'wl <number>'.
  227.    },
  228.    'd' => q{
  229. The 'wd <hit-number>' command retrieves the POD document form the
  230. server and stores it in the file 'w2c' in your CPAN directory. Then it
  231. runs 'perlpod' on it. If you have problems, check if you local
  232. 'perlpod' works with absolute path names. Older versions are know to
  233. fail. Also try to avoid fetching of large documents like 'perlfunc.pod'.
  234. Use 'wr <hit-number>' to see how large the documents are before fetching
  235. the actually if you have a slow connection.
  236. },
  237.  
  238.   );
  239.  
  240. sub wh {
  241.   my $self = shift;
  242.   my $cmd  = shift;
  243.  
  244.   if ($cmd and $cmd =~ /(\w)$/) {
  245.     print $HELP{$1} || "No help for 'w$1' yet.\n";
  246.   } else {
  247.     print qq[
  248. Available commands:
  249. wq        query           search the WAIT4CPAN server
  250. wr        hit-number      display search result record
  251. wd        hit-number      fetch the document and run perldoc on it
  252. wl        count           limit search to <count> hits
  253. wh        command         displays help on command if available
  254. ];
  255.   }
  256.   1;
  257. }
  258.  
  259. END {
  260.   unlink $tmp if -e $tmp;
  261. }
  262.  
  263. 1;
  264.  
  265. __DATA__
  266.  
  267. =head1 NAME
  268.  
  269. CPAN::WAIT - adds commands to search a WAIT4CPAN server to the CPAN C<shell()>
  270.  
  271. =head1 SYNOPSIS
  272.  
  273.   perl -MCPAN -e shell
  274.   > wq au=wall
  275.   > wr 3
  276.   > wd 3
  277.   > wl 20
  278.   > wh
  279.   > wh wq
  280.  
  281. =head1 DESCRIPTION
  282.  
  283. B<CPAN::WAIT> adds some comands to the CPAN C<shell()> to perform
  284. searches on a WAIT server. It connects to a WAIT server using a simple
  285. protocoll resembling NNTP as described in RFC977. It uses the
  286. B<WAIT::Client> module to handle this connection. This in turn
  287. inherits from B<Net::NNTP> from the F<libnet> package. So you need
  288. B<Net::NNTP> to use this module.
  289.  
  290. If no direct connection to the WAIT server is possible, the modules
  291. tries to connect via your HTTP proxy (as given by the CPAN
  292. configuration). Be warned though that the emulation of the stateful
  293. protocol via HTTP is slow.
  294.  
  295. The variable C<CPAN::WAIT::TIMEOUT> specifies the number of seconds to
  296. wait for an answer from the server. The default is 20. You may want to
  297. set it to some larger value if you have a slow connection.
  298.  
  299. The commands available are:
  300.  
  301. =over 
  302.  
  303. =item B<wh> [B<command>]
  304.  
  305. Displays a short help message if called without arguments. If you
  306. provide the name of another command you will get more information on
  307. this command if available. Currently only B<wq> will be explained.
  308.  
  309. =item B<wl> I<count>
  310.  
  311. Limit the number of hits returned in a search to I<count>. The limit
  312. usually is set ot 10 of you don't set it.
  313.  
  314. =item B<wq> I<query>
  315.  
  316. Send a query to the server. 
  317.  
  318. Here are some query examples:
  319.  
  320.   information retrieval               free text query 
  321.   information or retrieval            same as above 
  322.   des=information retrieval           `information' must be in the description 
  323.   des=(information retrieval)         one of them in description 
  324.   des=(information or retrieval)      same as above 
  325.   des=(information and retrieval)     both of them in description 
  326.   des=(information not retrieval)     `information' in description and
  327.                                       `retrieval' not in description 
  328.   des=(information system*)           wild-card search
  329.   au=ilia                             author names may be misspelled
  330.  
  331. You can build arbitary boolean combination of the above examples.
  332. Field names may be abbreviated. For further information see
  333. F<http://ls6-www.informatik.uni-dortmund.de/CPAN>
  334.  
  335. The result should look like this:
  336.  
  337.   wq au=wall
  338.  
  339.    1 8.039 a2p - Awk to Perl translator 
  340.    2 8.039 s2p - Sed to Perl translator 
  341.    3 8.039 perlipc - Perl interprocess communication (signals, fifos, pipes, safe subprocesses, sockets, and semaphores) 
  342.    4 8.039 ExtUtils::DynaGlue - Methods for generating Perl extension files 
  343.    5 8.039 h2xs - convert .h C header files to Perl extensions 
  344.    6 8.039 Sys::Syslog, openlog, closelog, setlogmask, syslog - Perl interface to the UNIX syslog(3) calls 
  345.    7 8.039 h2ph - convert .h C header files to .ph Perl header files 
  346.    8 8.039 Shell - run shell commands transparently within perl 
  347.    9 8.039 pl2pm - Rough tool to translate Perl4 .pl files to Perl5 .pm modules. 
  348.   10 8.039 perlpod - plain old documentation 
  349.  
  350. =item B<wr> I<hit-number>
  351.  
  352. Display the Record of hit number I<hit-number>:
  353.  
  354.   wr 1
  355.   
  356.   source          authors/id/CHIPS/perl5.003_24.tar.gz
  357.   headline        a2p - Awk to Perl translator 
  358.   size            5643
  359.   docid           data/perl/x2p/a2p.pod
  360.  
  361.  
  362. =item B<wd> I<hit-number>
  363.  
  364. Fetches the full text from the server and runs B<perlpod> on it. Make
  365. sure that you have B<perlpod> in your path. Also check if your
  366. B<perlpod> version can handle absolute pathes. Some older versions
  367. ironically do not find a document if the full patch is given on the
  368. command line.
  369.  
  370. =back
  371.  
  372. =head1 AUTHOR
  373.  
  374. Ulrich Pfeifer E<lt>F<pfeifer@ls6.informatik.uni-dortmund.de>E<gt>
  375.  
  376.