home *** CD-ROM | disk | FTP | other *** search
/ PC World 2003 March / PCWorld_2003-03_cd.bin / Software / Topware / activeperl / ActivePerl / Perl / lib / CPAN.pm < prev    next >
Encoding:
Perl POD Document  |  2002-06-19  |  222.1 KB  |  7,056 lines

  1. # -*- Mode: cperl; coding: utf-8; cperl-indent-level: 4 -*-
  2. package CPAN;
  3. $VERSION = '1.61';
  4. # $Id: CPAN.pm,v 1.390 2002/05/07 10:04:58 k Exp $
  5.  
  6. # only used during development:
  7. $Revision = "";
  8. # $Revision = "[".substr(q$Revision: 1.390 $, 10)."]";
  9.  
  10. use Carp ();
  11. use Config ();
  12. use Cwd ();
  13. use DirHandle;
  14. use Exporter ();
  15. use ExtUtils::MakeMaker (); # $SelfLoader::DEBUG=1;
  16. use File::Basename ();
  17. use File::Copy ();
  18. use File::Find;
  19. use File::Path ();
  20. use FileHandle ();
  21. use Safe ();
  22. use Text::ParseWords ();
  23. use Text::Wrap;
  24. use File::Spec;
  25. use Sys::Hostname;
  26. no lib "."; # we need to run chdir all over and we would get at wrong
  27.             # libraries there
  28.  
  29. require Mac::BuildTools if $^O eq 'MacOS';
  30.  
  31. END { $End++; &cleanup; }
  32.  
  33. %CPAN::DEBUG = qw[
  34.           CPAN              1
  35.           Index             2
  36.           InfoObj           4
  37.           Author            8
  38.           Distribution     16
  39.           Bundle           32
  40.           Module           64
  41.           CacheMgr        128
  42.           Complete        256
  43.           FTP             512
  44.           Shell          1024
  45.           Eval           2048
  46.           Config         4096
  47.           Tarzip         8192
  48.           Version       16384
  49.           Queue         32768
  50. ];
  51.  
  52. $CPAN::DEBUG ||= 0;
  53. $CPAN::Signal ||= 0;
  54. $CPAN::Frontend ||= "CPAN::Shell";
  55. $CPAN::Defaultsite ||= "ftp://ftp.perl.org/pub/CPAN";
  56.  
  57. package CPAN;
  58. use strict qw(vars);
  59.  
  60. use vars qw($VERSION @EXPORT $AUTOLOAD $DEBUG $META $HAS_USABLE $term
  61.             $Revision $Signal $End $Suppress_readline $Frontend
  62.             $Defaultsite $Have_warned);
  63.  
  64. @CPAN::ISA = qw(CPAN::Debug Exporter);
  65.  
  66. @EXPORT = qw(
  67.          autobundle bundle expand force get cvs_import
  68.          install make readme recompile shell test clean
  69.         );
  70.  
  71. #-> sub CPAN::AUTOLOAD ;
  72. sub AUTOLOAD {
  73.     my($l) = $AUTOLOAD;
  74.     $l =~ s/.*:://;
  75.     my(%EXPORT);
  76.     @EXPORT{@EXPORT} = '';
  77.     CPAN::Config->load unless $CPAN::Config_loaded++;
  78.     if (exists $EXPORT{$l}){
  79.     CPAN::Shell->$l(@_);
  80.     } else {
  81.     $CPAN::Frontend->mywarn(qq{Unknown command "$AUTOLOAD". }.
  82.                 qq{Type ? for help.
  83. });
  84.     }
  85. }
  86.  
  87. #-> sub CPAN::shell ;
  88. sub shell {
  89.     my($self) = @_;
  90.     $Suppress_readline = ! -t STDIN unless defined $Suppress_readline;
  91.     CPAN::Config->load unless $CPAN::Config_loaded++;
  92.  
  93.     my $oprompt = shift || "cpan> ";
  94.     my $prompt = $oprompt;
  95.     my $commandline = shift || "";
  96.  
  97.     local($^W) = 1;
  98.     unless ($Suppress_readline) {
  99.     require Term::ReadLine;
  100.         if (! $term
  101.             or
  102.             $term->ReadLine eq "Term::ReadLine::Stub"
  103.            ) {
  104.             $term = Term::ReadLine->new('CPAN Monitor');
  105.         }
  106.     if ($term->ReadLine eq "Term::ReadLine::Gnu") {
  107.         my $attribs = $term->Attribs;
  108.          $attribs->{attempted_completion_function} = sub {
  109.          &CPAN::Complete::gnu_cpl;
  110.          }
  111.     } else {
  112.         $readline::rl_completion_function =
  113.         $readline::rl_completion_function = 'CPAN::Complete::cpl';
  114.     }
  115.     # $term->OUT is autoflushed anyway
  116.     my $odef = select STDERR;
  117.     $| = 1;
  118.     select STDOUT;
  119.     $| = 1;
  120.     select $odef;
  121.     }
  122.  
  123.     # no strict; # I do not recall why no strict was here (2000-09-03)
  124.     $META->checklock();
  125.     my $cwd = CPAN::anycwd();
  126.     my $try_detect_readline;
  127.     $try_detect_readline = $term->ReadLine eq "Term::ReadLine::Stub" if $term;
  128.     my $rl_avail = $Suppress_readline ? "suppressed" :
  129.     ($term->ReadLine ne "Term::ReadLine::Stub") ? "enabled" :
  130.         "available (try 'install Bundle::CPAN')";
  131.  
  132.     $CPAN::Frontend->myprint(
  133.                  sprintf qq{
  134. cpan shell -- CPAN exploration and modules installation (v%s%s)
  135. ReadLine support %s
  136.  
  137. },
  138.                              $CPAN::VERSION,
  139.                              $CPAN::Revision,
  140.                              $rl_avail
  141.                             )
  142.         unless $CPAN::Config->{'inhibit_startup_message'} ;
  143.     my($continuation) = "";
  144.   SHELLCOMMAND: while () {
  145.     if ($Suppress_readline) {
  146.         print $prompt;
  147.         last SHELLCOMMAND unless defined ($_ = <> );
  148.         chomp;
  149.     } else {
  150.         last SHELLCOMMAND unless
  151.                 defined ($_ = $term->readline($prompt, $commandline));
  152.     }
  153.     $_ = "$continuation$_" if $continuation;
  154.     s/^\s+//;
  155.     next SHELLCOMMAND if /^$/;
  156.     $_ = 'h' if /^\s*\?/;
  157.     if (/^(?:q(?:uit)?|bye|exit)$/i) {
  158.         last SHELLCOMMAND;
  159.     } elsif (s/\\$//s) {
  160.         chomp;
  161.         $continuation = $_;
  162.         $prompt = "    > ";
  163.     } elsif (/^\!/) {
  164.         s/^\!//;
  165.         my($eval) = $_;
  166.         package CPAN::Eval;
  167.         use vars qw($import_done);
  168.         CPAN->import(':DEFAULT') unless $import_done++;
  169.         CPAN->debug("eval[$eval]") if $CPAN::DEBUG;
  170.         eval($eval);
  171.         warn $@ if $@;
  172.         $continuation = "";
  173.         $prompt = $oprompt;
  174.     } elsif (/./) {
  175.         my(@line);
  176.         if ($] < 5.00322) { # parsewords had a bug until recently
  177.         @line = split;
  178.         } else {
  179.         eval { @line = Text::ParseWords::shellwords($_) };
  180.         warn($@), next SHELLCOMMAND if $@;
  181.                 warn("Text::Parsewords could not parse the line [$_]"),
  182.                     next SHELLCOMMAND unless @line;
  183.         }
  184.         $CPAN::META->debug("line[".join("|",@line)."]") if $CPAN::DEBUG;
  185.         my $command = shift @line;
  186.         eval { CPAN::Shell->$command(@line) };
  187.         warn $@ if $@;
  188.         chdir $cwd or $CPAN::Frontend->mydie(qq{Could not chdir to "$cwd": $!});
  189.         $CPAN::Frontend->myprint("\n");
  190.         $continuation = "";
  191.         $prompt = $oprompt;
  192.     }
  193.     } continue {
  194.       $commandline = ""; # I do want to be able to pass a default to
  195.                          # shell, but on the second command I see no
  196.                          # use in that
  197.       $Signal=0;
  198.       CPAN::Queue->nullify_queue;
  199.       if ($try_detect_readline) {
  200.     if ($CPAN::META->has_inst("Term::ReadLine::Gnu")
  201.         ||
  202.         $CPAN::META->has_inst("Term::ReadLine::Perl")
  203.        ) {
  204.         delete $INC{"Term/ReadLine.pm"};
  205.         my $redef = 0;
  206.         local($SIG{__WARN__}) = CPAN::Shell::paintdots_onreload(\$redef);
  207.         require Term::ReadLine;
  208.         $CPAN::Frontend->myprint("\n$redef subroutines in ".
  209.                      "Term::ReadLine redefined\n");
  210.             @_ = ($oprompt,"");
  211.         goto &shell;
  212.     }
  213.       }
  214.     }
  215.     chdir $cwd or $CPAN::Frontend->mydie(qq{Could not chdir to "$cwd": $!});
  216. }
  217.  
  218. package CPAN::CacheMgr;
  219. @CPAN::CacheMgr::ISA = qw(CPAN::InfoObj CPAN);
  220. use File::Find;
  221.  
  222. package CPAN::Config;
  223. use vars qw(%can $dot_cpan);
  224.  
  225. %can = (
  226.   'commit' => "Commit changes to disk",
  227.   'defaults' => "Reload defaults from disk",
  228.   'init'   => "Interactive setting of all options",
  229. );
  230.  
  231. package CPAN::FTP;
  232. use vars qw($Ua $Thesite $Themethod);
  233. @CPAN::FTP::ISA = qw(CPAN::Debug);
  234.  
  235. package CPAN::LWP::UserAgent;
  236. use vars qw(@ISA $USER $PASSWD $SETUPDONE);
  237. # we delay requiring LWP::UserAgent and setting up inheritence until we need it
  238.  
  239. package CPAN::Complete;
  240. @CPAN::Complete::ISA = qw(CPAN::Debug);
  241. @CPAN::Complete::COMMANDS = sort qw(
  242.                ! a b d h i m o q r u autobundle clean dump
  243.                make test install force readme reload look
  244.                        cvs_import ls
  245. ) unless @CPAN::Complete::COMMANDS;
  246.  
  247. package CPAN::Index;
  248. use vars qw($LAST_TIME $DATE_OF_02 $DATE_OF_03);
  249. @CPAN::Index::ISA = qw(CPAN::Debug);
  250. $LAST_TIME ||= 0;
  251. $DATE_OF_03 ||= 0;
  252. # use constant PROTOCOL => "2.0"; # outcommented to avoid warning on upgrade from 1.57
  253. sub PROTOCOL { 2.0 }
  254.  
  255. package CPAN::InfoObj;
  256. @CPAN::InfoObj::ISA = qw(CPAN::Debug);
  257.  
  258. package CPAN::Author;
  259. @CPAN::Author::ISA = qw(CPAN::InfoObj);
  260.  
  261. package CPAN::Distribution;
  262. @CPAN::Distribution::ISA = qw(CPAN::InfoObj);
  263.  
  264. package CPAN::Bundle;
  265. @CPAN::Bundle::ISA = qw(CPAN::Module);
  266.  
  267. package CPAN::Module;
  268. @CPAN::Module::ISA = qw(CPAN::InfoObj);
  269.  
  270. package CPAN::Shell;
  271. use vars qw($AUTOLOAD @ISA $COLOR_REGISTERED $ADVANCED_QUERY $PRINT_ORNAMENTING);
  272. @CPAN::Shell::ISA = qw(CPAN::Debug);
  273. $COLOR_REGISTERED ||= 0;
  274. $PRINT_ORNAMENTING ||= 0;
  275.  
  276. #-> sub CPAN::Shell::AUTOLOAD ;
  277. sub AUTOLOAD {
  278.     my($autoload) = $AUTOLOAD;
  279.     my $class = shift(@_);
  280.     # warn "autoload[$autoload] class[$class]";
  281.     $autoload =~ s/.*:://;
  282.     if ($autoload =~ /^w/) {
  283.     if ($CPAN::META->has_inst('CPAN::WAIT')) {
  284.         CPAN::WAIT->$autoload(@_);
  285.     } else {
  286.         $CPAN::Frontend->mywarn(qq{
  287. Commands starting with "w" require CPAN::WAIT to be installed.
  288. Please consider installing CPAN::WAIT to use the fulltext index.
  289. For this you just need to type
  290.     install CPAN::WAIT
  291. });
  292.     }
  293.     } else {
  294.     $CPAN::Frontend->mywarn(qq{Unknown command '$autoload'. }.
  295.                 qq{Type ? for help.
  296. });
  297.     }
  298. }
  299.  
  300. package CPAN::Tarzip;
  301. use vars qw($AUTOLOAD @ISA $BUGHUNTING);
  302. @CPAN::Tarzip::ISA = qw(CPAN::Debug);
  303. $BUGHUNTING = 0; # released code must have turned off
  304.  
  305. package CPAN::Queue;
  306.  
  307. # One use of the queue is to determine if we should or shouldn't
  308. # announce the availability of a new CPAN module
  309.  
  310. # Now we try to use it for dependency tracking. For that to happen
  311. # we need to draw a dependency tree and do the leaves first. This can
  312. # easily be reached by running CPAN.pm recursively, but we don't want
  313. # to waste memory and run into deep recursion. So what we can do is
  314. # this:
  315.  
  316. # CPAN::Queue is the package where the queue is maintained. Dependencies
  317. # often have high priority and must be brought to the head of the queue,
  318. # possibly by jumping the queue if they are already there. My first code
  319. # attempt tried to be extremely correct. Whenever a module needed
  320. # immediate treatment, I either unshifted it to the front of the queue,
  321. # or, if it was already in the queue, I spliced and let it bypass the
  322. # others. This became a too correct model that made it impossible to put
  323. # an item more than once into the queue. Why would you need that? Well,
  324. # you need temporary duplicates as the manager of the queue is a loop
  325. # that
  326. #
  327. #  (1) looks at the first item in the queue without shifting it off
  328. #
  329. #  (2) cares for the item
  330. #
  331. #  (3) removes the item from the queue, *even if its agenda failed and
  332. #      even if the item isn't the first in the queue anymore* (that way
  333. #      protecting against never ending queues)
  334. #
  335. # So if an item has prerequisites, the installation fails now, but we
  336. # want to retry later. That's easy if we have it twice in the queue.
  337. #
  338. # I also expect insane dependency situations where an item gets more
  339. # than two lives in the queue. Simplest example is triggered by 'install
  340. # Foo Foo Foo'. People make this kind of mistakes and I don't want to
  341. # get in the way. I wanted the queue manager to be a dumb servant, not
  342. # one that knows everything.
  343. #
  344. # Who would I tell in this model that the user wants to be asked before
  345. # processing? I can't attach that information to the module object,
  346. # because not modules are installed but distributions. So I'd have to
  347. # tell the distribution object that it should ask the user before
  348. # processing. Where would the question be triggered then? Most probably
  349. # in CPAN::Distribution::rematein.
  350. # Hope that makes sense, my head is a bit off:-) -- AK
  351.  
  352. use vars qw{ @All };
  353.  
  354. # CPAN::Queue::new ;
  355. sub new {
  356.   my($class,$s) = @_;
  357.   my $self = bless { qmod => $s }, $class;
  358.   push @All, $self;
  359.   return $self;
  360. }
  361.  
  362. # CPAN::Queue::first ;
  363. sub first {
  364.   my $obj = $All[0];
  365.   $obj->{qmod};
  366. }
  367.  
  368. # CPAN::Queue::delete_first ;
  369. sub delete_first {
  370.   my($class,$what) = @_;
  371.   my $i;
  372.   for my $i (0..$#All) {
  373.     if (  $All[$i]->{qmod} eq $what ) {
  374.       splice @All, $i, 1;
  375.       return;
  376.     }
  377.   }
  378. }
  379.  
  380. # CPAN::Queue::jumpqueue ;
  381. sub jumpqueue {
  382.     my $class = shift;
  383.     my @what = @_;
  384.     CPAN->debug(sprintf("before jumpqueue All[%s] what[%s]",
  385.                         join(",",map {$_->{qmod}} @All),
  386.                         join(",",@what)
  387.                        )) if $CPAN::DEBUG;
  388.   WHAT: for my $what (reverse @what) {
  389.         my $jumped = 0;
  390.         for (my $i=0; $i<$#All;$i++) { #prevent deep recursion
  391.             CPAN->debug("i[$All[$i]]what[$what]") if $CPAN::DEBUG;
  392.             if ($All[$i]->{qmod} eq $what){
  393.                 $jumped++;
  394.                 if ($jumped > 100) { # one's OK if e.g. just
  395.                                      # processing now; more are OK if
  396.                                      # user typed it several times
  397.                     $CPAN::Frontend->mywarn(
  398. qq{Object [$what] queued more than 100 times, ignoring}
  399.                  );
  400.                     next WHAT;
  401.                 }
  402.             }
  403.         }
  404.         my $obj = bless { qmod => $what }, $class;
  405.         unshift @All, $obj;
  406.     }
  407.     CPAN->debug(sprintf("after jumpqueue All[%s] what[%s]",
  408.                         join(",",map {$_->{qmod}} @All),
  409.                         join(",",@what)
  410.                        )) if $CPAN::DEBUG;
  411. }
  412.  
  413. # CPAN::Queue::exists ;
  414. sub exists {
  415.   my($self,$what) = @_;
  416.   my @all = map { $_->{qmod} } @All;
  417.   my $exists = grep { $_->{qmod} eq $what } @All;
  418.   # warn "in exists what[$what] all[@all] exists[$exists]";
  419.   $exists;
  420. }
  421.  
  422. # CPAN::Queue::delete ;
  423. sub delete {
  424.   my($self,$mod) = @_;
  425.   @All = grep { $_->{qmod} ne $mod } @All;
  426. }
  427.  
  428. # CPAN::Queue::nullify_queue ;
  429. sub nullify_queue {
  430.   @All = ();
  431. }
  432.  
  433.  
  434.  
  435. package CPAN;
  436.  
  437. $META ||= CPAN->new; # In case we re-eval ourselves we need the ||
  438.  
  439. # from here on only subs.
  440. ################################################################################
  441.  
  442. #-> sub CPAN::all_objects ;
  443. sub all_objects {
  444.     my($mgr,$class) = @_;
  445.     CPAN::Config->load unless $CPAN::Config_loaded++;
  446.     CPAN->debug("mgr[$mgr] class[$class]") if $CPAN::DEBUG;
  447.     CPAN::Index->reload;
  448.     values %{ $META->{readwrite}{$class} }; # unsafe meta access, ok
  449. }
  450. *all = \&all_objects;
  451.  
  452. # Called by shell, not in batch mode. In batch mode I see no risk in
  453. # having many processes updating something as installations are
  454. # continually checked at runtime. In shell mode I suspect it is
  455. # unintentional to open more than one shell at a time
  456.  
  457. #-> sub CPAN::checklock ;
  458. sub checklock {
  459.     my($self) = @_;
  460.     my $lockfile = File::Spec->catfile($CPAN::Config->{cpan_home},".lock");
  461.     if (-f $lockfile && -M _ > 0) {
  462.     my $fh = FileHandle->new($lockfile) or
  463.             $CPAN::Frontend->mydie("Could not open $lockfile: $!");
  464.     my $otherpid  = <$fh>;
  465.     my $otherhost = <$fh>;
  466.     $fh->close;
  467.     if (defined $otherpid && $otherpid) {
  468.         chomp $otherpid;
  469.         }
  470.     if (defined $otherhost && $otherhost) {
  471.         chomp $otherhost;
  472.     }
  473.     my $thishost  = hostname();
  474.     if (defined $otherhost && defined $thishost &&
  475.         $otherhost ne '' && $thishost ne '' &&
  476.         $otherhost ne $thishost) {
  477.             $CPAN::Frontend->mydie(sprintf("CPAN.pm panic: Lockfile $lockfile\n".
  478.                                            "reports other host $otherhost and other process $otherpid.\n".
  479.                                            "Cannot proceed.\n"));
  480.     }
  481.     elsif (defined $otherpid && $otherpid) {
  482.         return if $$ == $otherpid; # should never happen
  483.         $CPAN::Frontend->mywarn(
  484.                     qq{
  485. There seems to be running another CPAN process (pid $otherpid).  Contacting...
  486. });
  487.         if (kill 0, $otherpid) {
  488.         $CPAN::Frontend->mydie(qq{Other job is running.
  489. You may want to kill it and delete the lockfile, maybe. On UNIX try:
  490.     kill $otherpid
  491.     rm $lockfile
  492. });
  493.         } elsif (-w $lockfile) {
  494.         my($ans) =
  495.             ExtUtils::MakeMaker::prompt
  496.             (qq{Other job not responding. Shall I overwrite }.
  497.              qq{the lockfile? (Y/N)},"y");
  498.         $CPAN::Frontend->myexit("Ok, bye\n")
  499.             unless $ans =~ /^y/i;
  500.         } else {
  501.         Carp::croak(
  502.                 qq{Lockfile $lockfile not writeable by you. }.
  503.                 qq{Cannot proceed.\n}.
  504.                 qq{    On UNIX try:\n}.
  505.                 qq{    rm $lockfile\n}.
  506.                 qq{  and then rerun us.\n}
  507.                );
  508.         }
  509.     } else {
  510.             $CPAN::Frontend->mydie(sprintf("CPAN.pm panic: Lockfile $lockfile\n".
  511.                                            "reports other process with ID ".
  512.                                            "$otherpid. Cannot proceed.\n"));
  513.         }
  514.     }
  515.     my $dotcpan = $CPAN::Config->{cpan_home};
  516.     eval { File::Path::mkpath($dotcpan);};
  517.     if ($@) {
  518.       # A special case at least for Jarkko.
  519.       my $firsterror = $@;
  520.       my $seconderror;
  521.       my $symlinkcpan;
  522.       if (-l $dotcpan) {
  523.     $symlinkcpan = readlink $dotcpan;
  524.     die "readlink $dotcpan failed: $!" unless defined $symlinkcpan;
  525.     eval { File::Path::mkpath($symlinkcpan); };
  526.     if ($@) {
  527.       $seconderror = $@;
  528.     } else {
  529.       $CPAN::Frontend->mywarn(qq{
  530. Working directory $symlinkcpan created.
  531. });
  532.     }
  533.       }
  534.       unless (-d $dotcpan) {
  535.     my $diemess = qq{
  536. Your configuration suggests "$dotcpan" as your
  537. CPAN.pm working directory. I could not create this directory due
  538. to this error: $firsterror\n};
  539.     $diemess .= qq{
  540. As "$dotcpan" is a symlink to "$symlinkcpan",
  541. I tried to create that, but I failed with this error: $seconderror
  542. } if $seconderror;
  543.     $diemess .= qq{
  544. Please make sure the directory exists and is writable.
  545. };
  546.     $CPAN::Frontend->mydie($diemess);
  547.       }
  548.     }
  549.     my $fh;
  550.     unless ($fh = FileHandle->new(">$lockfile")) {
  551.     if ($! =~ /Permission/) {
  552.         my $incc = $INC{'CPAN/Config.pm'};
  553.         my $myincc = File::Spec->catfile($ENV{HOME},'.cpan','CPAN','MyConfig.pm');
  554.         $CPAN::Frontend->myprint(qq{
  555.  
  556. Your configuration suggests that CPAN.pm should use a working
  557. directory of
  558.     $CPAN::Config->{cpan_home}
  559. Unfortunately we could not create the lock file
  560.     $lockfile
  561. due to permission problems.
  562.  
  563. Please make sure that the configuration variable
  564.     \$CPAN::Config->{cpan_home}
  565. points to a directory where you can write a .lock file. You can set
  566. this variable in either
  567.     $incc
  568. or
  569.     $myincc
  570.  
  571. });
  572.     }
  573.     $CPAN::Frontend->mydie("Could not open >$lockfile: $!");
  574.     }
  575.     $fh->print($$, "\n");
  576.     $fh->print(hostname(), "\n");
  577.     $self->{LOCK} = $lockfile;
  578.     $fh->close;
  579.     $SIG{TERM} = sub {
  580.       &cleanup;
  581.       $CPAN::Frontend->mydie("Got SIGTERM, leaving");
  582.     };
  583.     $SIG{INT} = sub {
  584.       # no blocks!!!
  585.       &cleanup if $Signal;
  586.       $CPAN::Frontend->mydie("Got another SIGINT") if $Signal;
  587.       print "Caught SIGINT\n";
  588.       $Signal++;
  589.     };
  590.  
  591. #       From: Larry Wall <larry@wall.org>
  592. #       Subject: Re: deprecating SIGDIE
  593. #       To: perl5-porters@perl.org
  594. #       Date: Thu, 30 Sep 1999 14:58:40 -0700 (PDT)
  595. #
  596. #       The original intent of __DIE__ was only to allow you to substitute one
  597. #       kind of death for another on an application-wide basis without respect
  598. #       to whether you were in an eval or not.  As a global backstop, it should
  599. #       not be used any more lightly (or any more heavily :-) than class
  600. #       UNIVERSAL.  Any attempt to build a general exception model on it should
  601. #       be politely squashed.  Any bug that causes every eval {} to have to be
  602. #       modified should be not so politely squashed.
  603. #
  604. #       Those are my current opinions.  It is also my optinion that polite
  605. #       arguments degenerate to personal arguments far too frequently, and that
  606. #       when they do, it's because both people wanted it to, or at least didn't
  607. #       sufficiently want it not to.
  608. #
  609. #       Larry
  610.  
  611.     # global backstop to cleanup if we should really die
  612.     $SIG{__DIE__} = \&cleanup;
  613.     $self->debug("Signal handler set.") if $CPAN::DEBUG;
  614. }
  615.  
  616. #-> sub CPAN::DESTROY ;
  617. sub DESTROY {
  618.     &cleanup; # need an eval?
  619. }
  620.  
  621. #-> sub CPAN::anycwd ;
  622. sub anycwd () {
  623.     my $getcwd;
  624.     $getcwd = $CPAN::Config->{'getcwd'} || 'cwd';
  625.     CPAN->$getcwd();
  626. }
  627.  
  628. #-> sub CPAN::cwd ;
  629. sub cwd {Cwd::cwd();}
  630.  
  631. #-> sub CPAN::getcwd ;
  632. sub getcwd {Cwd::getcwd();}
  633.  
  634. #-> sub CPAN::exists ;
  635. sub exists {
  636.     my($mgr,$class,$id) = @_;
  637.     CPAN::Config->load unless $CPAN::Config_loaded++;
  638.     CPAN::Index->reload;
  639.     ### Carp::croak "exists called without class argument" unless $class;
  640.     $id ||= "";
  641.     exists $META->{readonly}{$class}{$id} or
  642.         exists $META->{readwrite}{$class}{$id}; # unsafe meta access, ok
  643. }
  644.  
  645. #-> sub CPAN::delete ;
  646. sub delete {
  647.   my($mgr,$class,$id) = @_;
  648.   delete $META->{readonly}{$class}{$id}; # unsafe meta access, ok
  649.   delete $META->{readwrite}{$class}{$id}; # unsafe meta access, ok
  650. }
  651.  
  652. #-> sub CPAN::has_usable
  653. # has_inst is sometimes too optimistic, we should replace it with this
  654. # has_usable whenever a case is given
  655. sub has_usable {
  656.     my($self,$mod,$message) = @_;
  657.     return 1 if $HAS_USABLE->{$mod};
  658.     my $has_inst = $self->has_inst($mod,$message);
  659.     return unless $has_inst;
  660.     my $usable;
  661.     $usable = {
  662.                LWP => [ # we frequently had "Can't locate object
  663.                         # method "new" via package "LWP::UserAgent" at
  664.                         # (eval 69) line 2006
  665.                        sub {require LWP},
  666.                        sub {require LWP::UserAgent},
  667.                        sub {require HTTP::Request},
  668.                        sub {require URI::URL},
  669.                       ],
  670.                Net::FTP => [
  671.                             sub {require Net::FTP},
  672.                             sub {require Net::Config},
  673.                            ]
  674.               };
  675.     if ($usable->{$mod}) {
  676.       for my $c (0..$#{$usable->{$mod}}) {
  677.         my $code = $usable->{$mod}[$c];
  678.         my $ret = eval { &$code() };
  679.         if ($@) {
  680.           warn "DEBUG: c[$c]\$\@[$@]ret[$ret]";
  681.           return;
  682.         }
  683.       }
  684.     }
  685.     return $HAS_USABLE->{$mod} = 1;
  686. }
  687.  
  688. #-> sub CPAN::has_inst
  689. sub has_inst {
  690.     my($self,$mod,$message) = @_;
  691.     Carp::croak("CPAN->has_inst() called without an argument")
  692.     unless defined $mod;
  693.     if (defined $message && $message eq "no"
  694.         ||
  695.         exists $CPAN::META->{dontload_hash}{$mod} # unsafe meta access, ok
  696.         ||
  697.         exists $CPAN::Config->{dontload_hash}{$mod}
  698.        ) {
  699.       $CPAN::META->{dontload_hash}{$mod}||=1; # unsafe meta access, ok
  700.       return 0;
  701.     }
  702.     my $file = $mod;
  703.     my $obj;
  704.     $file =~ s|::|/|g;
  705.     $file =~ s|/|\\|g if $^O eq 'MSWin32';
  706.     $file .= ".pm";
  707.     if ($INC{$file}) {
  708.     # checking %INC is wrong, because $INC{LWP} may be true
  709.     # although $INC{"URI/URL.pm"} may have failed. But as
  710.     # I really want to say "bla loaded OK", I have to somehow
  711.     # cache results.
  712.     ### warn "$file in %INC"; #debug
  713.     return 1;
  714.     } elsif (eval { require $file }) {
  715.     # eval is good: if we haven't yet read the database it's
  716.     # perfect and if we have installed the module in the meantime,
  717.     # it tries again. The second require is only a NOOP returning
  718.     # 1 if we had success, otherwise it's retrying
  719.  
  720.     $CPAN::Frontend->myprint("CPAN: $mod loaded ok\n");
  721.     if ($mod eq "CPAN::WAIT") {
  722.         push @CPAN::Shell::ISA, CPAN::WAIT;
  723.     }
  724.     return 1;
  725.     } elsif ($mod eq "Net::FTP") {
  726.     $CPAN::Frontend->mywarn(qq{
  727.   Please, install Net::FTP as soon as possible. CPAN.pm installs it for you
  728.   if you just type
  729.       install Bundle::libnet
  730.  
  731. }) unless $Have_warned->{"Net::FTP"}++;
  732.     sleep 3;
  733.     } elsif ($mod eq "Digest::MD5"){
  734.     $CPAN::Frontend->myprint(qq{
  735.   CPAN: MD5 security checks disabled because Digest::MD5 not installed.
  736.   Please consider installing the Digest::MD5 module.
  737.  
  738. });
  739.     sleep 2;
  740.     } else {
  741.     delete $INC{$file}; # if it inc'd LWP but failed during, say, URI
  742.     }
  743.     return 0;
  744. }
  745.  
  746. #-> sub CPAN::instance ;
  747. sub instance {
  748.     my($mgr,$class,$id) = @_;
  749.     CPAN::Index->reload;
  750.     $id ||= "";
  751.     # unsafe meta access, ok?
  752.     return $META->{readwrite}{$class}{$id} if exists $META->{readwrite}{$class}{$id};
  753.     $META->{readwrite}{$class}{$id} ||= $class->new(ID => $id);
  754. }
  755.  
  756. #-> sub CPAN::new ;
  757. sub new {
  758.     bless {}, shift;
  759. }
  760.  
  761. #-> sub CPAN::cleanup ;
  762. sub cleanup {
  763.   # warn "cleanup called with arg[@_] End[$End] Signal[$Signal]";
  764.   local $SIG{__DIE__} = '';
  765.   my($message) = @_;
  766.   my $i = 0;
  767.   my $ineval = 0;
  768.   if (
  769.       0 &&           # disabled, try reload cpan with it
  770.       $] > 5.004_60  # thereabouts
  771.      ) {
  772.     $ineval = $^S;
  773.   } else {
  774.     my($subroutine);
  775.     while ((undef,undef,undef,$subroutine) = caller(++$i)) {
  776.       $ineval = 1, last if
  777.       $subroutine eq '(eval)';
  778.     }
  779.   }
  780.   return if $ineval && !$End;
  781.   return unless defined $META->{LOCK}; # unsafe meta access, ok
  782.   return unless -f $META->{LOCK}; # unsafe meta access, ok
  783.   unlink $META->{LOCK}; # unsafe meta access, ok
  784.   # require Carp;
  785.   # Carp::cluck("DEBUGGING");
  786.   $CPAN::Frontend->mywarn("Lockfile removed.\n");
  787. }
  788.  
  789. sub is_tested {
  790.     my($self,$what) = @_;
  791.     $self->{is_tested}{$what} = 1;
  792. }
  793.  
  794. sub is_installed {
  795.     my($self,$what) = @_;
  796.     delete $self->{is_tested}{$what};
  797. }
  798.  
  799. sub set_perl5lib {
  800.     my($self) = @_;
  801.     $self->{is_tested} ||= {};
  802.     return unless %{$self->{is_tested}};
  803.     my $env = $ENV{PERL5LIB};
  804.     $env = $ENV{PERLLIB} unless defined $env;
  805.     my @env;
  806.     push @env, $env if defined $env and length $env;
  807.     my @dirs = map {("$_/blib/arch", "$_/blib/lib")} keys %{$self->{is_tested}};
  808.     $CPAN::Frontend->myprint("Prepending @dirs to PERL5LIB.\n");
  809.     $ENV{PERL5LIB} = join $Config::Config{path_sep}, @dirs, @env;
  810. }
  811.  
  812. package CPAN::CacheMgr;
  813.  
  814. #-> sub CPAN::CacheMgr::as_string ;
  815. sub as_string {
  816.     eval { require Data::Dumper };
  817.     if ($@) {
  818.     return shift->SUPER::as_string;
  819.     } else {
  820.     return Data::Dumper::Dumper(shift);
  821.     }
  822. }
  823.  
  824. #-> sub CPAN::CacheMgr::cachesize ;
  825. sub cachesize {
  826.     shift->{DU};
  827. }
  828.  
  829. #-> sub CPAN::CacheMgr::tidyup ;
  830. sub tidyup {
  831.   my($self) = @_;
  832.   return unless -d $self->{ID};
  833.   while ($self->{DU} > $self->{'MAX'} ) {
  834.     my($toremove) = shift @{$self->{FIFO}};
  835.     $CPAN::Frontend->myprint(sprintf(
  836.                      "Deleting from cache".
  837.                      ": $toremove (%.1f>%.1f MB)\n",
  838.                      $self->{DU}, $self->{'MAX'})
  839.                 );
  840.     return if $CPAN::Signal;
  841.     $self->force_clean_cache($toremove);
  842.     return if $CPAN::Signal;
  843.   }
  844. }
  845.  
  846. #-> sub CPAN::CacheMgr::dir ;
  847. sub dir {
  848.     shift->{ID};
  849. }
  850.  
  851. #-> sub CPAN::CacheMgr::entries ;
  852. sub entries {
  853.     my($self,$dir) = @_;
  854.     return unless defined $dir;
  855.     $self->debug("reading dir[$dir]") if $CPAN::DEBUG;
  856.     $dir ||= $self->{ID};
  857.     my($cwd) = CPAN::anycwd();
  858.     chdir $dir or Carp::croak("Can't chdir to $dir: $!");
  859.     my $dh = DirHandle->new(File::Spec->curdir)
  860.         or Carp::croak("Couldn't opendir $dir: $!");
  861.     my(@entries);
  862.     for ($dh->read) {
  863.     next if $_ eq "." || $_ eq "..";
  864.     if (-f $_) {
  865.         push @entries, File::Spec->catfile($dir,$_);
  866.     } elsif (-d _) {
  867.         push @entries, File::Spec->catdir($dir,$_);
  868.     } else {
  869.         $CPAN::Frontend->mywarn("Warning: weird direntry in $dir: $_\n");
  870.     }
  871.     }
  872.     chdir $cwd or Carp::croak("Can't chdir to $cwd: $!");
  873.     sort { -M $b <=> -M $a} @entries;
  874. }
  875.  
  876. #-> sub CPAN::CacheMgr::disk_usage ;
  877. sub disk_usage {
  878.     my($self,$dir) = @_;
  879.     return if exists $self->{SIZE}{$dir};
  880.     return if $CPAN::Signal;
  881.     my($Du) = 0;
  882.     find(
  883.      sub {
  884.        $File::Find::prune++ if $CPAN::Signal;
  885.        return if -l $_;
  886.        if ($^O eq 'MacOS') {
  887.          require Mac::Files;
  888.          my $cat  = Mac::Files::FSpGetCatInfo($_);
  889.          $Du += $cat->ioFlLgLen() + $cat->ioFlRLgLen() if $cat;
  890.        } else {
  891.          $Du += (-s _);
  892.        }
  893.      },
  894.      $dir
  895.     );
  896.     return if $CPAN::Signal;
  897.     $self->{SIZE}{$dir} = $Du/1024/1024;
  898.     push @{$self->{FIFO}}, $dir;
  899.     $self->debug("measured $dir is $Du") if $CPAN::DEBUG;
  900.     $self->{DU} += $Du/1024/1024;
  901.     $self->{DU};
  902. }
  903.  
  904. #-> sub CPAN::CacheMgr::force_clean_cache ;
  905. sub force_clean_cache {
  906.     my($self,$dir) = @_;
  907.     return unless -e $dir;
  908.     $self->debug("have to rmtree $dir, will free $self->{SIZE}{$dir}")
  909.     if $CPAN::DEBUG;
  910.     File::Path::rmtree($dir);
  911.     $self->{DU} -= $self->{SIZE}{$dir};
  912.     delete $self->{SIZE}{$dir};
  913. }
  914.  
  915. #-> sub CPAN::CacheMgr::new ;
  916. sub new {
  917.     my $class = shift;
  918.     my $time = time;
  919.     my($debug,$t2);
  920.     $debug = "";
  921.     my $self = {
  922.         ID => $CPAN::Config->{'build_dir'},
  923.         MAX => $CPAN::Config->{'build_cache'},
  924.         SCAN => $CPAN::Config->{'scan_cache'} || 'atstart',
  925.         DU => 0
  926.            };
  927.     File::Path::mkpath($self->{ID});
  928.     my $dh = DirHandle->new($self->{ID});
  929.     bless $self, $class;
  930.     $self->scan_cache;
  931.     $t2 = time;
  932.     $debug .= "timing of CacheMgr->new: ".($t2 - $time);
  933.     $time = $t2;
  934.     CPAN->debug($debug) if $CPAN::DEBUG;
  935.     $self;
  936. }
  937.  
  938. #-> sub CPAN::CacheMgr::scan_cache ;
  939. sub scan_cache {
  940.     my $self = shift;
  941.     return if $self->{SCAN} eq 'never';
  942.     $CPAN::Frontend->mydie("Unknown scan_cache argument: $self->{SCAN}")
  943.     unless $self->{SCAN} eq 'atstart';
  944.     $CPAN::Frontend->myprint(
  945.                  sprintf("Scanning cache %s for sizes\n",
  946.                      $self->{ID}));
  947.     my $e;
  948.     for $e ($self->entries($self->{ID})) {
  949.     next if $e eq ".." || $e eq ".";
  950.     $self->disk_usage($e);
  951.     return if $CPAN::Signal;
  952.     }
  953.     $self->tidyup;
  954. }
  955.  
  956. package CPAN::Debug;
  957.  
  958. #-> sub CPAN::Debug::debug ;
  959. sub debug {
  960.     my($self,$arg) = @_;
  961.     my($caller,$func,$line,@rest) = caller(1); # caller(0) eg
  962.                                                # Complete, caller(1)
  963.                                                # eg readline
  964.     ($caller) = caller(0);
  965.     $caller =~ s/.*:://;
  966.     $arg = "" unless defined $arg;
  967.     my $rest = join "|", map { defined $_ ? $_ : "UNDEF" } @rest;
  968.     if ($CPAN::DEBUG{$caller} & $CPAN::DEBUG){
  969.     if ($arg and ref $arg) {
  970.         eval { require Data::Dumper };
  971.         if ($@) {
  972.         $CPAN::Frontend->myprint($arg->as_string);
  973.         } else {
  974.         $CPAN::Frontend->myprint(Data::Dumper::Dumper($arg));
  975.         }
  976.     } else {
  977.         $CPAN::Frontend->myprint("Debug($caller:$func,$line,[$rest]): $arg\n");
  978.     }
  979.     }
  980. }
  981.  
  982. package CPAN::Config;
  983.  
  984. #-> sub CPAN::Config::edit ;
  985. # returns true on successful action
  986. sub edit {
  987.     my($self,@args) = @_;
  988.     return unless @args;
  989.     CPAN->debug("self[$self]args[".join(" | ",@args)."]");
  990.     my($o,$str,$func,$args,$key_exists);
  991.     $o = shift @args;
  992.     if($can{$o}) {
  993.     $self->$o(@args);
  994.     return 1;
  995.     } else {
  996.         CPAN->debug("o[$o]") if $CPAN::DEBUG;
  997.     if ($o =~ /list$/) {
  998.         $func = shift @args;
  999.         $func ||= "";
  1000.             CPAN->debug("func[$func]") if $CPAN::DEBUG;
  1001.             my $changed;
  1002.         # Let's avoid eval, it's easier to comprehend without.
  1003.         if ($func eq "push") {
  1004.         push @{$CPAN::Config->{$o}}, @args;
  1005.                 $changed = 1;
  1006.         } elsif ($func eq "pop") {
  1007.         pop @{$CPAN::Config->{$o}};
  1008.                 $changed = 1;
  1009.         } elsif ($func eq "shift") {
  1010.         shift @{$CPAN::Config->{$o}};
  1011.                 $changed = 1;
  1012.         } elsif ($func eq "unshift") {
  1013.         unshift @{$CPAN::Config->{$o}}, @args;
  1014.                 $changed = 1;
  1015.         } elsif ($func eq "splice") {
  1016.         splice @{$CPAN::Config->{$o}}, @args;
  1017.                 $changed = 1;
  1018.         } elsif (@args) {
  1019.         $CPAN::Config->{$o} = [@args];
  1020.                 $changed = 1;
  1021.         } else {
  1022.                 $self->prettyprint($o);
  1023.         }
  1024.             if ($o eq "urllist" && $changed) {
  1025.                 # reset the cached values
  1026.                 undef $CPAN::FTP::Thesite;
  1027.                 undef $CPAN::FTP::Themethod;
  1028.             }
  1029.             return $changed;
  1030.     } else {
  1031.         $CPAN::Config->{$o} = $args[0] if defined $args[0];
  1032.         $self->prettyprint($o);
  1033.     }
  1034.     }
  1035. }
  1036.  
  1037. sub prettyprint {
  1038.   my($self,$k) = @_;
  1039.   my $v = $CPAN::Config->{$k};
  1040.   if (ref $v) {
  1041.     my(@report) = ref $v eq "ARRAY" ?
  1042.         @$v :
  1043.             map { sprintf("   %-18s => %s\n",
  1044.                           $_,
  1045.                           defined $v->{$_} ? $v->{$_} : "UNDEFINED"
  1046.                          )} keys %$v;
  1047.     $CPAN::Frontend->myprint(
  1048.                              join(
  1049.                                   "",
  1050.                                   sprintf(
  1051.                                           "    %-18s\n",
  1052.                                           $k
  1053.                                          ),
  1054.                                   map {"\t$_\n"} @report
  1055.                                  )
  1056.                             );
  1057.   } elsif (defined $v) {
  1058.     $CPAN::Frontend->myprint(sprintf "    %-18s %s\n", $k, $v);
  1059.   } else {
  1060.     $CPAN::Frontend->myprint(sprintf "    %-18s %s\n", $k, "UNDEFINED");
  1061.   }
  1062. }
  1063.  
  1064. #-> sub CPAN::Config::commit ;
  1065. sub commit {
  1066.     my($self,$configpm) = @_;
  1067.     unless (defined $configpm){
  1068.     $configpm ||= $INC{"CPAN/MyConfig.pm"};
  1069.     $configpm ||= $INC{"CPAN/Config.pm"};
  1070.     $configpm || Carp::confess(q{
  1071. CPAN::Config::commit called without an argument.
  1072. Please specify a filename where to save the configuration or try
  1073. "o conf init" to have an interactive course through configing.
  1074. });
  1075.     }
  1076.     my($mode);
  1077.     if (-f $configpm) {
  1078.     $mode = (stat $configpm)[2];
  1079.     if ($mode && ! -w _) {
  1080.         Carp::confess("$configpm is not writable");
  1081.     }
  1082.     }
  1083.  
  1084.     my $msg;
  1085.     $msg = <<EOF unless $configpm =~ /MyConfig/;
  1086.  
  1087. # This is CPAN.pm's systemwide configuration file. This file provides
  1088. # defaults for users, and the values can be changed in a per-user
  1089. # configuration file. The user-config file is being looked for as
  1090. # ~/.cpan/CPAN/MyConfig.pm.
  1091.  
  1092. EOF
  1093.     $msg ||= "\n";
  1094.     my($fh) = FileHandle->new;
  1095.     rename $configpm, "$configpm~" if -f $configpm;
  1096.     open $fh, ">$configpm" or
  1097.         $CPAN::Frontend->mydie("Couldn't open >$configpm: $!");
  1098.     $fh->print(qq[$msg\$CPAN::Config = \{\n]);
  1099.     foreach (sort keys %$CPAN::Config) {
  1100.     $fh->print(
  1101.            "  '$_' => ",
  1102.            ExtUtils::MakeMaker::neatvalue($CPAN::Config->{$_}),
  1103.            ",\n"
  1104.           );
  1105.     }
  1106.  
  1107.     $fh->print("};\n1;\n__END__\n");
  1108.     close $fh;
  1109.  
  1110.     #$mode = 0444 | ( $mode & 0111 ? 0111 : 0 );
  1111.     #chmod $mode, $configpm;
  1112. ###why was that so?    $self->defaults;
  1113.     $CPAN::Frontend->myprint("commit: wrote $configpm\n");
  1114.     1;
  1115. }
  1116.  
  1117. *default = \&defaults;
  1118. #-> sub CPAN::Config::defaults ;
  1119. sub defaults {
  1120.     my($self) = @_;
  1121.     $self->unload;
  1122.     $self->load;
  1123.     1;
  1124. }
  1125.  
  1126. sub init {
  1127.     my($self) = @_;
  1128.     undef $CPAN::Config->{'inhibit_startup_message'}; # lazy trick to
  1129.                                                       # have the least
  1130.                                                       # important
  1131.                                                       # variable
  1132.                                                       # undefined
  1133.     $self->load;
  1134.     1;
  1135. }
  1136.  
  1137. # This is a piece of repeated code that is abstracted here for
  1138. # maintainability.  RMB
  1139. #
  1140. sub _configpmtest {
  1141.     my($configpmdir, $configpmtest) = @_; 
  1142.     if (-w $configpmtest) {
  1143.         return $configpmtest;
  1144.     } elsif (-w $configpmdir) {
  1145.         #_#_# following code dumped core on me with 5.003_11, a.k.
  1146.         my $configpm_bak = "$configpmtest.bak";
  1147.         unlink $configpm_bak if -f $configpm_bak;
  1148.         if( -f $configpmtest ) {    
  1149.             if( rename $configpmtest, $configpm_bak ) {  
  1150.                 $CPAN::Frontend->mywarn(<<END)
  1151. Old configuration file $configpmtest
  1152.     moved to $configpm_bak
  1153. END
  1154.         }
  1155.     }    
  1156.     my $fh = FileHandle->new;
  1157.     if ($fh->open(">$configpmtest")) {
  1158.         $fh->print("1;\n");
  1159.         return $configpmtest;
  1160.     } else {
  1161.         # Should never happen
  1162.         Carp::confess("Cannot open >$configpmtest");
  1163.     }
  1164.     } else { return } 
  1165. }
  1166.  
  1167. #-> sub CPAN::Config::load ;
  1168. sub load {
  1169.     my($self) = shift;
  1170.     my(@miss);
  1171.     use Carp;
  1172.     eval {require CPAN::Config;};       # We eval because of some
  1173.                                         # MakeMaker problems
  1174.     unless ($dot_cpan++){
  1175.       unshift @INC, File::Spec->catdir($ENV{HOME},".cpan");
  1176.       eval {require CPAN::MyConfig;};   # where you can override
  1177.                                         # system wide settings
  1178.       shift @INC;
  1179.     }
  1180.     return unless @miss = $self->missing_config_data;
  1181.  
  1182.     require CPAN::FirstTime;
  1183.     my($configpm,$fh,$redo,$theycalled);
  1184.     $redo ||= "";
  1185.     $theycalled++ if @miss==1 && $miss[0] eq 'inhibit_startup_message';
  1186.     if (defined $INC{"CPAN/Config.pm"} && -w $INC{"CPAN/Config.pm"}) {
  1187.     $configpm = $INC{"CPAN/Config.pm"};
  1188.     $redo++;
  1189.     } elsif (defined $INC{"CPAN/MyConfig.pm"} && -w $INC{"CPAN/MyConfig.pm"}) {
  1190.     $configpm = $INC{"CPAN/MyConfig.pm"};
  1191.     $redo++;
  1192.     } else {
  1193.     my($path_to_cpan) = File::Basename::dirname($INC{"CPAN.pm"});
  1194.     my($configpmdir) = File::Spec->catdir($path_to_cpan,"CPAN");
  1195.     my($configpmtest) = File::Spec->catfile($configpmdir,"Config.pm");
  1196.     if (-d $configpmdir or File::Path::mkpath($configpmdir)) {
  1197.         $configpm = _configpmtest($configpmdir,$configpmtest); 
  1198.     }
  1199.     unless ($configpm) {
  1200.         $configpmdir = File::Spec->catdir($ENV{HOME},".cpan","CPAN");
  1201.         File::Path::mkpath($configpmdir);
  1202.         $configpmtest = File::Spec->catfile($configpmdir,"MyConfig.pm");
  1203.         $configpm = _configpmtest($configpmdir,$configpmtest); 
  1204.         unless ($configpm) {
  1205.         Carp::confess(qq{WARNING: CPAN.pm is unable to }.
  1206.                   qq{create a configuration file.});
  1207.         }
  1208.     }
  1209.     }
  1210.     local($") = ", ";
  1211.     $CPAN::Frontend->myprint(<<END) if $redo && ! $theycalled;
  1212. We have to reconfigure CPAN.pm due to following uninitialized parameters:
  1213.  
  1214. @miss
  1215. END
  1216.     $CPAN::Frontend->myprint(qq{
  1217. $configpm initialized.
  1218. });
  1219.     sleep 2;
  1220.     CPAN::FirstTime::init($configpm);
  1221. }
  1222.  
  1223. #-> sub CPAN::Config::missing_config_data ;
  1224. sub missing_config_data {
  1225.     my(@miss);
  1226.     for (
  1227.          "cpan_home", "keep_source_where", "build_dir", "build_cache",
  1228.          "scan_cache", "index_expire", "gzip", "tar", "unzip", "make",
  1229.          "pager",
  1230.          "makepl_arg", "make_arg", "make_install_arg", "urllist",
  1231.          "inhibit_startup_message", "ftp_proxy", "http_proxy", "no_proxy",
  1232.          "prerequisites_policy",
  1233.          "cache_metadata",
  1234.         ) {
  1235.     push @miss, $_ unless defined $CPAN::Config->{$_};
  1236.     }
  1237.     return @miss;
  1238. }
  1239.  
  1240. #-> sub CPAN::Config::unload ;
  1241. sub unload {
  1242.     delete $INC{'CPAN/MyConfig.pm'};
  1243.     delete $INC{'CPAN/Config.pm'};
  1244. }
  1245.  
  1246. #-> sub CPAN::Config::help ;
  1247. sub help {
  1248.     $CPAN::Frontend->myprint(q[
  1249. Known options:
  1250.   defaults  reload default config values from disk
  1251.   commit    commit session changes to disk
  1252.   init      go through a dialog to set all parameters
  1253.  
  1254. You may edit key values in the follow fashion (the "o" is a literal
  1255. letter o):
  1256.  
  1257.   o conf build_cache 15
  1258.  
  1259.   o conf build_dir "/foo/bar"
  1260.  
  1261.   o conf urllist shift
  1262.  
  1263.   o conf urllist unshift ftp://ftp.foo.bar/
  1264.  
  1265. ]);
  1266.     undef; #don't reprint CPAN::Config
  1267. }
  1268.  
  1269. #-> sub CPAN::Config::cpl ;
  1270. sub cpl {
  1271.     my($word,$line,$pos) = @_;
  1272.     $word ||= "";
  1273.     CPAN->debug("word[$word] line[$line] pos[$pos]") if $CPAN::DEBUG;
  1274.     my(@words) = split " ", substr($line,0,$pos+1);
  1275.     if (
  1276.     defined($words[2])
  1277.     and
  1278.     (
  1279.      $words[2] =~ /list$/ && @words == 3
  1280.      ||
  1281.      $words[2] =~ /list$/ && @words == 4 && length($word)
  1282.     )
  1283.        ) {
  1284.     return grep /^\Q$word\E/, qw(splice shift unshift pop push);
  1285.     } elsif (@words >= 4) {
  1286.     return ();
  1287.     }
  1288.     my(@o_conf) = (keys %CPAN::Config::can, keys %$CPAN::Config);
  1289.     return grep /^\Q$word\E/, @o_conf;
  1290. }
  1291.  
  1292. package CPAN::Shell;
  1293.  
  1294. #-> sub CPAN::Shell::h ;
  1295. sub h {
  1296.     my($class,$about) = @_;
  1297.     if (defined $about) {
  1298.     $CPAN::Frontend->myprint("Detailed help not yet implemented\n");
  1299.     } else {
  1300.     $CPAN::Frontend->myprint(q{
  1301. Display Information
  1302.  command  argument          description
  1303.  a,b,d,m  WORD or /REGEXP/  about authors, bundles, distributions, modules
  1304.  i        WORD or /REGEXP/  about anything of above
  1305.  r        NONE              reinstall recommendations
  1306.  ls       AUTHOR            about files in the author's directory
  1307.  
  1308. Download, Test, Make, Install...
  1309.  get                        download
  1310.  make                       make (implies get)
  1311.  test      MODULES,         make test (implies make)
  1312.  install   DISTS, BUNDLES   make install (implies test)
  1313.  clean                      make clean
  1314.  look                       open subshell in these dists' directories
  1315.  readme                     display these dists' README files
  1316.  
  1317. Other
  1318.  h,?           display this menu       ! perl-code   eval a perl command
  1319.  o conf [opt]  set and query options   q             quit the cpan shell
  1320.  reload cpan   load CPAN.pm again      reload index  load newer indices
  1321.  autobundle    Snapshot                force cmd     unconditionally do cmd});
  1322.     }
  1323. }
  1324.  
  1325. *help = \&h;
  1326.  
  1327. #-> sub CPAN::Shell::a ;
  1328. sub a {
  1329.   my($self,@arg) = @_;
  1330.   # authors are always UPPERCASE
  1331.   for (@arg) {
  1332.     $_ = uc $_ unless /=/;
  1333.   }
  1334.   $CPAN::Frontend->myprint($self->format_result('Author',@arg));
  1335. }
  1336.  
  1337. #-> sub CPAN::Shell::ls ;
  1338. sub ls      {
  1339.     my($self,@arg) = @_;
  1340.     my @accept;
  1341.     for (@arg) {
  1342.         unless (/^[A-Z\-]+$/i) {
  1343.             $CPAN::Frontend->mywarn("ls command rejects argument $_: not an author");
  1344.             next;
  1345.         }
  1346.         push @accept, uc $_;
  1347.     }
  1348.     for my $a (@accept){
  1349.         my $author = $self->expand('Author',$a) or die "No author found for $a";
  1350.         $author->ls;
  1351.     }
  1352. }
  1353.  
  1354. #-> sub CPAN::Shell::local_bundles ;
  1355. sub local_bundles {
  1356.     my($self,@which) = @_;
  1357.     my($incdir,$bdir,$dh);
  1358.     foreach $incdir ($CPAN::Config->{'cpan_home'},@INC) {
  1359.         my @bbase = "Bundle";
  1360.         while (my $bbase = shift @bbase) {
  1361.             $bdir = File::Spec->catdir($incdir,split /::/, $bbase);
  1362.             CPAN->debug("bdir[$bdir]\@bbase[@bbase]") if $CPAN::DEBUG;
  1363.             if ($dh = DirHandle->new($bdir)) { # may fail
  1364.                 my($entry);
  1365.                 for $entry ($dh->read) {
  1366.                     next if $entry =~ /^\./;
  1367.                     if (-d File::Spec->catdir($bdir,$entry)){
  1368.                         push @bbase, "$bbase\::$entry";
  1369.                     } else {
  1370.                         next unless $entry =~ s/\.pm(?!\n)\Z//;
  1371.                         $CPAN::META->instance('CPAN::Bundle',"$bbase\::$entry");
  1372.                     }
  1373.                 }
  1374.             }
  1375.         }
  1376.     }
  1377. }
  1378.  
  1379. #-> sub CPAN::Shell::b ;
  1380. sub b {
  1381.     my($self,@which) = @_;
  1382.     CPAN->debug("which[@which]") if $CPAN::DEBUG;
  1383.     $self->local_bundles;
  1384.     $CPAN::Frontend->myprint($self->format_result('Bundle',@which));
  1385. }
  1386.  
  1387. #-> sub CPAN::Shell::d ;
  1388. sub d { $CPAN::Frontend->myprint(shift->format_result('Distribution',@_));}
  1389.  
  1390. #-> sub CPAN::Shell::m ;
  1391. sub m { # emacs confused here }; sub mimimimimi { # emacs in sync here
  1392.     $CPAN::Frontend->myprint(shift->format_result('Module',@_));
  1393. }
  1394.  
  1395. #-> sub CPAN::Shell::i ;
  1396. sub i {
  1397.     my($self) = shift;
  1398.     my(@args) = @_;
  1399.     my(@type,$type,@m);
  1400.     @type = qw/Author Bundle Distribution Module/;
  1401.     @args = '/./' unless @args;
  1402.     my(@result);
  1403.     for $type (@type) {
  1404.     push @result, $self->expand($type,@args);
  1405.     }
  1406.     my $result = @result == 1 ?
  1407.     $result[0]->as_string :
  1408.             @result == 0 ?
  1409.                 "No objects found of any type for argument @args\n" :
  1410.                     join("",
  1411.                          (map {$_->as_glimpse} @result),
  1412.                          scalar @result, " items found\n",
  1413.                         );
  1414.     $CPAN::Frontend->myprint($result);
  1415. }
  1416.  
  1417. #-> sub CPAN::Shell::o ;
  1418.  
  1419. # CPAN::Shell::o and CPAN::Config::edit are closely related. 'o conf'
  1420. # should have been called set and 'o debug' maybe 'set debug'
  1421. sub o {
  1422.     my($self,$o_type,@o_what) = @_;
  1423.     $o_type ||= "";
  1424.     CPAN->debug("o_type[$o_type] o_what[".join(" | ",@o_what)."]\n");
  1425.     if ($o_type eq 'conf') {
  1426.     shift @o_what if @o_what && $o_what[0] eq 'help';
  1427.     if (!@o_what) { # print all things, "o conf"
  1428.         my($k,$v);
  1429.         $CPAN::Frontend->myprint("CPAN::Config options");
  1430.         if (exists $INC{'CPAN/Config.pm'}) {
  1431.           $CPAN::Frontend->myprint(" from $INC{'CPAN/Config.pm'}");
  1432.         }
  1433.         if (exists $INC{'CPAN/MyConfig.pm'}) {
  1434.           $CPAN::Frontend->myprint(" and $INC{'CPAN/MyConfig.pm'}");
  1435.         }
  1436.         $CPAN::Frontend->myprint(":\n");
  1437.         for $k (sort keys %CPAN::Config::can) {
  1438.         $v = $CPAN::Config::can{$k};
  1439.         $CPAN::Frontend->myprint(sprintf "    %-18s %s\n", $k, $v);
  1440.         }
  1441.         $CPAN::Frontend->myprint("\n");
  1442.         for $k (sort keys %$CPAN::Config) {
  1443.                 CPAN::Config->prettyprint($k);
  1444.         }
  1445.         $CPAN::Frontend->myprint("\n");
  1446.     } elsif (!CPAN::Config->edit(@o_what)) {
  1447.         $CPAN::Frontend->myprint(qq{Type 'o conf' to view configuration }.
  1448.                                      qq{edit options\n\n});
  1449.     }
  1450.     } elsif ($o_type eq 'debug') {
  1451.     my(%valid);
  1452.     @o_what = () if defined $o_what[0] && $o_what[0] =~ /help/i;
  1453.     if (@o_what) {
  1454.         while (@o_what) {
  1455.         my($what) = shift @o_what;
  1456.                 if ($what =~ s/^-// && exists $CPAN::DEBUG{$what}) {
  1457.                     $CPAN::DEBUG &= $CPAN::DEBUG ^ $CPAN::DEBUG{$what};
  1458.                     next;
  1459.                 }
  1460.         if ( exists $CPAN::DEBUG{$what} ) {
  1461.             $CPAN::DEBUG |= $CPAN::DEBUG{$what};
  1462.         } elsif ($what =~ /^\d/) {
  1463.             $CPAN::DEBUG = $what;
  1464.         } elsif (lc $what eq 'all') {
  1465.             my($max) = 0;
  1466.             for (values %CPAN::DEBUG) {
  1467.             $max += $_;
  1468.             }
  1469.             $CPAN::DEBUG = $max;
  1470.         } else {
  1471.             my($known) = 0;
  1472.             for (keys %CPAN::DEBUG) {
  1473.             next unless lc($_) eq lc($what);
  1474.             $CPAN::DEBUG |= $CPAN::DEBUG{$_};
  1475.             $known = 1;
  1476.             }
  1477.             $CPAN::Frontend->myprint("unknown argument [$what]\n")
  1478.             unless $known;
  1479.         }
  1480.         }
  1481.     } else {
  1482.       my $raw = "Valid options for debug are ".
  1483.           join(", ",sort(keys %CPAN::DEBUG), 'all').
  1484.           qq{ or a number. Completion works on the options. }.
  1485.               qq{Case is ignored.};
  1486.       require Text::Wrap;
  1487.       $CPAN::Frontend->myprint(Text::Wrap::fill("","",$raw));
  1488.       $CPAN::Frontend->myprint("\n\n");
  1489.     }
  1490.     if ($CPAN::DEBUG) {
  1491.         $CPAN::Frontend->myprint("Options set for debugging:\n");
  1492.         my($k,$v);
  1493.         for $k (sort {$CPAN::DEBUG{$a} <=> $CPAN::DEBUG{$b}} keys %CPAN::DEBUG) {
  1494.         $v = $CPAN::DEBUG{$k};
  1495.         $CPAN::Frontend->myprint(sprintf "    %-14s(%s)\n", $k, $v)
  1496.                     if $v & $CPAN::DEBUG;
  1497.         }
  1498.     } else {
  1499.         $CPAN::Frontend->myprint("Debugging turned off completely.\n");
  1500.     }
  1501.     } else {
  1502.     $CPAN::Frontend->myprint(qq{
  1503. Known options:
  1504.   conf    set or get configuration variables
  1505.   debug   set or get debugging options
  1506. });
  1507.     }
  1508. }
  1509.  
  1510. sub paintdots_onreload {
  1511.     my($ref) = shift;
  1512.     sub {
  1513.     if ( $_[0] =~ /[Ss]ubroutine (\w+) redefined/ ) {
  1514.         my($subr) = $1;
  1515.         ++$$ref;
  1516.         local($|) = 1;
  1517.         # $CPAN::Frontend->myprint(".($subr)");
  1518.         $CPAN::Frontend->myprint(".");
  1519.         return;
  1520.     }
  1521.     warn @_;
  1522.     };
  1523. }
  1524.  
  1525. #-> sub CPAN::Shell::reload ;
  1526. sub reload {
  1527.     my($self,$command,@arg) = @_;
  1528.     $command ||= "";
  1529.     $self->debug("self[$self]command[$command]arg[@arg]") if $CPAN::DEBUG;
  1530.     if ($command =~ /cpan/i) {
  1531.     CPAN->debug("reloading the whole CPAN.pm") if $CPAN::DEBUG;
  1532.     my $fh = FileHandle->new($INC{'CPAN.pm'});
  1533.     local($/);
  1534.     my $redef = 0;
  1535.     local($SIG{__WARN__}) = paintdots_onreload(\$redef);
  1536.     eval <$fh>;
  1537.     warn $@ if $@;
  1538.     $CPAN::Frontend->myprint("\n$redef subroutines redefined\n");
  1539.     } elsif ($command =~ /index/) {
  1540.       CPAN::Index->force_reload;
  1541.     } else {
  1542.       $CPAN::Frontend->myprint(qq{cpan     re-evals the CPAN.pm file
  1543. index    re-reads the index files\n});
  1544.     }
  1545. }
  1546.  
  1547. #-> sub CPAN::Shell::_binary_extensions ;
  1548. sub _binary_extensions {
  1549.     my($self) = shift @_;
  1550.     my(@result,$module,%seen,%need,$headerdone);
  1551.     for $module ($self->expand('Module','/./')) {
  1552.     my $file  = $module->cpan_file;
  1553.     next if $file eq "N/A";
  1554.     next if $file =~ /^Contact Author/;
  1555.         my $dist = $CPAN::META->instance('CPAN::Distribution',$file);
  1556.     next if $dist->isa_perl;
  1557.     next unless $module->xs_file;
  1558.     local($|) = 1;
  1559.     $CPAN::Frontend->myprint(".");
  1560.     push @result, $module;
  1561.     }
  1562. #    print join " | ", @result;
  1563.     $CPAN::Frontend->myprint("\n");
  1564.     return @result;
  1565. }
  1566.  
  1567. #-> sub CPAN::Shell::recompile ;
  1568. sub recompile {
  1569.     my($self) = shift @_;
  1570.     my($module,@module,$cpan_file,%dist);
  1571.     @module = $self->_binary_extensions();
  1572.     for $module (@module){  # we force now and compile later, so we
  1573.                             # don't do it twice
  1574.     $cpan_file = $module->cpan_file;
  1575.     my $pack = $CPAN::META->instance('CPAN::Distribution',$cpan_file);
  1576.     $pack->force;
  1577.     $dist{$cpan_file}++;
  1578.     }
  1579.     for $cpan_file (sort keys %dist) {
  1580.     $CPAN::Frontend->myprint("  CPAN: Recompiling $cpan_file\n\n");
  1581.     my $pack = $CPAN::META->instance('CPAN::Distribution',$cpan_file);
  1582.     $pack->install;
  1583.     $CPAN::Signal = 0; # it's tempting to reset Signal, so we can
  1584.                            # stop a package from recompiling,
  1585.                            # e.g. IO-1.12 when we have perl5.003_10
  1586.     }
  1587. }
  1588.  
  1589. #-> sub CPAN::Shell::_u_r_common ;
  1590. sub _u_r_common {
  1591.     my($self) = shift @_;
  1592.     my($what) = shift @_;
  1593.     CPAN->debug("self[$self] what[$what] args[@_]") if $CPAN::DEBUG;
  1594.     Carp::croak "Usage: \$obj->_u_r_common(a|r|u)" unless
  1595.           $what && $what =~ /^[aru]$/;
  1596.     my(@args) = @_;
  1597.     @args = '/./' unless @args;
  1598.     my(@result,$module,%seen,%need,$headerdone,
  1599.        $version_undefs,$version_zeroes);
  1600.     $version_undefs = $version_zeroes = 0;
  1601.     my $sprintf = "%s%-25s%s %9s %9s  %s\n";
  1602.     my @expand = $self->expand('Module',@args);
  1603.     my $expand = scalar @expand;
  1604.     if (0) { # Looks like noise to me, was very useful for debugging
  1605.              # for metadata cache
  1606.         $CPAN::Frontend->myprint(sprintf "%d matches in the database\n", $expand);
  1607.     }
  1608.     for $module (@expand) {
  1609.     my $file  = $module->cpan_file;
  1610.     next unless defined $file; # ??
  1611.     my($latest) = $module->cpan_version;
  1612.     my($inst_file) = $module->inst_file;
  1613.     my($have);
  1614.     return if $CPAN::Signal;
  1615.     if ($inst_file){
  1616.         if ($what eq "a") {
  1617.         $have = $module->inst_version;
  1618.         } elsif ($what eq "r") {
  1619.         $have = $module->inst_version;
  1620.         local($^W) = 0;
  1621.         if ($have eq "undef"){
  1622.             $version_undefs++;
  1623.         } elsif ($have == 0){
  1624.             $version_zeroes++;
  1625.         }
  1626.         next unless CPAN::Version->vgt($latest, $have);
  1627. # to be pedantic we should probably say:
  1628. #    && !($have eq "undef" && $latest ne "undef" && $latest gt "");
  1629. # to catch the case where CPAN has a version 0 and we have a version undef
  1630.         } elsif ($what eq "u") {
  1631.         next;
  1632.         }
  1633.     } else {
  1634.         if ($what eq "a") {
  1635.         next;
  1636.         } elsif ($what eq "r") {
  1637.         next;
  1638.         } elsif ($what eq "u") {
  1639.         $have = "-";
  1640.         }
  1641.     }
  1642.     return if $CPAN::Signal; # this is sometimes lengthy
  1643.     $seen{$file} ||= 0;
  1644.     if ($what eq "a") {
  1645.         push @result, sprintf "%s %s\n", $module->id, $have;
  1646.     } elsif ($what eq "r") {
  1647.         push @result, $module->id;
  1648.         next if $seen{$file}++;
  1649.     } elsif ($what eq "u") {
  1650.         push @result, $module->id;
  1651.         next if $seen{$file}++;
  1652.         next if $file =~ /^Contact/;
  1653.     }
  1654.     unless ($headerdone++){
  1655.         $CPAN::Frontend->myprint("\n");
  1656.         $CPAN::Frontend->myprint(sprintf(
  1657.                                              $sprintf,
  1658.                                              "",
  1659.                                              "Package namespace",
  1660.                                              "",
  1661.                                              "installed",
  1662.                                              "latest",
  1663.                                              "in CPAN file"
  1664.                                             ));
  1665.     }
  1666.         my $color_on = "";
  1667.         my $color_off = "";
  1668.         if (
  1669.             $COLOR_REGISTERED
  1670.             &&
  1671.             $CPAN::META->has_inst("Term::ANSIColor")
  1672.             &&
  1673.             $module->{RO}{description}
  1674.            ) {
  1675.             $color_on = Term::ANSIColor::color("green");
  1676.             $color_off = Term::ANSIColor::color("reset");
  1677.         }
  1678.     $CPAN::Frontend->myprint(sprintf $sprintf,
  1679.                                  $color_on,
  1680.                                  $module->id,
  1681.                                  $color_off,
  1682.                                  $have,
  1683.                                  $latest,
  1684.                                  $file);
  1685.     $need{$module->id}++;
  1686.     }
  1687.     unless (%need) {
  1688.     if ($what eq "u") {
  1689.         $CPAN::Frontend->myprint("No modules found for @args\n");
  1690.     } elsif ($what eq "r") {
  1691.         $CPAN::Frontend->myprint("All modules are up to date for @args\n");
  1692.     }
  1693.     }
  1694.     if ($what eq "r") {
  1695.     if ($version_zeroes) {
  1696.         my $s_has = $version_zeroes > 1 ? "s have" : " has";
  1697.         $CPAN::Frontend->myprint(qq{$version_zeroes installed module$s_has }.
  1698.         qq{a version number of 0\n});
  1699.     }
  1700.     if ($version_undefs) {
  1701.         my $s_has = $version_undefs > 1 ? "s have" : " has";
  1702.         $CPAN::Frontend->myprint(qq{$version_undefs installed module$s_has no }.
  1703.         qq{parseable version number\n});
  1704.     }
  1705.     }
  1706.     @result;
  1707. }
  1708.  
  1709. #-> sub CPAN::Shell::r ;
  1710. sub r {
  1711.     shift->_u_r_common("r",@_);
  1712. }
  1713.  
  1714. #-> sub CPAN::Shell::u ;
  1715. sub u {
  1716.     shift->_u_r_common("u",@_);
  1717. }
  1718.  
  1719. #-> sub CPAN::Shell::autobundle ;
  1720. sub autobundle {
  1721.     my($self) = shift;
  1722.     CPAN::Config->load unless $CPAN::Config_loaded++;
  1723.     my(@bundle) = $self->_u_r_common("a",@_);
  1724.     my($todir) = File::Spec->catdir($CPAN::Config->{'cpan_home'},"Bundle");
  1725.     File::Path::mkpath($todir);
  1726.     unless (-d $todir) {
  1727.     $CPAN::Frontend->myprint("Couldn't mkdir $todir for some reason\n");
  1728.     return;
  1729.     }
  1730.     my($y,$m,$d) =  (localtime)[5,4,3];
  1731.     $y+=1900;
  1732.     $m++;
  1733.     my($c) = 0;
  1734.     my($me) = sprintf "Snapshot_%04d_%02d_%02d_%02d", $y, $m, $d, $c;
  1735.     my($to) = File::Spec->catfile($todir,"$me.pm");
  1736.     while (-f $to) {
  1737.     $me = sprintf "Snapshot_%04d_%02d_%02d_%02d", $y, $m, $d, ++$c;
  1738.     $to = File::Spec->catfile($todir,"$me.pm");
  1739.     }
  1740.     my($fh) = FileHandle->new(">$to") or Carp::croak "Can't open >$to: $!";
  1741.     $fh->print(
  1742.            "package Bundle::$me;\n\n",
  1743.            "\$VERSION = '0.01';\n\n",
  1744.            "1;\n\n",
  1745.            "__END__\n\n",
  1746.            "=head1 NAME\n\n",
  1747.            "Bundle::$me - Snapshot of installation on ",
  1748.            $Config::Config{'myhostname'},
  1749.            " on ",
  1750.            scalar(localtime),
  1751.            "\n\n=head1 SYNOPSIS\n\n",
  1752.            "perl -MCPAN -e 'install Bundle::$me'\n\n",
  1753.            "=head1 CONTENTS\n\n",
  1754.            join("\n", @bundle),
  1755.            "\n\n=head1 CONFIGURATION\n\n",
  1756.            Config->myconfig,
  1757.            "\n\n=head1 AUTHOR\n\n",
  1758.            "This Bundle has been generated automatically ",
  1759.            "by the autobundle routine in CPAN.pm.\n",
  1760.           );
  1761.     $fh->close;
  1762.     $CPAN::Frontend->myprint("\nWrote bundle file
  1763.     $to\n\n");
  1764. }
  1765.  
  1766. #-> sub CPAN::Shell::expandany ;
  1767. sub expandany {
  1768.     my($self,$s) = @_;
  1769.     CPAN->debug("s[$s]") if $CPAN::DEBUG;
  1770.     if ($s =~ m|/|) { # looks like a file
  1771.         $s = CPAN::Distribution->normalize($s);
  1772.         return $CPAN::META->instance('CPAN::Distribution',$s);
  1773.         # Distributions spring into existence, not expand
  1774.     } elsif ($s =~ m|^Bundle::|) {
  1775.         $self->local_bundles; # scanning so late for bundles seems
  1776.                               # both attractive and crumpy: always
  1777.                               # current state but easy to forget
  1778.                               # somewhere
  1779.         return $self->expand('Bundle',$s);
  1780.     } else {
  1781.         return $self->expand('Module',$s)
  1782.             if $CPAN::META->exists('CPAN::Module',$s);
  1783.     }
  1784.     return;
  1785. }
  1786.  
  1787. #-> sub CPAN::Shell::expand ;
  1788. sub expand {
  1789.     shift;
  1790.     my($type,@args) = @_;
  1791.     my($arg,@m);
  1792.     CPAN->debug("type[$type]args[@args]") if $CPAN::DEBUG;
  1793.     for $arg (@args) {
  1794.     my($regex,$command);
  1795.     if ($arg =~ m|^/(.*)/$|) {
  1796.         $regex = $1;
  1797.     } elsif ($arg =~ m/=/) {
  1798.             $command = 1;
  1799.         }
  1800.     my $class = "CPAN::$type";
  1801.     my $obj;
  1802.         CPAN->debug(sprintf "class[%s]regex[%s]command[%s]",
  1803.                     $class,
  1804.                     defined $regex ? $regex : "UNDEFINED",
  1805.                     $command || "UNDEFINED",
  1806.                    ) if $CPAN::DEBUG;
  1807.     if (defined $regex) {
  1808.             for $obj (
  1809.                       sort
  1810.                       {$a->id cmp $b->id}
  1811.                       $CPAN::META->all_objects($class)
  1812.                      ) {
  1813.                 unless ($obj->id){
  1814.                     # BUG, we got an empty object somewhere
  1815.                     require Data::Dumper;
  1816.                     CPAN->debug(sprintf(
  1817.                                         "Bug in CPAN: Empty id on obj[%s][%s]",
  1818.                                         $obj,
  1819.                                         Data::Dumper::Dumper($obj)
  1820.                                        )) if $CPAN::DEBUG;
  1821.                     next;
  1822.                 }
  1823.                 push @m, $obj
  1824.                     if $obj->id =~ /$regex/i
  1825.                         or
  1826.                             (
  1827.                              (
  1828.                               $] < 5.00303 ### provide sort of
  1829.                               ### compatibility with 5.003
  1830.                               ||
  1831.                               $obj->can('name')
  1832.                              )
  1833.                              &&
  1834.                              $obj->name  =~ /$regex/i
  1835.                             );
  1836.             }
  1837.         } elsif ($command) {
  1838.             die "equal sign in command disabled (immature interface), ".
  1839.                 "you can set
  1840.  ! \$CPAN::Shell::ADVANCED_QUERY=1
  1841. to enable it. But please note, this is HIGHLY EXPERIMENTAL code
  1842. that may go away anytime.\n"
  1843.                     unless $ADVANCED_QUERY;
  1844.             my($method,$criterion) = $arg =~ /(.+?)=(.+)/;
  1845.             my($matchcrit) = $criterion =~ m/^~(.+)/;
  1846.             for my $self (
  1847.                           sort
  1848.                           {$a->id cmp $b->id}
  1849.                           $CPAN::META->all_objects($class)
  1850.                          ) {
  1851.                 my $lhs = $self->$method() or next; # () for 5.00503
  1852.                 if ($matchcrit) {
  1853.                     push @m, $self if $lhs =~ m/$matchcrit/;
  1854.                 } else {
  1855.                     push @m, $self if $lhs eq $criterion;
  1856.                 }
  1857.             }
  1858.     } else {
  1859.         my($xarg) = $arg;
  1860.         if ( $type eq 'Bundle' ) {
  1861.         $xarg =~ s/^(Bundle::)?(.*)/Bundle::$2/;
  1862.         } elsif ($type eq "Distribution") {
  1863.                 $xarg = CPAN::Distribution->normalize($arg);
  1864.             }
  1865.         if ($CPAN::META->exists($class,$xarg)) {
  1866.         $obj = $CPAN::META->instance($class,$xarg);
  1867.         } elsif ($CPAN::META->exists($class,$arg)) {
  1868.         $obj = $CPAN::META->instance($class,$arg);
  1869.         } else {
  1870.         next;
  1871.         }
  1872.         push @m, $obj;
  1873.     }
  1874.     }
  1875.     return wantarray ? @m : $m[0];
  1876. }
  1877.  
  1878. #-> sub CPAN::Shell::format_result ;
  1879. sub format_result {
  1880.     my($self) = shift;
  1881.     my($type,@args) = @_;
  1882.     @args = '/./' unless @args;
  1883.     my(@result) = $self->expand($type,@args);
  1884.     my $result = @result == 1 ?
  1885.     $result[0]->as_string :
  1886.             @result == 0 ?
  1887.                 "No objects of type $type found for argument @args\n" :
  1888.                     join("",
  1889.                          (map {$_->as_glimpse} @result),
  1890.                          scalar @result, " items found\n",
  1891.                         );
  1892.     $result;
  1893. }
  1894.  
  1895. # The only reason for this method is currently to have a reliable
  1896. # debugging utility that reveals which output is going through which
  1897. # channel. No, I don't like the colors ;-)
  1898.  
  1899. #-> sub CPAN::Shell::print_ornameted ;
  1900. sub print_ornamented {
  1901.     my($self,$what,$ornament) = @_;
  1902.     my $longest = 0;
  1903.     return unless defined $what;
  1904.  
  1905.     if ($CPAN::Config->{term_is_latin}){
  1906.         # courtesy jhi:
  1907.         $what
  1908.             =~ s{([\xC0-\xDF])([\x80-\xBF])}{chr(ord($1)<<6&0xC0|ord($2)&0x3F)}eg; #};
  1909.     }
  1910.     if ($PRINT_ORNAMENTING) {
  1911.     unless (defined &color) {
  1912.         if ($CPAN::META->has_inst("Term::ANSIColor")) {
  1913.         import Term::ANSIColor "color";
  1914.         } else {
  1915.         *color = sub { return "" };
  1916.         }
  1917.     }
  1918.     my $line;
  1919.     for $line (split /\n/, $what) {
  1920.         $longest = length($line) if length($line) > $longest;
  1921.     }
  1922.     my $sprintf = "%-" . $longest . "s";
  1923.     while ($what){
  1924.         $what =~ s/(.*\n?)//m;
  1925.         my $line = $1;
  1926.         last unless $line;
  1927.         my($nl) = chomp $line ? "\n" : "";
  1928.         #    print "line[$line]ornament[$ornament]sprintf[$sprintf]\n";
  1929.         print color($ornament), sprintf($sprintf,$line), color("reset"), $nl;
  1930.     }
  1931.     } else {
  1932.     print $what;
  1933.     }
  1934. }
  1935.  
  1936. sub myprint {
  1937.     my($self,$what) = @_;
  1938.  
  1939.     $self->print_ornamented($what, 'bold blue on_yellow');
  1940. }
  1941.  
  1942. sub myexit {
  1943.     my($self,$what) = @_;
  1944.     $self->myprint($what);
  1945.     exit;
  1946. }
  1947.  
  1948. sub mywarn {
  1949.     my($self,$what) = @_;
  1950.     $self->print_ornamented($what, 'bold red on_yellow');
  1951. }
  1952.  
  1953. sub myconfess {
  1954.     my($self,$what) = @_;
  1955.     $self->print_ornamented($what, 'bold red on_white');
  1956.     Carp::confess "died";
  1957. }
  1958.  
  1959. sub mydie {
  1960.     my($self,$what) = @_;
  1961.     $self->print_ornamented($what, 'bold red on_white');
  1962.     die "\n";
  1963. }
  1964.  
  1965. sub setup_output {
  1966.     return if -t STDOUT;
  1967.     my $odef = select STDERR;
  1968.     $| = 1;
  1969.     select STDOUT;
  1970.     $| = 1;
  1971.     select $odef;
  1972. }
  1973.  
  1974. #-> sub CPAN::Shell::rematein ;
  1975. # RE-adme||MA-ke||TE-st||IN-stall
  1976. sub rematein {
  1977.     shift;
  1978.     my($meth,@some) = @_;
  1979.     my $pragma = "";
  1980.     if ($meth eq 'force') {
  1981.     $pragma = $meth;
  1982.     $meth = shift @some;
  1983.     }
  1984.     setup_output();
  1985.     CPAN->debug("pragma[$pragma]meth[$meth] some[@some]") if $CPAN::DEBUG;
  1986.  
  1987.     # Here is the place to set "test_count" on all involved parties to
  1988.     # 0. We then can pass this counter on to the involved
  1989.     # distributions and those can refuse to test if test_count > X. In
  1990.     # the first stab at it we could use a 1 for "X".
  1991.  
  1992.     # But when do I reset the distributions to start with 0 again?
  1993.     # Jost suggested to have a random or cycling interaction ID that
  1994.     # we pass through. But the ID is something that is just left lying
  1995.     # around in addition to the counter, so I'd prefer to set the
  1996.     # counter to 0 now, and repeat at the end of the loop. But what
  1997.     # about dependencies? They appear later and are not reset, they
  1998.     # enter the queue but not its copy. How do they get a sensible
  1999.     # test_count?
  2000.  
  2001.     # construct the queue
  2002.     my($s,@s,@qcopy);
  2003.     foreach $s (@some) {
  2004.     my $obj;
  2005.     if (ref $s) {
  2006.             CPAN->debug("s is an object[$s]") if $CPAN::DEBUG;
  2007.         $obj = $s;
  2008.     } elsif ($s =~ m|^/|) { # looks like a regexp
  2009.             $CPAN::Frontend->mywarn("Sorry, $meth with a regular expression is ".
  2010.                                     "not supported\n");
  2011.             sleep 2;
  2012.             next;
  2013.     } else {
  2014.             CPAN->debug("calling expandany [$s]") if $CPAN::DEBUG;
  2015.         $obj = CPAN::Shell->expandany($s);
  2016.     }
  2017.     if (ref $obj) {
  2018.             $obj->color_cmd_tmps(0,1);
  2019.             CPAN::Queue->new($obj->id);
  2020.             push @qcopy, $obj;
  2021.     } elsif ($CPAN::META->exists('CPAN::Author',$s)) {
  2022.         $obj = $CPAN::META->instance('CPAN::Author',$s);
  2023.             if ($meth eq "dump") {
  2024.                 $obj->dump;
  2025.             } else {
  2026.                 $CPAN::Frontend->myprint(
  2027.                                          join "",
  2028.                                          "Don't be silly, you can't $meth ",
  2029.                                          $obj->fullname,
  2030.                                          " ;-)\n"
  2031.                                         );
  2032.                 sleep 2;
  2033.             }
  2034.     } else {
  2035.         $CPAN::Frontend
  2036.         ->myprint(qq{Warning: Cannot $meth $s, }.
  2037.               qq{don\'t know what it is.
  2038. Try the command
  2039.  
  2040.     i /$s/
  2041.  
  2042. to find objects with matching identifiers.
  2043. });
  2044.             sleep 2;
  2045.     }
  2046.     }
  2047.  
  2048.     # queuerunner (please be warned: when I started to change the
  2049.     # queue to hold objects instead of names, I made one or two
  2050.     # mistakes and never found which. I reverted back instead)
  2051.     while ($s = CPAN::Queue->first) {
  2052.         my $obj;
  2053.     if (ref $s) {
  2054.         $obj = $s; # I do not believe, we would survive if this happened
  2055.     } else {
  2056.         $obj = CPAN::Shell->expandany($s);
  2057.     }
  2058.         if ($pragma
  2059.             &&
  2060.             ($] < 5.00303 || $obj->can($pragma))){
  2061.             ### compatibility with 5.003
  2062.             $obj->$pragma($meth); # the pragma "force" in
  2063.                                   # "CPAN::Distribution" must know
  2064.                                   # what we are intending
  2065.         }
  2066.         if ($]>=5.00303 && $obj->can('called_for')) {
  2067.             $obj->called_for($s);
  2068.         }
  2069.         CPAN->debug(
  2070.                     qq{pragma[$pragma]meth[$meth]obj[$obj]as_string\[}.
  2071.                     $obj->as_string.
  2072.                     qq{\]}
  2073.                    ) if $CPAN::DEBUG;
  2074.  
  2075.         if ($obj->$meth()){
  2076.             CPAN::Queue->delete($s);
  2077.         } else {
  2078.             CPAN->debug("failed");
  2079.         }
  2080.  
  2081.         $obj->undelay;
  2082.     CPAN::Queue->delete_first($s);
  2083.     }
  2084.     for my $obj (@qcopy) {
  2085.         $obj->color_cmd_tmps(0,0);
  2086.     }
  2087. }
  2088.  
  2089. #-> sub CPAN::Shell::dump ;
  2090. sub dump    { shift->rematein('dump',@_); }
  2091. #-> sub CPAN::Shell::force ;
  2092. sub force   { shift->rematein('force',@_); }
  2093. #-> sub CPAN::Shell::get ;
  2094. sub get     { shift->rematein('get',@_); }
  2095. #-> sub CPAN::Shell::readme ;
  2096. sub readme  { shift->rematein('readme',@_); }
  2097. #-> sub CPAN::Shell::make ;
  2098. sub make    { shift->rematein('make',@_); }
  2099. #-> sub CPAN::Shell::test ;
  2100. sub test    { shift->rematein('test',@_); }
  2101. #-> sub CPAN::Shell::install ;
  2102. sub install { shift->rematein('install',@_); }
  2103. #-> sub CPAN::Shell::clean ;
  2104. sub clean   { shift->rematein('clean',@_); }
  2105. #-> sub CPAN::Shell::look ;
  2106. sub look   { shift->rematein('look',@_); }
  2107. #-> sub CPAN::Shell::cvs_import ;
  2108. sub cvs_import   { shift->rematein('cvs_import',@_); }
  2109.  
  2110. package CPAN::LWP::UserAgent;
  2111.  
  2112. sub config {
  2113.     return if $SETUPDONE;
  2114.     if ($CPAN::META->has_usable('LWP::UserAgent')) {
  2115.         require LWP::UserAgent;
  2116.         @ISA = qw(Exporter LWP::UserAgent);
  2117.         $SETUPDONE++;
  2118.     } else {
  2119.         $CPAN::Frontend->mywarn("LWP::UserAgent not available\n");
  2120.     }
  2121. }
  2122.  
  2123. sub get_basic_credentials {
  2124.     my($self, $realm, $uri, $proxy) = @_;
  2125.     return unless $proxy;
  2126.     if ($USER && $PASSWD) {
  2127.     } elsif (defined $CPAN::Config->{proxy_user} &&
  2128.         defined $CPAN::Config->{proxy_pass}) {
  2129.         $USER = $CPAN::Config->{proxy_user};
  2130.         $PASSWD = $CPAN::Config->{proxy_pass};
  2131.     } else {
  2132.         require ExtUtils::MakeMaker;
  2133.         ExtUtils::MakeMaker->import(qw(prompt));
  2134.         $USER = prompt("Proxy authentication needed!
  2135.  (Note: to permanently configure username and password run
  2136.    o conf proxy_user your_username
  2137.    o conf proxy_pass your_password
  2138.  )\nUsername:");
  2139.         if ($CPAN::META->has_inst("Term::ReadKey")) {
  2140.             Term::ReadKey::ReadMode("noecho");
  2141.         } else {
  2142.             $CPAN::Frontend->mywarn("Warning: Term::ReadKey seems not to be available, your password will be echoed to the terminal!\n");
  2143.         }
  2144.         $PASSWD = prompt("Password:");
  2145.         if ($CPAN::META->has_inst("Term::ReadKey")) {
  2146.             Term::ReadKey::ReadMode("restore");
  2147.         }
  2148.         $CPAN::Frontend->myprint("\n\n");
  2149.     }
  2150.     return($USER,$PASSWD);
  2151. }
  2152.  
  2153. sub mirror {
  2154.     my($self,$url,$aslocal) = @_;
  2155.     my $result = $self->SUPER::mirror($url,$aslocal);
  2156.     if ($result->code == 407) {
  2157.         undef $USER;
  2158.         undef $PASSWD;
  2159.         $result = $self->SUPER::mirror($url,$aslocal);
  2160.     }
  2161.     $result;
  2162. }
  2163.  
  2164. package CPAN::FTP;
  2165.  
  2166. #-> sub CPAN::FTP::ftp_get ;
  2167. sub ftp_get {
  2168.   my($class,$host,$dir,$file,$target) = @_;
  2169.   $class->debug(
  2170.         qq[Going to fetch file [$file] from dir [$dir]
  2171.     on host [$host] as local [$target]\n]
  2172.               ) if $CPAN::DEBUG;
  2173.   my $ftp = Net::FTP->new($host);
  2174.   return 0 unless defined $ftp;
  2175.   $ftp->debug(1) if $CPAN::DEBUG{'FTP'} & $CPAN::DEBUG;
  2176.   $class->debug(qq[Going to login("anonymous","$Config::Config{cf_email}")]);
  2177.   unless ( $ftp->login("anonymous",$Config::Config{'cf_email'}) ){
  2178.     warn "Couldn't login on $host";
  2179.     return;
  2180.   }
  2181.   unless ( $ftp->cwd($dir) ){
  2182.     warn "Couldn't cwd $dir";
  2183.     return;
  2184.   }
  2185.   $ftp->binary;
  2186.   $class->debug(qq[Going to ->get("$file","$target")\n]) if $CPAN::DEBUG;
  2187.   unless ( $ftp->get($file,$target) ){
  2188.     warn "Couldn't fetch $file from $host\n";
  2189.     return;
  2190.   }
  2191.   $ftp->quit; # it's ok if this fails
  2192.   return 1;
  2193. }
  2194.  
  2195. # If more accuracy is wanted/needed, Chris Leach sent me this patch...
  2196.  
  2197.  # > *** /install/perl/live/lib/CPAN.pm-    Wed Sep 24 13:08:48 1997
  2198.  # > --- /tmp/cp    Wed Sep 24 13:26:40 1997
  2199.  # > ***************
  2200.  # > *** 1562,1567 ****
  2201.  # > --- 1562,1580 ----
  2202.  # >       return 1 if substr($url,0,4) eq "file";
  2203.  # >       return 1 unless $url =~ m|://([^/]+)|;
  2204.  # >       my $host = $1;
  2205.  # > +     my $proxy = $CPAN::Config->{'http_proxy'} || $ENV{'http_proxy'};
  2206.  # > +     if ($proxy) {
  2207.  # > +         $proxy =~ m|://([^/:]+)|;
  2208.  # > +         $proxy = $1;
  2209.  # > +         my $noproxy = $CPAN::Config->{'no_proxy'} || $ENV{'no_proxy'};
  2210.  # > +         if ($noproxy) {
  2211.  # > +             if ($host !~ /$noproxy$/) {
  2212.  # > +                 $host = $proxy;
  2213.  # > +             }
  2214.  # > +         } else {
  2215.  # > +             $host = $proxy;
  2216.  # > +         }
  2217.  # > +     }
  2218.  # >       require Net::Ping;
  2219.  # >       return 1 unless $Net::Ping::VERSION >= 2;
  2220.  # >       my $p;
  2221.  
  2222.  
  2223. #-> sub CPAN::FTP::localize ;
  2224. sub localize {
  2225.     my($self,$file,$aslocal,$force) = @_;
  2226.     $force ||= 0;
  2227.     Carp::croak "Usage: ->localize(cpan_file,as_local_file[,$force])"
  2228.     unless defined $aslocal;
  2229.     $self->debug("file[$file] aslocal[$aslocal] force[$force]")
  2230.     if $CPAN::DEBUG;
  2231.  
  2232.     if ($^O eq 'MacOS') {
  2233.         # Comment by AK on 2000-09-03: Uniq short filenames would be
  2234.         # available in CHECKSUMS file
  2235.         my($name, $path) = File::Basename::fileparse($aslocal, '');
  2236.         if (length($name) > 31) {
  2237.             $name =~ s/(
  2238.                         \.(
  2239.                            readme(\.(gz|Z))? |
  2240.                            (tar\.)?(gz|Z) |
  2241.                            tgz |
  2242.                            zip |
  2243.                            pm\.(gz|Z)
  2244.                           )
  2245.                        )$//x;
  2246.             my $suf = $1;
  2247.             my $size = 31 - length($suf);
  2248.             while (length($name) > $size) {
  2249.                 chop $name;
  2250.             }
  2251.             $name .= $suf;
  2252.             $aslocal = File::Spec->catfile($path, $name);
  2253.         }
  2254.     }
  2255.  
  2256.     return $aslocal if -f $aslocal && -r _ && !($force & 1);
  2257.     my($restore) = 0;
  2258.     if (-f $aslocal){
  2259.     rename $aslocal, "$aslocal.bak";
  2260.     $restore++;
  2261.     }
  2262.  
  2263.     my($aslocal_dir) = File::Basename::dirname($aslocal);
  2264.     File::Path::mkpath($aslocal_dir);
  2265.     $CPAN::Frontend->mywarn(qq{Warning: You are not allowed to write into }.
  2266.     qq{directory "$aslocal_dir".
  2267.     I\'ll continue, but if you encounter problems, they may be due
  2268.     to insufficient permissions.\n}) unless -w $aslocal_dir;
  2269.  
  2270.     # Inheritance is not easier to manage than a few if/else branches
  2271.     if ($CPAN::META->has_usable('LWP::UserAgent')) {
  2272.      unless ($Ua) {
  2273.             CPAN::LWP::UserAgent->config;
  2274.         eval {$Ua = CPAN::LWP::UserAgent->new;}; # Why is has_usable still not fit enough?
  2275.             if ($@) {
  2276.                 $CPAN::Frontend->mywarn("CPAN::LWP::UserAgent->new dies with $@")
  2277.                     if $CPAN::DEBUG;
  2278.             } else {
  2279.                 my($var);
  2280.                 $Ua->proxy('ftp',  $var)
  2281.                     if $var = $CPAN::Config->{ftp_proxy} || $ENV{ftp_proxy};
  2282.                 $Ua->proxy('http', $var)
  2283.                     if $var = $CPAN::Config->{http_proxy} || $ENV{http_proxy};
  2284.  
  2285.  
  2286. # >>>>> On Wed, 13 Dec 2000 09:21:34 -0500, "Robison, Jonathon (J.M.)" <jrobiso2@visteon.com> said:
  2287. #  > I note that although CPAN.pm can use proxies, it doesn't seem equipped to
  2288. #  > use ones that require basic autorization.
  2289. #  
  2290. #  > Example of when I use it manually in my own stuff:
  2291. #  
  2292. #  > $ua->proxy(['http','ftp'], http://my.proxy.server:83');
  2293. #  > $req->proxy_authorization_basic("username","password");
  2294. #  > $res = $ua->request($req);
  2295.  
  2296.                 $Ua->no_proxy($var)
  2297.                     if $var = $CPAN::Config->{no_proxy} || $ENV{no_proxy};
  2298.             }
  2299.     }
  2300.     }
  2301.     $ENV{ftp_proxy} = $CPAN::Config->{ftp_proxy} if $CPAN::Config->{ftp_proxy};
  2302.     $ENV{http_proxy} = $CPAN::Config->{http_proxy}
  2303.         if $CPAN::Config->{http_proxy};
  2304.     $ENV{no_proxy} = $CPAN::Config->{no_proxy} if $CPAN::Config->{no_proxy};
  2305.  
  2306.     # Try the list of urls for each single object. We keep a record
  2307.     # where we did get a file from
  2308.     my(@reordered,$last);
  2309.     $CPAN::Config->{urllist} ||= [];
  2310.     unless (ref $CPAN::Config->{urllist} eq 'ARRAY') {
  2311.         warn "Malformed urllist; ignoring.  Configuration file corrupt?\n";
  2312.     }
  2313.     $last = $#{$CPAN::Config->{urllist}};
  2314.     if ($force & 2) { # local cpans probably out of date, don't reorder
  2315.     @reordered = (0..$last);
  2316.     } else {
  2317.     @reordered =
  2318.         sort {
  2319.         (substr($CPAN::Config->{urllist}[$b],0,4) eq "file")
  2320.             <=>
  2321.         (substr($CPAN::Config->{urllist}[$a],0,4) eq "file")
  2322.             or
  2323.         defined($Thesite)
  2324.             and
  2325.         ($b == $Thesite)
  2326.             <=>
  2327.         ($a == $Thesite)
  2328.         } 0..$last;
  2329.     }
  2330.     my(@levels);
  2331.     if ($Themethod) {
  2332.     @levels = ($Themethod, grep {$_ ne $Themethod} qw/easy hard hardest/);
  2333.     } else {
  2334.     @levels = qw/easy hard hardest/;
  2335.     }
  2336.     @levels = qw/easy/ if $^O eq 'MacOS';
  2337.     my($levelno);
  2338.     for $levelno (0..$#levels) {
  2339.         my $level = $levels[$levelno];
  2340.     my $method = "host$level";
  2341.     my @host_seq = $level eq "easy" ?
  2342.         @reordered : 0..$last;  # reordered has CDROM up front
  2343.     @host_seq = (0) unless @host_seq;
  2344.     my $ret = $self->$method(\@host_seq,$file,$aslocal);
  2345.     if ($ret) {
  2346.       $Themethod = $level;
  2347.       my $now = time;
  2348.       # utime $now, $now, $aslocal; # too bad, if we do that, we
  2349.                                       # might alter a local mirror
  2350.       $self->debug("level[$level]") if $CPAN::DEBUG;
  2351.       return $ret;
  2352.     } else {
  2353.       unlink $aslocal;
  2354.           last if $CPAN::Signal; # need to cleanup
  2355.     }
  2356.     }
  2357.     unless ($CPAN::Signal) {
  2358.         my(@mess);
  2359.         push @mess,
  2360.             qq{Please check, if the URLs I found in your configuration file \(}.
  2361.                 join(", ", @{$CPAN::Config->{urllist}}).
  2362.                     qq{\) are valid. The urllist can be edited.},
  2363.                         qq{E.g. with 'o conf urllist push ftp://myurl/'};
  2364.         $CPAN::Frontend->myprint(Text::Wrap::wrap("","",@mess). "\n\n");
  2365.         sleep 2;
  2366.         $CPAN::Frontend->myprint("Could not fetch $file\n");
  2367.     }
  2368.     if ($restore) {
  2369.     rename "$aslocal.bak", $aslocal;
  2370.     $CPAN::Frontend->myprint("Trying to get away with old file:\n" .
  2371.                  $self->ls($aslocal));
  2372.     return $aslocal;
  2373.     }
  2374.     return;
  2375. }
  2376.  
  2377. sub hosteasy {
  2378.     my($self,$host_seq,$file,$aslocal) = @_;
  2379.     my($i);
  2380.   HOSTEASY: for $i (@$host_seq) {
  2381.         my $url = $CPAN::Config->{urllist}[$i] || $CPAN::Defaultsite;
  2382.     $url .= "/" unless substr($url,-1) eq "/";
  2383.     $url .= $file;
  2384.     $self->debug("localizing perlish[$url]") if $CPAN::DEBUG;
  2385.     if ($url =~ /^file:/) {
  2386.         my $l;
  2387.         if ($CPAN::META->has_inst('URI::URL')) {
  2388.         my $u =  URI::URL->new($url);
  2389.         $l = $u->path;
  2390.         } else { # works only on Unix, is poorly constructed, but
  2391.         # hopefully better than nothing.
  2392.         # RFC 1738 says fileurl BNF is
  2393.         # fileurl = "file://" [ host | "localhost" ] "/" fpath
  2394.         # Thanks to "Mark D. Baushke" <mdb@cisco.com> for
  2395.         # the code
  2396.         ($l = $url) =~ s|^file://[^/]*/|/|; # discard the host part
  2397.         $l =~ s|^file:||;                   # assume they
  2398.                                                     # meant
  2399.                                                     # file://localhost
  2400.         $l =~ s|^/||s unless -f $l;         # e.g. /P:
  2401.         $self->debug("without URI::URL we try local file $l") if $CPAN::DEBUG;
  2402.         }
  2403.         if ( -f $l && -r _) {
  2404.         $Thesite = $i;
  2405.         return $l;
  2406.         }
  2407.         # Maybe mirror has compressed it?
  2408.         if (-f "$l.gz") {
  2409.         $self->debug("found compressed $l.gz") if $CPAN::DEBUG;
  2410.         CPAN::Tarzip->gunzip("$l.gz", $aslocal);
  2411.         if ( -f $aslocal) {
  2412.             $Thesite = $i;
  2413.             return $aslocal;
  2414.         }
  2415.         }
  2416.     }
  2417.         if ($CPAN::META->has_usable('LWP')) {
  2418.       $CPAN::Frontend->myprint("Fetching with LWP:
  2419.   $url
  2420. ");
  2421.       unless ($Ua) {
  2422.               CPAN::LWP::UserAgent->config;
  2423.               eval { $Ua = CPAN::LWP::UserAgent->new; };
  2424.               if ($@) {
  2425.                   $CPAN::Frontend->mywarn("CPAN::LWP::UserAgent->new dies with $@");
  2426.               }
  2427.       }
  2428.       my $res = $Ua->mirror($url, $aslocal);
  2429.       if ($res->is_success) {
  2430.         $Thesite = $i;
  2431.         my $now = time;
  2432.         utime $now, $now, $aslocal; # download time is more
  2433.                                         # important than upload time
  2434.         return $aslocal;
  2435.       } elsif ($url !~ /\.gz(?!\n)\Z/) {
  2436.         my $gzurl = "$url.gz";
  2437.         $CPAN::Frontend->myprint("Fetching with LWP:
  2438.   $gzurl
  2439. ");
  2440.         $res = $Ua->mirror($gzurl, "$aslocal.gz");
  2441.         if ($res->is_success &&
  2442.         CPAN::Tarzip->gunzip("$aslocal.gz",$aslocal)
  2443.            ) {
  2444.           $Thesite = $i;
  2445.           return $aslocal;
  2446.         }
  2447.       } else {
  2448.               $CPAN::Frontend->myprint(sprintf(
  2449.                                                "LWP failed with code[%s] message[%s]\n",
  2450.                                                $res->code,
  2451.                                                $res->message,
  2452.                                               ));
  2453.         # Alan Burlison informed me that in firewall environments
  2454.         # Net::FTP can still succeed where LWP fails. So we do not
  2455.         # skip Net::FTP anymore when LWP is available.
  2456.       }
  2457.     } else {
  2458.             $CPAN::Frontend->myprint("LWP not available\n");
  2459.     }
  2460.         return if $CPAN::Signal;
  2461.     if ($url =~ m|^ftp://(.*?)/(.*)/(.*)|) {
  2462.         # that's the nice and easy way thanks to Graham
  2463.         my($host,$dir,$getfile) = ($1,$2,$3);
  2464.         if ($CPAN::META->has_usable('Net::FTP')) {
  2465.         $dir =~ s|/+|/|g;
  2466.         $CPAN::Frontend->myprint("Fetching with Net::FTP:
  2467.   $url
  2468. ");
  2469.         $self->debug("getfile[$getfile]dir[$dir]host[$host]" .
  2470.                  "aslocal[$aslocal]") if $CPAN::DEBUG;
  2471.         if (CPAN::FTP->ftp_get($host,$dir,$getfile,$aslocal)) {
  2472.             $Thesite = $i;
  2473.             return $aslocal;
  2474.         }
  2475.         if ($aslocal !~ /\.gz(?!\n)\Z/) {
  2476.             my $gz = "$aslocal.gz";
  2477.             $CPAN::Frontend->myprint("Fetching with Net::FTP
  2478.   $url.gz
  2479. ");
  2480.            if (CPAN::FTP->ftp_get($host,
  2481.                        $dir,
  2482.                        "$getfile.gz",
  2483.                        $gz) &&
  2484.             CPAN::Tarzip->gunzip($gz,$aslocal)
  2485.                ){
  2486.             $Thesite = $i;
  2487.             return $aslocal;
  2488.             }
  2489.         }
  2490.         # next HOSTEASY;
  2491.         }
  2492.     }
  2493.         return if $CPAN::Signal;
  2494.     }
  2495. }
  2496.  
  2497. sub hosthard {
  2498.   my($self,$host_seq,$file,$aslocal) = @_;
  2499.  
  2500.   # Came back if Net::FTP couldn't establish connection (or
  2501.   # failed otherwise) Maybe they are behind a firewall, but they
  2502.   # gave us a socksified (or other) ftp program...
  2503.  
  2504.   my($i);
  2505.   my($devnull) = $CPAN::Config->{devnull} || "";
  2506.   # < /dev/null ";
  2507.   my($aslocal_dir) = File::Basename::dirname($aslocal);
  2508.   File::Path::mkpath($aslocal_dir);
  2509.   HOSTHARD: for $i (@$host_seq) {
  2510.     my $url = $CPAN::Config->{urllist}[$i] || $CPAN::Defaultsite;
  2511.     $url .= "/" unless substr($url,-1) eq "/";
  2512.     $url .= $file;
  2513.     my($proto,$host,$dir,$getfile);
  2514.  
  2515.     # Courtesy Mark Conty mark_conty@cargill.com change from
  2516.     # if ($url =~ m|^ftp://(.*?)/(.*)/(.*)|) {
  2517.     # to
  2518.     if ($url =~ m|^([^:]+)://(.*?)/(.*)/(.*)|) {
  2519.       # proto not yet used
  2520.       ($proto,$host,$dir,$getfile) = ($1,$2,$3,$4);
  2521.     } else {
  2522.       next HOSTHARD; # who said, we could ftp anything except ftp?
  2523.     }
  2524.         next HOSTHARD if $proto eq "file"; # file URLs would have had
  2525.                                            # success above. Likely a bogus URL
  2526.  
  2527.     $self->debug("localizing funkyftpwise[$url]") if $CPAN::DEBUG;
  2528.     my($f,$funkyftp);
  2529.     for $f ('lynx','ncftpget','ncftp','wget') {
  2530.       next unless exists $CPAN::Config->{$f};
  2531.       $funkyftp = $CPAN::Config->{$f};
  2532.       next unless defined $funkyftp;
  2533.       next if $funkyftp =~ /^\s*$/;
  2534.       my($asl_ungz, $asl_gz);
  2535.       ($asl_ungz = $aslocal) =~ s/\.gz//;
  2536.           $asl_gz = "$asl_ungz.gz";
  2537.       my($src_switch) = "";
  2538.       if ($f eq "lynx"){
  2539.         $src_switch = " -source";
  2540.       } elsif ($f eq "ncftp"){
  2541.         $src_switch = " -c";
  2542.           } elsif ($f eq "wget"){
  2543.               $src_switch = " -O -";
  2544.       }
  2545.       my($chdir) = "";
  2546.       my($stdout_redir) = " > $asl_ungz";
  2547.       if ($f eq "ncftpget"){
  2548.         $chdir = "cd $aslocal_dir && ";
  2549.         $stdout_redir = "";
  2550.       }
  2551.       $CPAN::Frontend->myprint(
  2552.                    qq[
  2553. Trying with "$funkyftp$src_switch" to get
  2554.     $url
  2555. ]);
  2556.       my($system) =
  2557.           "$chdir$funkyftp$src_switch \"$url\" $devnull$stdout_redir";
  2558.       $self->debug("system[$system]") if $CPAN::DEBUG;
  2559.       my($wstatus);
  2560.       if (($wstatus = system($system)) == 0
  2561.           &&
  2562.           ($f eq "lynx" ?
  2563.            -s $asl_ungz # lynx returns 0 when it fails somewhere
  2564.            : 1
  2565.           )
  2566.          ) {
  2567.         if (-s $aslocal) {
  2568.           # Looks good
  2569.         } elsif ($asl_ungz ne $aslocal) {
  2570.           # test gzip integrity
  2571.           if (CPAN::Tarzip->gtest($asl_ungz)) {
  2572.                   # e.g. foo.tar is gzipped --> foo.tar.gz
  2573.                   rename $asl_ungz, $aslocal;
  2574.           } else {
  2575.                   CPAN::Tarzip->gzip($asl_ungz,$asl_gz);
  2576.           }
  2577.         }
  2578.         $Thesite = $i;
  2579.         return $aslocal;
  2580.       } elsif ($url !~ /\.gz(?!\n)\Z/) {
  2581.         unlink $asl_ungz if
  2582.         -f $asl_ungz && -s _ == 0;
  2583.         my $gz = "$aslocal.gz";
  2584.         my $gzurl = "$url.gz";
  2585.         $CPAN::Frontend->myprint(
  2586.                      qq[
  2587. Trying with "$funkyftp$src_switch" to get
  2588.   $url.gz
  2589. ]);
  2590.         my($system) = "$funkyftp$src_switch \"$url.gz\" $devnull > $asl_gz";
  2591.         $self->debug("system[$system]") if $CPAN::DEBUG;
  2592.         my($wstatus);
  2593.         if (($wstatus = system($system)) == 0
  2594.         &&
  2595.         -s $asl_gz
  2596.            ) {
  2597.           # test gzip integrity
  2598.           if (CPAN::Tarzip->gtest($asl_gz)) {
  2599.                   CPAN::Tarzip->gunzip($asl_gz,$aslocal);
  2600.           } else {
  2601.                   # somebody uncompressed file for us?
  2602.                   rename $asl_ungz, $aslocal;
  2603.           }
  2604.           $Thesite = $i;
  2605.           return $aslocal;
  2606.         } else {
  2607.           unlink $asl_gz if -f $asl_gz;
  2608.         }
  2609.       } else {
  2610.         my $estatus = $wstatus >> 8;
  2611.         my $size = -f $aslocal ?
  2612.         ", left\n$aslocal with size ".-s _ :
  2613.             "\nWarning: expected file [$aslocal] doesn't exist";
  2614.         $CPAN::Frontend->myprint(qq{
  2615. System call "$system"
  2616. returned status $estatus (wstat $wstatus)$size
  2617. });
  2618.       }
  2619.           return if $CPAN::Signal;
  2620.     } # lynx,ncftpget,ncftp
  2621.     } # host
  2622. }
  2623.  
  2624. sub hosthardest {
  2625.     my($self,$host_seq,$file,$aslocal) = @_;
  2626.  
  2627.     my($i);
  2628.     my($aslocal_dir) = File::Basename::dirname($aslocal);
  2629.     File::Path::mkpath($aslocal_dir);
  2630.   HOSTHARDEST: for $i (@$host_seq) {
  2631.     unless (length $CPAN::Config->{'ftp'}) {
  2632.         $CPAN::Frontend->myprint("No external ftp command available\n\n");
  2633.         last HOSTHARDEST;
  2634.     }
  2635.     my $url = $CPAN::Config->{urllist}[$i] || $CPAN::Defaultsite;
  2636.     $url .= "/" unless substr($url,-1) eq "/";
  2637.     $url .= $file;
  2638.     $self->debug("localizing ftpwise[$url]") if $CPAN::DEBUG;
  2639.     unless ($url =~ m|^ftp://(.*?)/(.*)/(.*)|) {
  2640.         next;
  2641.     }
  2642.     my($host,$dir,$getfile) = ($1,$2,$3);
  2643.     my $timestamp = 0;
  2644.     my($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,
  2645.        $ctime,$blksize,$blocks) = stat($aslocal);
  2646.     $timestamp = $mtime ||= 0;
  2647.     my($netrc) = CPAN::FTP::netrc->new;
  2648.     my($netrcfile) = $netrc->netrc;
  2649.     my($verbose) = $CPAN::DEBUG{'FTP'} & $CPAN::DEBUG ? " -v" : "";
  2650.     my $targetfile = File::Basename::basename($aslocal);
  2651.     my(@dialog);
  2652.     push(
  2653.          @dialog,
  2654.          "lcd $aslocal_dir",
  2655.          "cd /",
  2656.          map("cd $_", split "/", $dir), # RFC 1738
  2657.          "bin",
  2658.          "get $getfile $targetfile",
  2659.          "quit"
  2660.         );
  2661.     if (! $netrcfile) {
  2662.         CPAN->debug("No ~/.netrc file found") if $CPAN::DEBUG;
  2663.     } elsif ($netrc->hasdefault || $netrc->contains($host)) {
  2664.         CPAN->debug(sprintf("hasdef[%d]cont($host)[%d]",
  2665.                 $netrc->hasdefault,
  2666.                 $netrc->contains($host))) if $CPAN::DEBUG;
  2667.         if ($netrc->protected) {
  2668.         $CPAN::Frontend->myprint(qq{
  2669.   Trying with external ftp to get
  2670.     $url
  2671.   As this requires some features that are not thoroughly tested, we\'re
  2672.   not sure, that we get it right....
  2673.  
  2674. }
  2675.              );
  2676.         $self->talk_ftp("$CPAN::Config->{'ftp'}$verbose $host",
  2677.                 @dialog);
  2678.         ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
  2679.          $atime,$mtime,$ctime,$blksize,$blocks) = stat($aslocal);
  2680.         $mtime ||= 0;
  2681.         if ($mtime > $timestamp) {
  2682.             $CPAN::Frontend->myprint("GOT $aslocal\n");
  2683.             $Thesite = $i;
  2684.             return $aslocal;
  2685.         } else {
  2686.             $CPAN::Frontend->myprint("Hmm... Still failed!\n");
  2687.         }
  2688.                 return if $CPAN::Signal;
  2689.         } else {
  2690.         $CPAN::Frontend->mywarn(qq{Your $netrcfile is not }.
  2691.                     qq{correctly protected.\n});
  2692.         }
  2693.     } else {
  2694.         $CPAN::Frontend->mywarn("Your ~/.netrc neither contains $host
  2695.   nor does it have a default entry\n");
  2696.     }
  2697.  
  2698.     # OK, they don't have a valid ~/.netrc. Use 'ftp -n'
  2699.     # then and login manually to host, using e-mail as
  2700.     # password.
  2701.     $CPAN::Frontend->myprint(qq{Issuing "$CPAN::Config->{'ftp'}$verbose -n"\n});
  2702.     unshift(
  2703.         @dialog,
  2704.         "open $host",
  2705.         "user anonymous $Config::Config{'cf_email'}"
  2706.            );
  2707.     $self->talk_ftp("$CPAN::Config->{'ftp'}$verbose -n", @dialog);
  2708.     ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
  2709.      $atime,$mtime,$ctime,$blksize,$blocks) = stat($aslocal);
  2710.     $mtime ||= 0;
  2711.     if ($mtime > $timestamp) {
  2712.         $CPAN::Frontend->myprint("GOT $aslocal\n");
  2713.         $Thesite = $i;
  2714.         return $aslocal;
  2715.     } else {
  2716.         $CPAN::Frontend->myprint("Bad luck... Still failed!\n");
  2717.     }
  2718.         return if $CPAN::Signal;
  2719.     $CPAN::Frontend->myprint("Can't access URL $url.\n\n");
  2720.     sleep 2;
  2721.     } # host
  2722. }
  2723.  
  2724. sub talk_ftp {
  2725.     my($self,$command,@dialog) = @_;
  2726.     my $fh = FileHandle->new;
  2727.     $fh->open("|$command") or die "Couldn't open ftp: $!";
  2728.     foreach (@dialog) { $fh->print("$_\n") }
  2729.     $fh->close;        # Wait for process to complete
  2730.     my $wstatus = $?;
  2731.     my $estatus = $wstatus >> 8;
  2732.     $CPAN::Frontend->myprint(qq{
  2733. Subprocess "|$command"
  2734.   returned status $estatus (wstat $wstatus)
  2735. }) if $wstatus;
  2736. }
  2737.  
  2738. # find2perl needs modularization, too, all the following is stolen
  2739. # from there
  2740. # CPAN::FTP::ls
  2741. sub ls {
  2742.     my($self,$name) = @_;
  2743.     my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$sizemm,
  2744.      $atime,$mtime,$ctime,$blksize,$blocks) = lstat($name);
  2745.  
  2746.     my($perms,%user,%group);
  2747.     my $pname = $name;
  2748.  
  2749.     if ($blocks) {
  2750.     $blocks = int(($blocks + 1) / 2);
  2751.     }
  2752.     else {
  2753.     $blocks = int(($sizemm + 1023) / 1024);
  2754.     }
  2755.  
  2756.     if    (-f _) { $perms = '-'; }
  2757.     elsif (-d _) { $perms = 'd'; }
  2758.     elsif (-c _) { $perms = 'c'; $sizemm = &sizemm; }
  2759.     elsif (-b _) { $perms = 'b'; $sizemm = &sizemm; }
  2760.     elsif (-p _) { $perms = 'p'; }
  2761.     elsif (-S _) { $perms = 's'; }
  2762.     else         { $perms = 'l'; $pname .= ' -> ' . readlink($_); }
  2763.  
  2764.     my(@rwx) = ('---','--x','-w-','-wx','r--','r-x','rw-','rwx');
  2765.     my(@moname) = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
  2766.     my $tmpmode = $mode;
  2767.     my $tmp = $rwx[$tmpmode & 7];
  2768.     $tmpmode >>= 3;
  2769.     $tmp = $rwx[$tmpmode & 7] . $tmp;
  2770.     $tmpmode >>= 3;
  2771.     $tmp = $rwx[$tmpmode & 7] . $tmp;
  2772.     substr($tmp,2,1) =~ tr/-x/Ss/ if -u _;
  2773.     substr($tmp,5,1) =~ tr/-x/Ss/ if -g _;
  2774.     substr($tmp,8,1) =~ tr/-x/Tt/ if -k _;
  2775.     $perms .= $tmp;
  2776.  
  2777.     my $user = $user{$uid} || $uid;   # too lazy to implement lookup
  2778.     my $group = $group{$gid} || $gid;
  2779.  
  2780.     my($sec,$min,$hour,$mday,$mon,$year) = localtime($mtime);
  2781.     my($timeyear);
  2782.     my($moname) = $moname[$mon];
  2783.     if (-M _ > 365.25 / 2) {
  2784.     $timeyear = $year + 1900;
  2785.     }
  2786.     else {
  2787.     $timeyear = sprintf("%02d:%02d", $hour, $min);
  2788.     }
  2789.  
  2790.     sprintf "%5lu %4ld %-10s %2d %-8s %-8s %8s %s %2d %5s %s\n",
  2791.         $ino,
  2792.          $blocks,
  2793.               $perms,
  2794.                 $nlink,
  2795.                 $user,
  2796.                      $group,
  2797.                       $sizemm,
  2798.                           $moname,
  2799.                          $mday,
  2800.                              $timeyear,
  2801.                              $pname;
  2802. }
  2803.  
  2804. package CPAN::FTP::netrc;
  2805.  
  2806. sub new {
  2807.     my($class) = @_;
  2808.     my $file = File::Spec->catfile($ENV{HOME},".netrc");
  2809.  
  2810.     my($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
  2811.        $atime,$mtime,$ctime,$blksize,$blocks)
  2812.     = stat($file);
  2813.     $mode ||= 0;
  2814.     my $protected = 0;
  2815.  
  2816.     my($fh,@machines,$hasdefault);
  2817.     $hasdefault = 0;
  2818.     $fh = FileHandle->new or die "Could not create a filehandle";
  2819.  
  2820.     if($fh->open($file)){
  2821.     $protected = ($mode & 077) == 0;
  2822.     local($/) = "";
  2823.       NETRC: while (<$fh>) {
  2824.         my(@tokens) = split " ", $_;
  2825.       TOKEN: while (@tokens) {
  2826.         my($t) = shift @tokens;
  2827.         if ($t eq "default"){
  2828.             $hasdefault++;
  2829.             last NETRC;
  2830.         }
  2831.         last TOKEN if $t eq "macdef";
  2832.         if ($t eq "machine") {
  2833.             push @machines, shift @tokens;
  2834.         }
  2835.         }
  2836.     }
  2837.     } else {
  2838.     $file = $hasdefault = $protected = "";
  2839.     }
  2840.  
  2841.     bless {
  2842.        'mach' => [@machines],
  2843.        'netrc' => $file,
  2844.        'hasdefault' => $hasdefault,
  2845.        'protected' => $protected,
  2846.       }, $class;
  2847. }
  2848.  
  2849. # CPAN::FTP::hasdefault;
  2850. sub hasdefault { shift->{'hasdefault'} }
  2851. sub netrc      { shift->{'netrc'}      }
  2852. sub protected  { shift->{'protected'}  }
  2853. sub contains {
  2854.     my($self,$mach) = @_;
  2855.     for ( @{$self->{'mach'}} ) {
  2856.     return 1 if $_ eq $mach;
  2857.     }
  2858.     return 0;
  2859. }
  2860.  
  2861. package CPAN::Complete;
  2862.  
  2863. sub gnu_cpl {
  2864.     my($text, $line, $start, $end) = @_;
  2865.     my(@perlret) = cpl($text, $line, $start);
  2866.     # find longest common match. Can anybody show me how to peruse
  2867.     # T::R::Gnu to have this done automatically? Seems expensive.
  2868.     return () unless @perlret;
  2869.     my($newtext) = $text;
  2870.     for (my $i = length($text)+1;;$i++) {
  2871.     last unless length($perlret[0]) && length($perlret[0]) >= $i;
  2872.     my $try = substr($perlret[0],0,$i);
  2873.     my @tries = grep {substr($_,0,$i) eq $try} @perlret;
  2874.     # warn "try[$try]tries[@tries]";
  2875.     if (@tries == @perlret) {
  2876.         $newtext = $try;
  2877.     } else {
  2878.         last;
  2879.     }
  2880.     }
  2881.     ($newtext,@perlret);
  2882. }
  2883.  
  2884. #-> sub CPAN::Complete::cpl ;
  2885. sub cpl {
  2886.     my($word,$line,$pos) = @_;
  2887.     $word ||= "";
  2888.     $line ||= "";
  2889.     $pos ||= 0;
  2890.     CPAN->debug("word [$word] line[$line] pos[$pos]") if $CPAN::DEBUG;
  2891.     $line =~ s/^\s*//;
  2892.     if ($line =~ s/^(force\s*)//) {
  2893.     $pos -= length($1);
  2894.     }
  2895.     my @return;
  2896.     if ($pos == 0) {
  2897.     @return = grep /^$word/, @CPAN::Complete::COMMANDS;
  2898.     } elsif ( $line !~ /^[\!abcdghimorutl]/ ) {
  2899.     @return = ();
  2900.     } elsif ($line =~ /^(a|ls)\s/) {
  2901.     @return = cplx('CPAN::Author',uc($word));
  2902.     } elsif ($line =~ /^b\s/) {
  2903.         CPAN::Shell->local_bundles;
  2904.     @return = cplx('CPAN::Bundle',$word);
  2905.     } elsif ($line =~ /^d\s/) {
  2906.     @return = cplx('CPAN::Distribution',$word);
  2907.     } elsif ($line =~ m/^(
  2908.                           [mru]|make|clean|dump|get|test|install|readme|look|cvs_import
  2909.                          )\s/x ) {
  2910.         if ($word =~ /^Bundle::/) {
  2911.             CPAN::Shell->local_bundles;
  2912.         }
  2913.     @return = (cplx('CPAN::Module',$word),cplx('CPAN::Bundle',$word));
  2914.     } elsif ($line =~ /^i\s/) {
  2915.     @return = cpl_any($word);
  2916.     } elsif ($line =~ /^reload\s/) {
  2917.     @return = cpl_reload($word,$line,$pos);
  2918.     } elsif ($line =~ /^o\s/) {
  2919.     @return = cpl_option($word,$line,$pos);
  2920.     } elsif ($line =~ m/^\S+\s/ ) {
  2921.         # fallback for future commands and what we have forgotten above
  2922.     @return = (cplx('CPAN::Module',$word),cplx('CPAN::Bundle',$word));
  2923.     } else {
  2924.     @return = ();
  2925.     }
  2926.     return @return;
  2927. }
  2928.  
  2929. #-> sub CPAN::Complete::cplx ;
  2930. sub cplx {
  2931.     my($class, $word) = @_;
  2932.     # I believed for many years that this was sorted, today I
  2933.     # realized, it wasn't sorted anymore. Now (rev 1.301 / v 1.55) I
  2934.     # make it sorted again. Maybe sort was dropped when GNU-readline
  2935.     # support came in? The RCS file is difficult to read on that:-(
  2936.     sort grep /^\Q$word\E/, map { $_->id } $CPAN::META->all_objects($class);
  2937. }
  2938.  
  2939. #-> sub CPAN::Complete::cpl_any ;
  2940. sub cpl_any {
  2941.     my($word) = shift;
  2942.     return (
  2943.         cplx('CPAN::Author',$word),
  2944.         cplx('CPAN::Bundle',$word),
  2945.         cplx('CPAN::Distribution',$word),
  2946.         cplx('CPAN::Module',$word),
  2947.        );
  2948. }
  2949.  
  2950. #-> sub CPAN::Complete::cpl_reload ;
  2951. sub cpl_reload {
  2952.     my($word,$line,$pos) = @_;
  2953.     $word ||= "";
  2954.     my(@words) = split " ", $line;
  2955.     CPAN->debug("word[$word] line[$line] pos[$pos]") if $CPAN::DEBUG;
  2956.     my(@ok) = qw(cpan index);
  2957.     return @ok if @words == 1;
  2958.     return grep /^\Q$word\E/, @ok if @words == 2 && $word;
  2959. }
  2960.  
  2961. #-> sub CPAN::Complete::cpl_option ;
  2962. sub cpl_option {
  2963.     my($word,$line,$pos) = @_;
  2964.     $word ||= "";
  2965.     my(@words) = split " ", $line;
  2966.     CPAN->debug("word[$word] line[$line] pos[$pos]") if $CPAN::DEBUG;
  2967.     my(@ok) = qw(conf debug);
  2968.     return @ok if @words == 1;
  2969.     return grep /^\Q$word\E/, @ok if @words == 2 && length($word);
  2970.     if (0) {
  2971.     } elsif ($words[1] eq 'index') {
  2972.     return ();
  2973.     } elsif ($words[1] eq 'conf') {
  2974.     return CPAN::Config::cpl(@_);
  2975.     } elsif ($words[1] eq 'debug') {
  2976.     return sort grep /^\Q$word\E/, sort keys %CPAN::DEBUG, 'all';
  2977.     }
  2978. }
  2979.  
  2980. package CPAN::Index;
  2981.  
  2982. #-> sub CPAN::Index::force_reload ;
  2983. sub force_reload {
  2984.     my($class) = @_;
  2985.     $CPAN::Index::LAST_TIME = 0;
  2986.     $class->reload(1);
  2987. }
  2988.  
  2989. #-> sub CPAN::Index::reload ;
  2990. sub reload {
  2991.     my($cl,$force) = @_;
  2992.     my $time = time;
  2993.  
  2994.     # XXX check if a newer one is available. (We currently read it
  2995.     # from time to time)
  2996.     for ($CPAN::Config->{index_expire}) {
  2997.     $_ = 0.001 unless $_ && $_ > 0.001;
  2998.     }
  2999.     unless (1 || $CPAN::Have_warned->{readmetadatacache}++) {
  3000.         # debug here when CPAN doesn't seem to read the Metadata
  3001.         require Carp;
  3002.         Carp::cluck("META-PROTOCOL[$CPAN::META->{PROTOCOL}]");
  3003.     }
  3004.     unless ($CPAN::META->{PROTOCOL}) {
  3005.         $cl->read_metadata_cache;
  3006.         $CPAN::META->{PROTOCOL} ||= "1.0";
  3007.     }
  3008.     if ( $CPAN::META->{PROTOCOL} < PROTOCOL  ) {
  3009.         # warn "Setting last_time to 0";
  3010.         $LAST_TIME = 0; # No warning necessary
  3011.     }
  3012.     return if $LAST_TIME + $CPAN::Config->{index_expire}*86400 > $time
  3013.     and ! $force;
  3014.     if (0) {
  3015.         # IFF we are developing, it helps to wipe out the memory
  3016.         # between reloads, otherwise it is not what a user expects.
  3017.         undef $CPAN::META; # Neue Gruendlichkeit since v1.52(r1.274)
  3018.         $CPAN::META = CPAN->new;
  3019.     }
  3020.     {
  3021.         my($debug,$t2);
  3022.         local $LAST_TIME = $time;
  3023.         local $CPAN::META->{PROTOCOL} = PROTOCOL;
  3024.  
  3025.         my $needshort = $^O eq "dos";
  3026.  
  3027.         $cl->rd_authindex($cl
  3028.                           ->reload_x(
  3029.                                      "authors/01mailrc.txt.gz",
  3030.                                      $needshort ?
  3031.                                      File::Spec->catfile('authors', '01mailrc.gz') :
  3032.                                      File::Spec->catfile('authors', '01mailrc.txt.gz'),
  3033.                                      $force));
  3034.         $t2 = time;
  3035.         $debug = "timing reading 01[".($t2 - $time)."]";
  3036.         $time = $t2;
  3037.         return if $CPAN::Signal; # this is sometimes lengthy
  3038.         $cl->rd_modpacks($cl
  3039.                          ->reload_x(
  3040.                                     "modules/02packages.details.txt.gz",
  3041.                                     $needshort ?
  3042.                                     File::Spec->catfile('modules', '02packag.gz') :
  3043.                                     File::Spec->catfile('modules', '02packages.details.txt.gz'),
  3044.                                     $force));
  3045.         $t2 = time;
  3046.         $debug .= "02[".($t2 - $time)."]";
  3047.         $time = $t2;
  3048.         return if $CPAN::Signal; # this is sometimes lengthy
  3049.         $cl->rd_modlist($cl
  3050.                         ->reload_x(
  3051.                                    "modules/03modlist.data.gz",
  3052.                                    $needshort ?
  3053.                                    File::Spec->catfile('modules', '03mlist.gz') :
  3054.                                    File::Spec->catfile('modules', '03modlist.data.gz'),
  3055.                                    $force));
  3056.         $cl->write_metadata_cache;
  3057.         $t2 = time;
  3058.         $debug .= "03[".($t2 - $time)."]";
  3059.         $time = $t2;
  3060.         CPAN->debug($debug) if $CPAN::DEBUG;
  3061.     }
  3062.     $LAST_TIME = $time;
  3063.     $CPAN::META->{PROTOCOL} = PROTOCOL;
  3064. }
  3065.  
  3066. #-> sub CPAN::Index::reload_x ;
  3067. sub reload_x {
  3068.     my($cl,$wanted,$localname,$force) = @_;
  3069.     $force |= 2; # means we're dealing with an index here
  3070.     CPAN::Config->load; # we should guarantee loading wherever we rely
  3071.                         # on Config XXX
  3072.     $localname ||= $wanted;
  3073.     my $abs_wanted = File::Spec->catfile($CPAN::Config->{'keep_source_where'},
  3074.                      $localname);
  3075.     if (
  3076.     -f $abs_wanted &&
  3077.     -M $abs_wanted < $CPAN::Config->{'index_expire'} &&
  3078.     !($force & 1)
  3079.        ) {
  3080.     my $s = $CPAN::Config->{'index_expire'} == 1 ? "" : "s";
  3081.     $cl->debug(qq{$abs_wanted younger than $CPAN::Config->{'index_expire'} }.
  3082.            qq{day$s. I\'ll use that.});
  3083.     return $abs_wanted;
  3084.     } else {
  3085.     $force |= 1; # means we're quite serious about it.
  3086.     }
  3087.     return CPAN::FTP->localize($wanted,$abs_wanted,$force);
  3088. }
  3089.  
  3090. #-> sub CPAN::Index::rd_authindex ;
  3091. sub rd_authindex {
  3092.     my($cl, $index_target) = @_;
  3093.     my @lines;
  3094.     return unless defined $index_target;
  3095.     $CPAN::Frontend->myprint("Going to read $index_target\n");
  3096.     local(*FH);
  3097.     tie *FH, CPAN::Tarzip, $index_target;
  3098.     local($/) = "\n";
  3099.     push @lines, split /\012/ while <FH>;
  3100.     foreach (@lines) {
  3101.     my($userid,$fullname,$email) =
  3102.         m/alias\s+(\S+)\s+\"([^\"\<]+)\s+\<([^\>]+)\>\"/;
  3103.     next unless $userid && $fullname && $email;
  3104.  
  3105.     # instantiate an author object
  3106.      my $userobj = $CPAN::META->instance('CPAN::Author',$userid);
  3107.     $userobj->set('FULLNAME' => $fullname, 'EMAIL' => $email);
  3108.     return if $CPAN::Signal;
  3109.     }
  3110. }
  3111.  
  3112. sub userid {
  3113.   my($self,$dist) = @_;
  3114.   $dist = $self->{'id'} unless defined $dist;
  3115.   my($ret) = $dist =~ m|(?:\w/\w\w/)?([^/]+)/|;
  3116.   $ret;
  3117. }
  3118.  
  3119. #-> sub CPAN::Index::rd_modpacks ;
  3120. sub rd_modpacks {
  3121.     my($self, $index_target) = @_;
  3122.     my @lines;
  3123.     return unless defined $index_target;
  3124.     $CPAN::Frontend->myprint("Going to read $index_target\n");
  3125.     my $fh = CPAN::Tarzip->TIEHANDLE($index_target);
  3126.     local($/) = "\n";
  3127.     while ($_ = $fh->READLINE) {
  3128.     s/\012/\n/g;
  3129.     my @ls = map {"$_\n"} split /\n/, $_;
  3130.     unshift @ls, "\n" x length($1) if /^(\n+)/;
  3131.     push @lines, @ls;
  3132.     }
  3133.     # read header
  3134.     my($line_count,$last_updated);
  3135.     while (@lines) {
  3136.     my $shift = shift(@lines);
  3137.     last if $shift =~ /^\s*$/;
  3138.     $shift =~ /^Line-Count:\s+(\d+)/ and $line_count = $1;
  3139.         $shift =~ /^Last-Updated:\s+(.+)/ and $last_updated = $1;
  3140.     }
  3141.     if (not defined $line_count) {
  3142.  
  3143.     warn qq{Warning: Your $index_target does not contain a Line-Count header.
  3144. Please check the validity of the index file by comparing it to more
  3145. than one CPAN mirror. I'll continue but problems seem likely to
  3146. happen.\a
  3147. };
  3148.  
  3149.     sleep 5;
  3150.     } elsif ($line_count != scalar @lines) {
  3151.  
  3152.     warn sprintf qq{Warning: Your %s
  3153. contains a Line-Count header of %d but I see %d lines there. Please
  3154. check the validity of the index file by comparing it to more than one
  3155. CPAN mirror. I'll continue but problems seem likely to happen.\a\n},
  3156. $index_target, $line_count, scalar(@lines);
  3157.  
  3158.     }
  3159.     if (not defined $last_updated) {
  3160.  
  3161.     warn qq{Warning: Your $index_target does not contain a Last-Updated header.
  3162. Please check the validity of the index file by comparing it to more
  3163. than one CPAN mirror. I'll continue but problems seem likely to
  3164. happen.\a
  3165. };
  3166.  
  3167.     sleep 5;
  3168.     } else {
  3169.  
  3170.     $CPAN::Frontend
  3171.             ->myprint(sprintf qq{  Database was generated on %s\n},
  3172.                       $last_updated);
  3173.         $DATE_OF_02 = $last_updated;
  3174.  
  3175.         if ($CPAN::META->has_inst(HTTP::Date)) {
  3176.             require HTTP::Date;
  3177.             my($age) = (time - HTTP::Date::str2time($last_updated))/3600/24;
  3178.             if ($age > 30) {
  3179.  
  3180.                 $CPAN::Frontend
  3181.                     ->mywarn(sprintf
  3182.                              qq{Warning: This index file is %d days old.
  3183.   Please check the host you chose as your CPAN mirror for staleness.
  3184.   I'll continue but problems seem likely to happen.\a\n},
  3185.                              $age);
  3186.  
  3187.             }
  3188.         } else {
  3189.             $CPAN::Frontend->myprint("  HTTP::Date not available\n");
  3190.         }
  3191.     }
  3192.  
  3193.  
  3194.     # A necessity since we have metadata_cache: delete what isn't
  3195.     # there anymore
  3196.     my $secondtime = $CPAN::META->exists("CPAN::Module","CPAN");
  3197.     CPAN->debug("secondtime[$secondtime]") if $CPAN::DEBUG;
  3198.     my(%exists);
  3199.     foreach (@lines) {
  3200.     chomp;
  3201.         # before 1.56 we split into 3 and discarded the rest. From
  3202.         # 1.57 we assign remaining text to $comment thus allowing to
  3203.         # influence isa_perl
  3204.     my($mod,$version,$dist,$comment) = split " ", $_, 4;
  3205.     my($bundle,$id,$userid);
  3206.  
  3207.     if ($mod eq 'CPAN' &&
  3208.         ! (
  3209.            CPAN::Queue->exists('Bundle::CPAN') ||
  3210.            CPAN::Queue->exists('CPAN')
  3211.           )
  3212.        ) {
  3213.             local($^W)= 0;
  3214.             if ($version > $CPAN::VERSION){
  3215.                 $CPAN::Frontend->myprint(qq{
  3216.   There's a new CPAN.pm version (v$version) available!
  3217.   [Current version is v$CPAN::VERSION]
  3218.   You might want to try
  3219.     install Bundle::CPAN
  3220.     reload cpan
  3221.   without quitting the current session. It should be a seamless upgrade
  3222.   while we are running...
  3223. }); #});
  3224.                 sleep 2;
  3225.         $CPAN::Frontend->myprint(qq{\n});
  3226.         }
  3227.         last if $CPAN::Signal;
  3228.     } elsif ($mod =~ /^Bundle::(.*)/) {
  3229.         $bundle = $1;
  3230.     }
  3231.  
  3232.     if ($bundle){
  3233.         $id =  $CPAN::META->instance('CPAN::Bundle',$mod);
  3234.         # Let's make it a module too, because bundles have so much
  3235.         # in common with modules.
  3236.  
  3237.             # Changed in 1.57_63: seems like memory bloat now without
  3238.             # any value, so commented out
  3239.  
  3240.         # $CPAN::META->instance('CPAN::Module',$mod);
  3241.  
  3242.     } else {
  3243.  
  3244.         # instantiate a module object
  3245.         $id = $CPAN::META->instance('CPAN::Module',$mod);
  3246.  
  3247.     }
  3248.  
  3249.     if ($id->cpan_file ne $dist){ # update only if file is
  3250.                                       # different. CPAN prohibits same
  3251.                                       # name with different version
  3252.         $userid = $self->userid($dist);
  3253.         $id->set(
  3254.              'CPAN_USERID' => $userid,
  3255.              'CPAN_VERSION' => $version,
  3256.              'CPAN_FILE' => $dist,
  3257.             );
  3258.     }
  3259.  
  3260.     # instantiate a distribution object
  3261.     if ($CPAN::META->exists('CPAN::Distribution',$dist)) {
  3262.       # we do not need CONTAINSMODS unless we do something with
  3263.       # this dist, so we better produce it on demand.
  3264.  
  3265.       ## my $obj = $CPAN::META->instance(
  3266.       ##                   'CPAN::Distribution' => $dist
  3267.       ##                  );
  3268.       ## $obj->{CONTAINSMODS}{$mod} = undef; # experimental
  3269.     } else {
  3270.       $CPAN::META->instance(
  3271.                 'CPAN::Distribution' => $dist
  3272.                    )->set(
  3273.                       'CPAN_USERID' => $userid,
  3274.                                       'CPAN_COMMENT' => $comment,
  3275.                      );
  3276.     }
  3277.         if ($secondtime) {
  3278.             for my $name ($mod,$dist) {
  3279.                 CPAN->debug("exists name[$name]") if $CPAN::DEBUG;
  3280.                 $exists{$name} = undef;
  3281.             }
  3282.         }
  3283.     return if $CPAN::Signal;
  3284.     }
  3285.     undef $fh;
  3286.     if ($secondtime) {
  3287.         for my $class (qw(CPAN::Module CPAN::Bundle CPAN::Distribution)) {
  3288.             for my $o ($CPAN::META->all_objects($class)) {
  3289.                 next if exists $exists{$o->{ID}};
  3290.                 $CPAN::META->delete($class,$o->{ID});
  3291.                 CPAN->debug("deleting ID[$o->{ID}] in class[$class]")
  3292.                     if $CPAN::DEBUG;
  3293.             }
  3294.         }
  3295.     }
  3296. }
  3297.  
  3298. #-> sub CPAN::Index::rd_modlist ;
  3299. sub rd_modlist {
  3300.     my($cl,$index_target) = @_;
  3301.     return unless defined $index_target;
  3302.     $CPAN::Frontend->myprint("Going to read $index_target\n");
  3303.     my $fh = CPAN::Tarzip->TIEHANDLE($index_target);
  3304.     my @eval;
  3305.     local($/) = "\n";
  3306.     while ($_ = $fh->READLINE) {
  3307.     s/\012/\n/g;
  3308.     my @ls = map {"$_\n"} split /\n/, $_;
  3309.     unshift @ls, "\n" x length($1) if /^(\n+)/;
  3310.     push @eval, @ls;
  3311.     }
  3312.     while (@eval) {
  3313.     my $shift = shift(@eval);
  3314.     if ($shift =~ /^Date:\s+(.*)/){
  3315.         return if $DATE_OF_03 eq $1;
  3316.         ($DATE_OF_03) = $1;
  3317.     }
  3318.     last if $shift =~ /^\s*$/;
  3319.     }
  3320.     undef $fh;
  3321.     push @eval, q{CPAN::Modulelist->data;};
  3322.     local($^W) = 0;
  3323.     my($comp) = Safe->new("CPAN::Safe1");
  3324.     my($eval) = join("", @eval);
  3325.     my $ret = $comp->reval($eval);
  3326.     Carp::confess($@) if $@;
  3327.     return if $CPAN::Signal;
  3328.     for (keys %$ret) {
  3329.     my $obj = $CPAN::META->instance("CPAN::Module",$_);
  3330.         delete $ret->{$_}{modid}; # not needed here, maybe elsewhere
  3331.     $obj->set(%{$ret->{$_}});
  3332.     return if $CPAN::Signal;
  3333.     }
  3334. }
  3335.  
  3336. #-> sub CPAN::Index::write_metadata_cache ;
  3337. sub write_metadata_cache {
  3338.     my($self) = @_;
  3339.     return unless $CPAN::Config->{'cache_metadata'};
  3340.     return unless $CPAN::META->has_usable("Storable");
  3341.     my $cache;
  3342.     foreach my $k (qw(CPAN::Bundle CPAN::Author CPAN::Module
  3343.               CPAN::Distribution)) {
  3344.     $cache->{$k} = $CPAN::META->{readonly}{$k}; # unsafe meta access, ok
  3345.     }
  3346.     my $metadata_file = File::Spec->catfile($CPAN::Config->{cpan_home},"Metadata");
  3347.     $cache->{last_time} = $LAST_TIME;
  3348.     $cache->{DATE_OF_02} = $DATE_OF_02;
  3349.     $cache->{PROTOCOL} = PROTOCOL;
  3350.     $CPAN::Frontend->myprint("Going to write $metadata_file\n");
  3351.     eval { Storable::nstore($cache, $metadata_file) };
  3352.     $CPAN::Frontend->mywarn($@) if $@;
  3353. }
  3354.  
  3355. #-> sub CPAN::Index::read_metadata_cache ;
  3356. sub read_metadata_cache {
  3357.     my($self) = @_;
  3358.     return unless $CPAN::Config->{'cache_metadata'};
  3359.     return unless $CPAN::META->has_usable("Storable");
  3360.     my $metadata_file = File::Spec->catfile($CPAN::Config->{cpan_home},"Metadata");
  3361.     return unless -r $metadata_file and -f $metadata_file;
  3362.     $CPAN::Frontend->myprint("Going to read $metadata_file\n");
  3363.     my $cache;
  3364.     eval { $cache = Storable::retrieve($metadata_file) };
  3365.     $CPAN::Frontend->mywarn($@) if $@;
  3366.     if (!$cache || ref $cache ne 'HASH'){
  3367.         $LAST_TIME = 0;
  3368.         return;
  3369.     }
  3370.     if (exists $cache->{PROTOCOL}) {
  3371.         if (PROTOCOL > $cache->{PROTOCOL}) {
  3372.             $CPAN::Frontend->mywarn(sprintf("Ignoring Metadata cache written ".
  3373.                                             "with protocol v%s, requiring v%s",
  3374.                                             $cache->{PROTOCOL},
  3375.                                             PROTOCOL)
  3376.                                    );
  3377.             return;
  3378.         }
  3379.     } else {
  3380.         $CPAN::Frontend->mywarn("Ignoring Metadata cache written ".
  3381.                                 "with protocol v1.0");
  3382.         return;
  3383.     }
  3384.     my $clcnt = 0;
  3385.     my $idcnt = 0;
  3386.     while(my($class,$v) = each %$cache) {
  3387.     next unless $class =~ /^CPAN::/;
  3388.     $CPAN::META->{readonly}{$class} = $v; # unsafe meta access, ok
  3389.         while (my($id,$ro) = each %$v) {
  3390.             $CPAN::META->{readwrite}{$class}{$id} ||=
  3391.                 $class->new(ID=>$id, RO=>$ro);
  3392.             $idcnt++;
  3393.         }
  3394.         $clcnt++;
  3395.     }
  3396.     unless ($clcnt) { # sanity check
  3397.         $CPAN::Frontend->myprint("Warning: Found no data in $metadata_file\n");
  3398.         return;
  3399.     }
  3400.     if ($idcnt < 1000) {
  3401.         $CPAN::Frontend->myprint("Warning: Found only $idcnt objects ".
  3402.                                  "in $metadata_file\n");
  3403.         return;
  3404.     }
  3405.     $CPAN::META->{PROTOCOL} ||=
  3406.         $cache->{PROTOCOL}; # reading does not up or downgrade, but it
  3407.                             # does initialize to some protocol
  3408.     $LAST_TIME = $cache->{last_time};
  3409.     $DATE_OF_02 = $cache->{DATE_OF_02};
  3410.     $CPAN::Frontend->myprint("  Database was generated on $DATE_OF_02\n")
  3411.     if defined $DATE_OF_02; # An old cache may not contain DATE_OF_02
  3412.     return;
  3413. }
  3414.  
  3415. package CPAN::InfoObj;
  3416.  
  3417. # Accessors
  3418. sub cpan_userid { shift->{RO}{CPAN_USERID} }
  3419. sub id { shift->{ID}; }
  3420.  
  3421. #-> sub CPAN::InfoObj::new ;
  3422. sub new {
  3423.     my $this = bless {}, shift;
  3424.     %$this = @_;
  3425.     $this
  3426. }
  3427.  
  3428. # The set method may only be used by code that reads index data or
  3429. # otherwise "objective" data from the outside world. All session
  3430. # related material may do anything else with instance variables but
  3431. # must not touch the hash under the RO attribute. The reason is that
  3432. # the RO hash gets written to Metadata file and is thus persistent.
  3433.  
  3434. #-> sub CPAN::InfoObj::set ;
  3435. sub set {
  3436.     my($self,%att) = @_;
  3437.     my $class = ref $self;
  3438.  
  3439.     # This must be ||=, not ||, because only if we write an empty
  3440.     # reference, only then the set method will write into the readonly
  3441.     # area. But for Distributions that spring into existence, maybe
  3442.     # because of a typo, we do not like it that they are written into
  3443.     # the readonly area and made permanent (at least for a while) and
  3444.     # that is why we do not "allow" other places to call ->set.
  3445.     unless ($self->id) {
  3446.         CPAN->debug("Bug? Empty ID, rejecting");
  3447.         return;
  3448.     }
  3449.     my $ro = $self->{RO} =
  3450.         $CPAN::META->{readonly}{$class}{$self->id} ||= {};
  3451.  
  3452.     while (my($k,$v) = each %att) {
  3453.         $ro->{$k} = $v;
  3454.     }
  3455. }
  3456.  
  3457. #-> sub CPAN::InfoObj::as_glimpse ;
  3458. sub as_glimpse {
  3459.     my($self) = @_;
  3460.     my(@m);
  3461.     my $class = ref($self);
  3462.     $class =~ s/^CPAN:://;
  3463.     push @m, sprintf "%-15s %s\n", $class, $self->{ID};
  3464.     join "", @m;
  3465. }
  3466.  
  3467. #-> sub CPAN::InfoObj::as_string ;
  3468. sub as_string {
  3469.     my($self) = @_;
  3470.     my(@m);
  3471.     my $class = ref($self);
  3472.     $class =~ s/^CPAN:://;
  3473.     push @m, $class, " id = $self->{ID}\n";
  3474.     for (sort keys %{$self->{RO}}) {
  3475.     # next if m/^(ID|RO)$/;
  3476.     my $extra = "";
  3477.     if ($_ eq "CPAN_USERID") {
  3478.             $extra .= " (".$self->author;
  3479.             my $email; # old perls!
  3480.             if ($email = $CPAN::META->instance("CPAN::Author",
  3481.                                                $self->cpan_userid
  3482.                                               )->email) {
  3483.                 $extra .= " <$email>";
  3484.             } else {
  3485.                 $extra .= " <no email>";
  3486.             }
  3487.             $extra .= ")";
  3488.         } elsif ($_ eq "FULLNAME") { # potential UTF-8 conversion
  3489.             push @m, sprintf "    %-12s %s\n", $_, $self->fullname;
  3490.             next;
  3491.         }
  3492.         next unless defined $self->{RO}{$_};
  3493.         push @m, sprintf "    %-12s %s%s\n", $_, $self->{RO}{$_}, $extra;
  3494.     }
  3495.     for (sort keys %$self) {
  3496.     next if m/^(ID|RO)$/;
  3497.     if (ref($self->{$_}) eq "ARRAY") {
  3498.       push @m, sprintf "    %-12s %s\n", $_, "@{$self->{$_}}";
  3499.     } elsif (ref($self->{$_}) eq "HASH") {
  3500.       push @m, sprintf(
  3501.                "    %-12s %s\n",
  3502.                $_,
  3503.                join(" ",keys %{$self->{$_}}),
  3504.                           );
  3505.     } else {
  3506.       push @m, sprintf "    %-12s %s\n", $_, $self->{$_};
  3507.     }
  3508.     }
  3509.     join "", @m, "\n";
  3510. }
  3511.  
  3512. #-> sub CPAN::InfoObj::author ;
  3513. sub author {
  3514.     my($self) = @_;
  3515.     $CPAN::META->instance("CPAN::Author",$self->cpan_userid)->fullname;
  3516. }
  3517.  
  3518. #-> sub CPAN::InfoObj::dump ;
  3519. sub dump {
  3520.   my($self) = @_;
  3521.   require Data::Dumper;
  3522.   print Data::Dumper::Dumper($self);
  3523. }
  3524.  
  3525. package CPAN::Author;
  3526.  
  3527. #-> sub CPAN::Author::id
  3528. sub id {
  3529.     my $self = shift;
  3530.     my $id = $self->{ID};
  3531.     $CPAN::Frontend->mydie("Illegal author id[$id]") unless $id =~ /^[A-Z]/;
  3532.     $id;
  3533. }
  3534.  
  3535. #-> sub CPAN::Author::as_glimpse ;
  3536. sub as_glimpse {
  3537.     my($self) = @_;
  3538.     my(@m);
  3539.     my $class = ref($self);
  3540.     $class =~ s/^CPAN:://;
  3541.     push @m, sprintf(qq{%-15s %s ("%s" <%s>)\n},
  3542.                      $class,
  3543.                      $self->{ID},
  3544.                      $self->fullname,
  3545.                      $self->email);
  3546.     join "", @m;
  3547. }
  3548.  
  3549. #-> sub CPAN::Author::fullname ;
  3550. sub fullname {
  3551.     shift->{RO}{FULLNAME};
  3552. }
  3553. *name = \&fullname;
  3554.  
  3555. #-> sub CPAN::Author::email ;
  3556. sub email    { shift->{RO}{EMAIL}; }
  3557.  
  3558. #-> sub CPAN::Author::ls ;
  3559. sub ls {
  3560.     my $self = shift;
  3561.     my $id = $self->id;
  3562.  
  3563.     # adapted from CPAN::Distribution::verifyMD5 ;
  3564.     my(@csf); # chksumfile
  3565.     @csf = $self->id =~ /(.)(.)(.*)/;
  3566.     $csf[1] = join "", @csf[0,1];
  3567.     $csf[2] = join "", @csf[1,2];
  3568.     my(@dl);
  3569.     @dl = $self->dir_listing([$csf[0],"CHECKSUMS"], 0);
  3570.     unless (grep {$_->[2] eq $csf[1]} @dl) {
  3571.         $CPAN::Frontend->myprint("No files in the directory of $id\n");
  3572.         return;
  3573.     }
  3574.     @dl = $self->dir_listing([@csf[0,1],"CHECKSUMS"], 0);
  3575.     unless (grep {$_->[2] eq $csf[2]} @dl) {
  3576.         $CPAN::Frontend->myprint("No files in the directory of $id\n");
  3577.         return;
  3578.     }
  3579.     @dl = $self->dir_listing([@csf,"CHECKSUMS"], 1);
  3580.     $CPAN::Frontend->myprint(join "", map {
  3581.         sprintf("%8d %10s %s/%s\n", $_->[0], $_->[1], $id, $_->[2])
  3582.     } sort { $a->[2] cmp $b->[2] } @dl);
  3583. }
  3584.  
  3585. # returns an array of arrays, the latter contain (size,mtime,filename)
  3586. #-> sub CPAN::Author::dir_listing ;
  3587. sub dir_listing {
  3588.     my $self = shift;
  3589.     my $chksumfile = shift;
  3590.     my $recursive = shift;
  3591.     my $lc_want =
  3592.     File::Spec->catfile($CPAN::Config->{keep_source_where},
  3593.                 "authors", "id", @$chksumfile);
  3594.     local($") = "/";
  3595.     # connect "force" argument with "index_expire".
  3596.     my $force = 0;
  3597.     if (my @stat = stat $lc_want) {
  3598.         $force = $stat[9] + $CPAN::Config->{index_expire}*86400 <= time;
  3599.     }
  3600.     my $lc_file = CPAN::FTP->localize("authors/id/@$chksumfile",
  3601.                                       $lc_want,$force);
  3602.     unless ($lc_file) {
  3603.         $CPAN::Frontend->myprint("Trying $lc_want.gz\n");
  3604.     $chksumfile->[-1] .= ".gz";
  3605.     $lc_file = CPAN::FTP->localize("authors/id/@$chksumfile",
  3606.                                        "$lc_want.gz",1);
  3607.     if ($lc_file) {
  3608.         $lc_file =~ s{\.gz(?!\n)\Z}{}; #};
  3609.         CPAN::Tarzip->gunzip("$lc_file.gz",$lc_file);
  3610.     } else {
  3611.         return;
  3612.     }
  3613.     }
  3614.  
  3615.     # adapted from CPAN::Distribution::MD5_check_file ;
  3616.     my $fh = FileHandle->new;
  3617.     my($cksum);
  3618.     if (open $fh, $lc_file){
  3619.     local($/);
  3620.     my $eval = <$fh>;
  3621.     $eval =~ s/\015?\012/\n/g;
  3622.     close $fh;
  3623.     my($comp) = Safe->new();
  3624.     $cksum = $comp->reval($eval);
  3625.     if ($@) {
  3626.         rename $lc_file, "$lc_file.bad";
  3627.         Carp::confess($@) if $@;
  3628.     }
  3629.     } else {
  3630.     Carp::carp "Could not open $lc_file for reading";
  3631.     }
  3632.     my(@result,$f);
  3633.     for $f (sort keys %$cksum) {
  3634.         if (exists $cksum->{$f}{isdir}) {
  3635.             if ($recursive) {
  3636.                 my(@dir) = @$chksumfile;
  3637.                 pop @dir;
  3638.                 push @dir, $f, "CHECKSUMS";
  3639.                 push @result, map {
  3640.                     [$_->[0], $_->[1], "$f/$_->[2]"]
  3641.                 } $self->dir_listing(\@dir,1);
  3642.             } else {
  3643.                 push @result, [ 0, "-", $f ];
  3644.             }
  3645.         } else {
  3646.             push @result, [
  3647.                            ($cksum->{$f}{"size"}||0),
  3648.                            $cksum->{$f}{"mtime"}||"---",
  3649.                            $f
  3650.                           ];
  3651.         }
  3652.     }
  3653.     @result;
  3654. }
  3655.  
  3656. package CPAN::Distribution;
  3657.  
  3658. # Accessors
  3659. sub cpan_comment { shift->{RO}{CPAN_COMMENT} }
  3660.  
  3661. sub undelay {
  3662.     my $self = shift;
  3663.     delete $self->{later};
  3664. }
  3665.  
  3666. # CPAN::Distribution::normalize
  3667. sub normalize {
  3668.     my($self,$s) = @_;
  3669.     $s = $self->id unless defined $s;
  3670.     if (
  3671.         $s =~ tr|/|| == 1
  3672.         or
  3673.         $s !~ m|[A-Z]/[A-Z-]{2}/[A-Z-]{2,}/|
  3674.        ) {
  3675.         return $s if $s =~ m:^N/A|^Contact Author: ;
  3676.         $s =~ s|^(.)(.)([^/]*/)(.+)$|$1/$1$2/$1$2$3$4| or
  3677.             $CPAN::Frontend->mywarn("Strange distribution name [$s]");
  3678.         CPAN->debug("s[$s]") if $CPAN::DEBUG;
  3679.     }
  3680.     $s;
  3681. }
  3682.  
  3683. #-> sub CPAN::Distribution::color_cmd_tmps ;
  3684. sub color_cmd_tmps {
  3685.     my($self) = shift;
  3686.     my($depth) = shift || 0;
  3687.     my($color) = shift || 0;
  3688.     # a distribution needs to recurse into its prereq_pms
  3689.  
  3690.     return if exists $self->{incommandcolor}
  3691.         && $self->{incommandcolor}==$color;
  3692.     $CPAN::Frontend->mydie(sprintf("CPAN.pm panic: deep recursion in ".
  3693.                                    "color_cmd_tmps depth[%s] self[%s] id[%s]",
  3694.                                    $depth,
  3695.                                    $self,
  3696.                                    $self->id
  3697.                                   )) if $depth>=100;
  3698.     ##### warn "color_cmd_tmps $depth $color " . $self->id; # sleep 1;
  3699.     my $prereq_pm = $self->prereq_pm;
  3700.     if (defined $prereq_pm) {
  3701.         for my $pre (keys %$prereq_pm) {
  3702.             my $premo = CPAN::Shell->expand("Module",$pre);
  3703.             $premo->color_cmd_tmps($depth+1,$color);
  3704.         }
  3705.     }
  3706.     if ($color==0) {
  3707.         delete $self->{sponsored_mods};
  3708.         delete $self->{badtestcnt};
  3709.     }
  3710.     $self->{incommandcolor} = $color;
  3711. }
  3712.  
  3713. #-> sub CPAN::Distribution::as_string ;
  3714. sub as_string {
  3715.   my $self = shift;
  3716.   $self->containsmods;
  3717.   $self->SUPER::as_string(@_);
  3718. }
  3719.  
  3720. #-> sub CPAN::Distribution::containsmods ;
  3721. sub containsmods {
  3722.   my $self = shift;
  3723.   return keys %{$self->{CONTAINSMODS}} if exists $self->{CONTAINSMODS};
  3724.   my $dist_id = $self->{ID};
  3725.   for my $mod ($CPAN::META->all_objects("CPAN::Module")) {
  3726.     my $mod_file = $mod->cpan_file or next;
  3727.     my $mod_id = $mod->{ID} or next;
  3728.     # warn "mod_file[$mod_file] dist_id[$dist_id] mod_id[$mod_id]";
  3729.     # sleep 1;
  3730.     $self->{CONTAINSMODS}{$mod_id} = undef if $mod_file eq $dist_id;
  3731.   }
  3732.   keys %{$self->{CONTAINSMODS}};
  3733. }
  3734.  
  3735. #-> sub CPAN::Distribution::uptodate ;
  3736. sub uptodate {
  3737.     my($self) = @_;
  3738.     my $c;
  3739.     foreach $c ($self->containsmods) {
  3740.         my $obj = CPAN::Shell->expandany($c);
  3741.         return 0 unless $obj->uptodate;
  3742.     }
  3743.     return 1;
  3744. }
  3745.  
  3746. #-> sub CPAN::Distribution::called_for ;
  3747. sub called_for {
  3748.     my($self,$id) = @_;
  3749.     $self->{CALLED_FOR} = $id if defined $id;
  3750.     return $self->{CALLED_FOR};
  3751. }
  3752.  
  3753. #-> sub CPAN::Distribution::safe_chdir ;
  3754. sub safe_chdir {
  3755.     my($self,$todir) = @_;
  3756.     # we die if we cannot chdir and we are debuggable
  3757.     Carp::confess("safe_chdir called without todir argument")
  3758.           unless defined $todir and length $todir;
  3759.     if (chdir $todir) {
  3760.         $self->debug(sprintf "changed directory to %s", CPAN::anycwd())
  3761.             if $CPAN::DEBUG;
  3762.     } else {
  3763.         my $cwd = CPAN::anycwd();
  3764.         $CPAN::Frontend->mydie(qq{Could not chdir from cwd[$cwd] }.
  3765.                                qq{to todir[$todir]: $!});
  3766.     }
  3767. }
  3768.  
  3769. #-> sub CPAN::Distribution::get ;
  3770. sub get {
  3771.     my($self) = @_;
  3772.   EXCUSE: {
  3773.     my @e;
  3774.     exists $self->{'build_dir'} and push @e,
  3775.         "Is already unwrapped into directory $self->{'build_dir'}";
  3776.     $CPAN::Frontend->myprint(join "", map {"  $_\n"} @e) and return if @e;
  3777.     }
  3778.     my $sub_wd = CPAN::anycwd(); # for cleaning up as good as possible
  3779.  
  3780.     #
  3781.     # Get the file on local disk
  3782.     #
  3783.  
  3784.     my($local_file);
  3785.     my($local_wanted) =
  3786.         File::Spec->catfile(
  3787.                 $CPAN::Config->{keep_source_where},
  3788.                 "authors",
  3789.                 "id",
  3790.                 split("/",$self->id)
  3791.                );
  3792.  
  3793.     $self->debug("Doing localize") if $CPAN::DEBUG;
  3794.     unless ($local_file =
  3795.             CPAN::FTP->localize("authors/id/$self->{ID}",
  3796.                                 $local_wanted)) {
  3797.         my $note = "";
  3798.         if ($CPAN::Index::DATE_OF_02) {
  3799.             $note = "Note: Current database in memory was generated ".
  3800.                 "on $CPAN::Index::DATE_OF_02\n";
  3801.         }
  3802.         $CPAN::Frontend->mydie("Giving up on '$local_wanted'\n$note");
  3803.     }
  3804.     $self->debug("local_file[$local_file]") if $CPAN::DEBUG;
  3805.     $self->{localfile} = $local_file;
  3806.     return if $CPAN::Signal;
  3807.  
  3808.     #
  3809.     # Check integrity
  3810.     #
  3811.     if ($CPAN::META->has_inst("Digest::MD5")) {
  3812.     $self->debug("Digest::MD5 is installed, verifying");
  3813.     $self->verifyMD5;
  3814.     } else {
  3815.     $self->debug("Digest::MD5 is NOT installed");
  3816.     }
  3817.     return if $CPAN::Signal;
  3818.  
  3819.     #
  3820.     # Create a clean room and go there
  3821.     #
  3822.     $CPAN::META->{cachemgr} ||= CPAN::CacheMgr->new(); # unsafe meta access, ok
  3823.     my $builddir = $CPAN::META->{cachemgr}->dir; # unsafe meta access, ok
  3824.     $self->safe_chdir($builddir);
  3825.     $self->debug("Removing tmp") if $CPAN::DEBUG;
  3826.     File::Path::rmtree("tmp");
  3827.     mkdir "tmp", 0755 or Carp::croak "Couldn't mkdir tmp: $!";
  3828.     if ($CPAN::Signal){
  3829.         $self->safe_chdir($sub_wd);
  3830.         return;
  3831.     }
  3832.     $self->safe_chdir("tmp");
  3833.  
  3834.     #
  3835.     # Unpack the goods
  3836.     #
  3837.     if ($local_file =~ /(\.tar\.(gz|Z)|\.tgz)(?!\n)\Z/i){
  3838.         $self->{was_uncompressed}++ unless CPAN::Tarzip->gtest($local_file);
  3839.     $self->untar_me($local_file);
  3840.     } elsif ( $local_file =~ /\.zip(?!\n)\Z/i ) {
  3841.     $self->unzip_me($local_file);
  3842.     } elsif ( $local_file =~ /\.pm\.(gz|Z)(?!\n)\Z/) {
  3843.         $self->{was_uncompressed}++ unless CPAN::Tarzip->gtest($local_file);
  3844.     $self->pm2dir_me($local_file);
  3845.     } else {
  3846.     $self->{archived} = "NO";
  3847.         $self->safe_chdir($sub_wd);
  3848.         return;
  3849.     }
  3850.  
  3851.     # we are still in the tmp directory!
  3852.     # Let's check if the package has its own directory.
  3853.     my $dh = DirHandle->new(File::Spec->curdir)
  3854.         or Carp::croak("Couldn't opendir .: $!");
  3855.     my @readdir = grep $_ !~ /^\.\.?(?!\n)\Z/s, $dh->read; ### MAC??
  3856.     $dh->close;
  3857.     my ($distdir,$packagedir);
  3858.     if (@readdir == 1 && -d $readdir[0]) {
  3859.         $distdir = $readdir[0];
  3860.         $packagedir = File::Spec->catdir($builddir,$distdir);
  3861.         $self->debug("packagedir[$packagedir]builddir[$builddir]distdir[$distdir]")
  3862.             if $CPAN::DEBUG;
  3863.         -d $packagedir and $CPAN::Frontend->myprint("Removing previously used ".
  3864.                                                     "$packagedir\n");
  3865.         File::Path::rmtree($packagedir);
  3866.         rename($distdir,$packagedir) or
  3867.             Carp::confess("Couldn't rename $distdir to $packagedir: $!");
  3868.         $self->debug(sprintf("renamed distdir[%s] to packagedir[%s] -e[%s]-d[%s]",
  3869.                              $distdir,
  3870.                              $packagedir,
  3871.                              -e $packagedir,
  3872.                              -d $packagedir,
  3873.                             )) if $CPAN::DEBUG;
  3874.     } else {
  3875.         my $userid = $self->cpan_userid;
  3876.         unless ($userid) {
  3877.             CPAN->debug("no userid? self[$self]");
  3878.             $userid = "anon";
  3879.         }
  3880.         my $pragmatic_dir = $userid . '000';
  3881.         $pragmatic_dir =~ s/\W_//g;
  3882.         $pragmatic_dir++ while -d "../$pragmatic_dir";
  3883.         $packagedir = File::Spec->catdir($builddir,$pragmatic_dir);
  3884.         $self->debug("packagedir[$packagedir]") if $CPAN::DEBUG;
  3885.         File::Path::mkpath($packagedir);
  3886.         my($f);
  3887.         for $f (@readdir) { # is already without "." and ".."
  3888.             my $to = File::Spec->catdir($packagedir,$f);
  3889.             rename($f,$to) or Carp::confess("Couldn't rename $f to $to: $!");
  3890.         }
  3891.     }
  3892.     if ($CPAN::Signal){
  3893.         $self->safe_chdir($sub_wd);
  3894.         return;
  3895.     }
  3896.  
  3897.     $self->{'build_dir'} = $packagedir;
  3898.     $self->safe_chdir(File::Spec->updir);
  3899.     File::Path::rmtree("tmp");
  3900.  
  3901.     my($mpl) = File::Spec->catfile($packagedir,"Makefile.PL");
  3902.     my($mpl_exists) = -f $mpl;
  3903.     unless ($mpl_exists) {
  3904.         # NFS has been reported to have racing problems after the
  3905.         # renaming of a directory in some environments.
  3906.         # This trick helps.
  3907.         sleep 1;
  3908.         my $mpldh = DirHandle->new($packagedir)
  3909.             or Carp::croak("Couldn't opendir $packagedir: $!");
  3910.         $mpl_exists = grep /^Makefile\.PL$/, $mpldh->read;
  3911.         $mpldh->close;
  3912.     }
  3913.     unless ($mpl_exists) {
  3914.         $self->debug(sprintf("makefilepl[%s]anycwd[%s]",
  3915.                              $mpl,
  3916.                              CPAN::anycwd(),
  3917.                             )) if $CPAN::DEBUG;
  3918.         my($configure) = File::Spec->catfile($packagedir,"Configure");
  3919.         if (-f $configure) {
  3920.             # do we have anything to do?
  3921.             $self->{'configure'} = $configure;
  3922.         } elsif (-f File::Spec->catfile($packagedir,"Makefile")) {
  3923.             $CPAN::Frontend->myprint(qq{
  3924. Package comes with a Makefile and without a Makefile.PL.
  3925. We\'ll try to build it with that Makefile then.
  3926. });
  3927.             $self->{writemakefile} = "YES";
  3928.             sleep 2;
  3929.         } else {
  3930.             my $cf = $self->called_for || "unknown";
  3931.             if ($cf =~ m|/|) {
  3932.                 $cf =~ s|.*/||;
  3933.                 $cf =~ s|\W.*||;
  3934.             }
  3935.             $cf =~ s|[/\\:]||g; # risk of filesystem damage
  3936.             $cf = "unknown" unless length($cf);
  3937.             $CPAN::Frontend->myprint(qq{Package seems to come without Makefile.PL.
  3938.   (The test -f "$mpl" returned false.)
  3939.   Writing one on our own (setting NAME to $cf)\a\n});
  3940.             $self->{had_no_makefile_pl}++;
  3941.             sleep 3;
  3942.  
  3943.             # Writing our own Makefile.PL
  3944.  
  3945.             my $fh = FileHandle->new;
  3946.             $fh->open(">$mpl")
  3947.                 or Carp::croak("Could not open >$mpl: $!");
  3948.             $fh->print(
  3949. qq{# This Makefile.PL has been autogenerated by the module CPAN.pm
  3950. # because there was no Makefile.PL supplied.
  3951. # Autogenerated on: }.scalar localtime().qq{
  3952.  
  3953. use ExtUtils::MakeMaker;
  3954. WriteMakefile(NAME => q[$cf]);
  3955.  
  3956. });
  3957.             $fh->close;
  3958.         }
  3959.     }
  3960.  
  3961.     return $self;
  3962. }
  3963.  
  3964. # CPAN::Distribution::untar_me ;
  3965. sub untar_me {
  3966.     my($self,$local_file) = @_;
  3967.     $self->{archived} = "tar";
  3968.     if (CPAN::Tarzip->untar($local_file)) {
  3969.     $self->{unwrapped} = "YES";
  3970.     } else {
  3971.     $self->{unwrapped} = "NO";
  3972.     }
  3973. }
  3974.  
  3975. # CPAN::Distribution::unzip_me ;
  3976. sub unzip_me {
  3977.     my($self,$local_file) = @_;
  3978.     $self->{archived} = "zip";
  3979.     if (CPAN::Tarzip->unzip($local_file)) {
  3980.     $self->{unwrapped} = "YES";
  3981.     } else {
  3982.     $self->{unwrapped} = "NO";
  3983.     }
  3984.     return;
  3985. }
  3986.  
  3987. sub pm2dir_me {
  3988.     my($self,$local_file) = @_;
  3989.     $self->{archived} = "pm";
  3990.     my $to = File::Basename::basename($local_file);
  3991.     $to =~ s/\.(gz|Z)(?!\n)\Z//;
  3992.     if (CPAN::Tarzip->gunzip($local_file,$to)) {
  3993.     $self->{unwrapped} = "YES";
  3994.     } else {
  3995.     $self->{unwrapped} = "NO";
  3996.     }
  3997. }
  3998.  
  3999. #-> sub CPAN::Distribution::new ;
  4000. sub new {
  4001.     my($class,%att) = @_;
  4002.  
  4003.     # $CPAN::META->{cachemgr} ||= CPAN::CacheMgr->new();
  4004.  
  4005.     my $this = { %att };
  4006.     return bless $this, $class;
  4007. }
  4008.  
  4009. #-> sub CPAN::Distribution::look ;
  4010. sub look {
  4011.     my($self) = @_;
  4012.  
  4013.     if ($^O eq 'MacOS') {
  4014.       $self->Mac::BuildTools::look;
  4015.       return;
  4016.     }
  4017.  
  4018.     if (  $CPAN::Config->{'shell'} ) {
  4019.     $CPAN::Frontend->myprint(qq{
  4020. Trying to open a subshell in the build directory...
  4021. });
  4022.     } else {
  4023.     $CPAN::Frontend->myprint(qq{
  4024. Your configuration does not define a value for subshells.
  4025. Please define it with "o conf shell <your shell>"
  4026. });
  4027.     return;
  4028.     }
  4029.     my $dist = $self->id;
  4030.     my $dir;
  4031.     unless ($dir = $self->dir) {
  4032.         $self->get;
  4033.     }
  4034.     unless ($dir ||= $self->dir) {
  4035.     $CPAN::Frontend->mywarn(qq{
  4036. Could not determine which directory to use for looking at $dist.
  4037. });
  4038.     return;
  4039.     }
  4040.     my $pwd  = CPAN::anycwd();
  4041.     $self->safe_chdir($dir);
  4042.     $CPAN::Frontend->myprint(qq{Working directory is $dir\n});
  4043.     system($CPAN::Config->{'shell'}) == 0
  4044.     or $CPAN::Frontend->mydie("Subprocess shell error");
  4045.     $self->safe_chdir($pwd);
  4046. }
  4047.  
  4048. # CPAN::Distribution::cvs_import ;
  4049. sub cvs_import {
  4050.     my($self) = @_;
  4051.     $self->get;
  4052.     my $dir = $self->dir;
  4053.  
  4054.     my $package = $self->called_for;
  4055.     my $module = $CPAN::META->instance('CPAN::Module', $package);
  4056.     my $version = $module->cpan_version;
  4057.  
  4058.     my $userid = $self->cpan_userid;
  4059.  
  4060.     my $cvs_dir = (split '/', $dir)[-1];
  4061.     $cvs_dir =~ s/-\d+[^-]+(?!\n)\Z//;
  4062.     my $cvs_root = 
  4063.       $CPAN::Config->{cvsroot} || $ENV{CVSROOT};
  4064.     my $cvs_site_perl = 
  4065.       $CPAN::Config->{cvs_site_perl} || $ENV{CVS_SITE_PERL};
  4066.     if ($cvs_site_perl) {
  4067.     $cvs_dir = "$cvs_site_perl/$cvs_dir";
  4068.     }
  4069.     my $cvs_log = qq{"imported $package $version sources"};
  4070.     $version =~ s/\./_/g;
  4071.     my @cmd = ('cvs', '-d', $cvs_root, 'import', '-m', $cvs_log,
  4072.            "$cvs_dir", $userid, "v$version");
  4073.  
  4074.     my $pwd  = CPAN::anycwd();
  4075.     chdir($dir) or $CPAN::Frontend->mydie(qq{Could not chdir to "$dir": $!});
  4076.  
  4077.     $CPAN::Frontend->myprint(qq{Working directory is $dir\n});
  4078.  
  4079.     $CPAN::Frontend->myprint(qq{@cmd\n});
  4080.     system(@cmd) == 0 or
  4081.     $CPAN::Frontend->mydie("cvs import failed");
  4082.     chdir($pwd) or $CPAN::Frontend->mydie(qq{Could not chdir to "$pwd": $!});
  4083. }
  4084.  
  4085. #-> sub CPAN::Distribution::readme ;
  4086. sub readme {
  4087.     my($self) = @_;
  4088.     my($dist) = $self->id;
  4089.     my($sans,$suffix) = $dist =~ /(.+)\.(tgz|tar[\._-]gz|tar\.Z|zip)$/;
  4090.     $self->debug("sans[$sans] suffix[$suffix]\n") if $CPAN::DEBUG;
  4091.     my($local_file);
  4092.     my($local_wanted) =
  4093.      File::Spec->catfile(
  4094.                  $CPAN::Config->{keep_source_where},
  4095.                  "authors",
  4096.                  "id",
  4097.                  split("/","$sans.readme"),
  4098.                 );
  4099.     $self->debug("Doing localize") if $CPAN::DEBUG;
  4100.     $local_file = CPAN::FTP->localize("authors/id/$sans.readme",
  4101.                       $local_wanted)
  4102.     or $CPAN::Frontend->mydie(qq{No $sans.readme found});;
  4103.  
  4104.     if ($^O eq 'MacOS') {
  4105.         Mac::BuildTools::launch_file($local_file);
  4106.         return;
  4107.     }
  4108.  
  4109.     my $fh_pager = FileHandle->new;
  4110.     local($SIG{PIPE}) = "IGNORE";
  4111.     $fh_pager->open("|$CPAN::Config->{'pager'}")
  4112.     or die "Could not open pager $CPAN::Config->{'pager'}: $!";
  4113.     my $fh_readme = FileHandle->new;
  4114.     $fh_readme->open($local_file)
  4115.     or $CPAN::Frontend->mydie(qq{Could not open "$local_file": $!});
  4116.     $CPAN::Frontend->myprint(qq{
  4117. Displaying file
  4118.   $local_file
  4119. with pager "$CPAN::Config->{'pager'}"
  4120. });
  4121.     sleep 2;
  4122.     $fh_pager->print(<$fh_readme>);
  4123. }
  4124.  
  4125. #-> sub CPAN::Distribution::verifyMD5 ;
  4126. sub verifyMD5 {
  4127.     my($self) = @_;
  4128.   EXCUSE: {
  4129.     my @e;
  4130.     $self->{MD5_STATUS} ||= "";
  4131.     $self->{MD5_STATUS} eq "OK" and push @e, "MD5 Checksum was ok";
  4132.     $CPAN::Frontend->myprint(join "", map {"  $_\n"} @e) and return if @e;
  4133.     }
  4134.     my($lc_want,$lc_file,@local,$basename);
  4135.     @local = split("/",$self->id);
  4136.     pop @local;
  4137.     push @local, "CHECKSUMS";
  4138.     $lc_want =
  4139.     File::Spec->catfile($CPAN::Config->{keep_source_where},
  4140.                 "authors", "id", @local);
  4141.     local($") = "/";
  4142.     if (
  4143.     -s $lc_want
  4144.     &&
  4145.     $self->MD5_check_file($lc_want)
  4146.        ) {
  4147.     return $self->{MD5_STATUS} = "OK";
  4148.     }
  4149.     $lc_file = CPAN::FTP->localize("authors/id/@local",
  4150.                    $lc_want,1);
  4151.     unless ($lc_file) {
  4152.         $CPAN::Frontend->myprint("Trying $lc_want.gz\n");
  4153.     $local[-1] .= ".gz";
  4154.     $lc_file = CPAN::FTP->localize("authors/id/@local",
  4155.                        "$lc_want.gz",1);
  4156.     if ($lc_file) {
  4157.         $lc_file =~ s/\.gz(?!\n)\Z//;
  4158.         CPAN::Tarzip->gunzip("$lc_file.gz",$lc_file);
  4159.     } else {
  4160.         return;
  4161.     }
  4162.     }
  4163.     $self->MD5_check_file($lc_file);
  4164. }
  4165.  
  4166. #-> sub CPAN::Distribution::MD5_check_file ;
  4167. sub MD5_check_file {
  4168.     my($self,$chk_file) = @_;
  4169.     my($cksum,$file,$basename);
  4170.     $file = $self->{localfile};
  4171.     $basename = File::Basename::basename($file);
  4172.     my $fh = FileHandle->new;
  4173.     if (open $fh, $chk_file){
  4174.     local($/);
  4175.     my $eval = <$fh>;
  4176.     $eval =~ s/\015?\012/\n/g;
  4177.     close $fh;
  4178.     my($comp) = Safe->new();
  4179.     $cksum = $comp->reval($eval);
  4180.     if ($@) {
  4181.         rename $chk_file, "$chk_file.bad";
  4182.         Carp::confess($@) if $@;
  4183.     }
  4184.     } else {
  4185.     Carp::carp "Could not open $chk_file for reading";
  4186.     }
  4187.  
  4188.     if (exists $cksum->{$basename}{md5}) {
  4189.     $self->debug("Found checksum for $basename:" .
  4190.              "$cksum->{$basename}{md5}\n") if $CPAN::DEBUG;
  4191.  
  4192.     open($fh, $file);
  4193.     binmode $fh;
  4194.     my $eq = $self->eq_MD5($fh,$cksum->{$basename}{'md5'});
  4195.     $fh->close;
  4196.     $fh = CPAN::Tarzip->TIEHANDLE($file);
  4197.  
  4198.     unless ($eq) {
  4199.       # had to inline it, when I tied it, the tiedness got lost on
  4200.       # the call to eq_MD5. (Jan 1998)
  4201.       my $md5 = Digest::MD5->new;
  4202.       my($data,$ref);
  4203.       $ref = \$data;
  4204.       while ($fh->READ($ref, 4096) > 0){
  4205.         $md5->add($data);
  4206.       }
  4207.       my $hexdigest = $md5->hexdigest;
  4208.       $eq += $hexdigest eq $cksum->{$basename}{'md5-ungz'};
  4209.     }
  4210.  
  4211.     if ($eq) {
  4212.       $CPAN::Frontend->myprint("Checksum for $file ok\n");
  4213.       return $self->{MD5_STATUS} = "OK";
  4214.     } else {
  4215.         $CPAN::Frontend->myprint(qq{\nChecksum mismatch for }.
  4216.                      qq{distribution file. }.
  4217.                      qq{Please investigate.\n\n}.
  4218.                      $self->as_string,
  4219.                      $CPAN::META->instance(
  4220.                                'CPAN::Author',
  4221.                                $self->cpan_userid
  4222.                               )->as_string);
  4223.  
  4224.         my $wrap = qq{I\'d recommend removing $file. Its MD5
  4225. checksum is incorrect. Maybe you have configured your 'urllist' with
  4226. a bad URL. Please check this array with 'o conf urllist', and
  4227. retry.};
  4228.  
  4229.             $CPAN::Frontend->mydie(Text::Wrap::wrap("","",$wrap));
  4230.  
  4231.             # former versions just returned here but this seems a
  4232.             # serious threat that deserves a die
  4233.  
  4234.         # $CPAN::Frontend->myprint("\n\n");
  4235.         # sleep 3;
  4236.         # return;
  4237.     }
  4238.     # close $fh if fileno($fh);
  4239.     } else {
  4240.     $self->{MD5_STATUS} ||= "";
  4241.     if ($self->{MD5_STATUS} eq "NIL") {
  4242.         $CPAN::Frontend->mywarn(qq{
  4243. Warning: No md5 checksum for $basename in $chk_file.
  4244.  
  4245. The cause for this may be that the file is very new and the checksum
  4246. has not yet been calculated, but it may also be that something is
  4247. going awry right now.
  4248. });
  4249.             my $answer = ExtUtils::MakeMaker::prompt("Proceed?", "yes");
  4250.             $answer =~ /^\s*y/i or $CPAN::Frontend->mydie("Aborted.");
  4251.     }
  4252.     $self->{MD5_STATUS} = "NIL";
  4253.     return;
  4254.     }
  4255. }
  4256.  
  4257. #-> sub CPAN::Distribution::eq_MD5 ;
  4258. sub eq_MD5 {
  4259.     my($self,$fh,$expectMD5) = @_;
  4260.     my $md5 = Digest::MD5->new;
  4261.     my($data);
  4262.     while (read($fh, $data, 4096)){
  4263.       $md5->add($data);
  4264.     }
  4265.     # $md5->addfile($fh);
  4266.     my $hexdigest = $md5->hexdigest;
  4267.     # warn "fh[$fh] hex[$hexdigest] aexp[$expectMD5]";
  4268.     $hexdigest eq $expectMD5;
  4269. }
  4270.  
  4271. #-> sub CPAN::Distribution::force ;
  4272.  
  4273. # Both modules and distributions know if "force" is in effect by
  4274. # autoinspection, not by inspecting a global variable. One of the
  4275. # reason why this was chosen to work that way was the treatment of
  4276. # dependencies. They should not autpomatically inherit the force
  4277. # status. But this has the downside that ^C and die() will return to
  4278. # the prompt but will not be able to reset the force_update
  4279. # attributes. We try to correct for it currently in the read_metadata
  4280. # routine, and immediately before we check for a Signal. I hope this
  4281. # works out in one of v1.57_53ff
  4282.  
  4283. sub force {
  4284.   my($self, $method) = @_;
  4285.   for my $att (qw(
  4286.   MD5_STATUS archived build_dir localfile make install unwrapped
  4287.   writemakefile
  4288.  )) {
  4289.     delete $self->{$att};
  4290.   }
  4291.   if ($method && $method eq "install") {
  4292.     $self->{"force_update"}++; # name should probably have been force_install
  4293.   }
  4294. }
  4295.  
  4296. #-> sub CPAN::Distribution::unforce ;
  4297. sub unforce {
  4298.   my($self) = @_;
  4299.   delete $self->{'force_update'};
  4300. }
  4301.  
  4302. #-> sub CPAN::Distribution::isa_perl ;
  4303. sub isa_perl {
  4304.   my($self) = @_;
  4305.   my $file = File::Basename::basename($self->id);
  4306.   if ($file =~ m{ ^ perl
  4307.                   -?
  4308.           (5)
  4309.           ([._-])
  4310.           (
  4311.                    \d{3}(_[0-4][0-9])?
  4312.                    |
  4313.                    \d*[24680]\.\d+
  4314.                   )
  4315.           \.tar[._-]gz
  4316.           (?!\n)\Z
  4317.         }xs){
  4318.     return "$1.$3";
  4319.   } elsif ($self->cpan_comment
  4320.            &&
  4321.            $self->cpan_comment =~ /isa_perl\(.+?\)/){
  4322.     return $1;
  4323.   }
  4324. }
  4325.  
  4326. #-> sub CPAN::Distribution::perl ;
  4327. sub perl {
  4328.     my($self) = @_;
  4329.     my($perl) = File::Spec->file_name_is_absolute($^X) ? $^X : "";
  4330.     my $pwd  = CPAN::anycwd();
  4331.     my $candidate = File::Spec->catfile($pwd,$^X);
  4332.     $perl ||= $candidate if MM->maybe_command($candidate);
  4333.     unless ($perl) {
  4334.     my ($component,$perl_name);
  4335.       DIST_PERLNAME: foreach $perl_name ($^X, 'perl', 'perl5', "perl$]") {
  4336.         PATH_COMPONENT: foreach $component (File::Spec->path(),
  4337.                         $Config::Config{'binexp'}) {
  4338.           next unless defined($component) && $component;
  4339.           my($abs) = File::Spec->catfile($component,$perl_name);
  4340.           if (MM->maybe_command($abs)) {
  4341.               $perl = $abs;
  4342.               last DIST_PERLNAME;
  4343.           }
  4344.           }
  4345.       }
  4346.     }
  4347.     $perl;
  4348. }
  4349.  
  4350. #-> sub CPAN::Distribution::make ;
  4351. sub make {
  4352.     my($self) = @_;
  4353.     $CPAN::Frontend->myprint(sprintf "Running make for %s\n", $self->id);
  4354.     # Emergency brake if they said install Pippi and get newest perl
  4355.     if ($self->isa_perl) {
  4356.       if (
  4357.       $self->called_for ne $self->id &&
  4358.           ! $self->{force_update}
  4359.      ) {
  4360.         # if we die here, we break bundles
  4361.     $CPAN::Frontend->mywarn(sprintf qq{
  4362. The most recent version "%s" of the module "%s"
  4363. comes with the current version of perl (%s).
  4364. I\'ll build that only if you ask for something like
  4365.     force install %s
  4366. or
  4367.     install %s
  4368. },
  4369.                    $CPAN::META->instance(
  4370.                              'CPAN::Module',
  4371.                              $self->called_for
  4372.                             )->cpan_version,
  4373.                    $self->called_for,
  4374.                    $self->isa_perl,
  4375.                    $self->called_for,
  4376.                    $self->id);
  4377.         sleep 5; return;
  4378.       }
  4379.     }
  4380.     $self->get;
  4381.   EXCUSE: {
  4382.     my @e;
  4383.     $self->{archived} eq "NO" and push @e,
  4384.     "Is neither a tar nor a zip archive.";
  4385.  
  4386.     $self->{unwrapped} eq "NO" and push @e,
  4387.     "had problems unarchiving. Please build manually";
  4388.  
  4389.     exists $self->{writemakefile} &&
  4390.         $self->{writemakefile} =~ m/ ^ NO\s* ( .* ) /sx and push @e,
  4391.         $1 || "Had some problem writing Makefile";
  4392.  
  4393.     defined $self->{'make'} and push @e,
  4394.             "Has already been processed within this session";
  4395.  
  4396.         exists $self->{later} and length($self->{later}) and
  4397.             push @e, $self->{later};
  4398.  
  4399.     $CPAN::Frontend->myprint(join "", map {"  $_\n"} @e) and return if @e;
  4400.     }
  4401.     $CPAN::Frontend->myprint("\n  CPAN.pm: Going to build ".$self->id."\n\n");
  4402.     my $builddir = $self->dir;
  4403.     chdir $builddir or Carp::croak("Couldn't chdir $builddir: $!");
  4404.     $self->debug("Changed directory to $builddir") if $CPAN::DEBUG;
  4405.  
  4406.     if ($^O eq 'MacOS') {
  4407.         Mac::BuildTools::make($self);
  4408.         return;
  4409.     }
  4410.  
  4411.     my $system;
  4412.     if ($self->{'configure'}) {
  4413.       $system = $self->{'configure'};
  4414.     } else {
  4415.     my($perl) = $self->perl or die "Couldn\'t find executable perl\n";
  4416.     my $switch = "";
  4417. # This needs a handler that can be turned on or off:
  4418. #    $switch = "-MExtUtils::MakeMaker ".
  4419. #        "-Mops=:default,:filesys_read,:filesys_open,require,chdir"
  4420. #        if $] > 5.00310;
  4421.     $system = "$perl $switch Makefile.PL $CPAN::Config->{makepl_arg}";
  4422.     }
  4423.     unless (exists $self->{writemakefile}) {
  4424.     local($SIG{ALRM}) = sub { die "inactivity_timeout reached\n" };
  4425.     my($ret,$pid);
  4426.     $@ = "";
  4427.     if ($CPAN::Config->{inactivity_timeout}) {
  4428.         eval {
  4429.         alarm $CPAN::Config->{inactivity_timeout};
  4430.         local $SIG{CHLD}; # = sub { wait };
  4431.         if (defined($pid = fork)) {
  4432.             if ($pid) { #parent
  4433.             # wait;
  4434.             waitpid $pid, 0;
  4435.             } else {    #child
  4436.               # note, this exec isn't necessary if
  4437.               # inactivity_timeout is 0. On the Mac I'd
  4438.               # suggest, we set it always to 0.
  4439.               exec $system;
  4440.             }
  4441.         } else {
  4442.             $CPAN::Frontend->myprint("Cannot fork: $!");
  4443.             return;
  4444.         }
  4445.         };
  4446.         alarm 0;
  4447.         if ($@){
  4448.         kill 9, $pid;
  4449.         waitpid $pid, 0;
  4450.         $CPAN::Frontend->myprint($@);
  4451.         $self->{writemakefile} = "NO $@";
  4452.         $@ = "";
  4453.         return;
  4454.         }
  4455.     } else {
  4456.       $ret = system($system);
  4457.       if ($ret != 0) {
  4458.         $self->{writemakefile} = "NO Makefile.PL returned status $ret";
  4459.         return;
  4460.       }
  4461.     }
  4462.     if (-f "Makefile") {
  4463.       $self->{writemakefile} = "YES";
  4464.           delete $self->{make_clean}; # if cleaned before, enable next
  4465.     } else {
  4466.       $self->{writemakefile} =
  4467.           qq{NO Makefile.PL refused to write a Makefile.};
  4468.       # It's probably worth it to record the reason, so let's retry
  4469.       # local $/;
  4470.       # my $fh = IO::File->new("$system |"); # STDERR? STDIN?
  4471.       # $self->{writemakefile} .= <$fh>;
  4472.     }
  4473.     }
  4474.     if ($CPAN::Signal){
  4475.       delete $self->{force_update};
  4476.       return;
  4477.     }
  4478.     if (my @prereq = $self->unsat_prereq){
  4479.       return 1 if $self->follow_prereqs(@prereq); # signal success to the queuerunner
  4480.     }
  4481.     $system = join " ", $CPAN::Config->{'make'}, $CPAN::Config->{make_arg};
  4482.     if (system($system) == 0) {
  4483.      $CPAN::Frontend->myprint("  $system -- OK\n");
  4484.      $self->{'make'} = "YES";
  4485.     } else {
  4486.      $self->{writemakefile} ||= "YES";
  4487.      $self->{'make'} = "NO";
  4488.      $CPAN::Frontend->myprint("  $system -- NOT OK\n");
  4489.     }
  4490. }
  4491.  
  4492. sub follow_prereqs {
  4493.     my($self) = shift;
  4494.     my(@prereq) = @_;
  4495.     my $id = $self->id;
  4496.     $CPAN::Frontend->myprint("---- Unsatisfied dependencies detected ".
  4497.                              "during [$id] -----\n");
  4498.  
  4499.     for my $p (@prereq) {
  4500.     $CPAN::Frontend->myprint("    $p\n");
  4501.     }
  4502.     my $follow = 0;
  4503.     if ($CPAN::Config->{prerequisites_policy} eq "follow") {
  4504.     $follow = 1;
  4505.     } elsif ($CPAN::Config->{prerequisites_policy} eq "ask") {
  4506.     require ExtUtils::MakeMaker;
  4507.     my $answer = ExtUtils::MakeMaker::prompt(
  4508. "Shall I follow them and prepend them to the queue
  4509. of modules we are processing right now?", "yes");
  4510.     $follow = $answer =~ /^\s*y/i;
  4511.     } else {
  4512.     local($") = ", ";
  4513.     $CPAN::Frontend->
  4514.             myprint("  Ignoring dependencies on modules @prereq\n");
  4515.     }
  4516.     if ($follow) {
  4517.         # color them as dirty
  4518.         for my $p (@prereq) {
  4519.             CPAN::Shell->expandany($p)->color_cmd_tmps(0,1);
  4520.         }
  4521.         CPAN::Queue->jumpqueue(@prereq,$id); # queue them and requeue yourself
  4522.         $self->{later} = "Delayed until after prerequisites";
  4523.         return 1; # signal success to the queuerunner
  4524.     }
  4525. }
  4526.  
  4527. #-> sub CPAN::Distribution::unsat_prereq ;
  4528. sub unsat_prereq {
  4529.     my($self) = @_;
  4530.     my $prereq_pm = $self->prereq_pm or return;
  4531.     my(@need);
  4532.   NEED: while (my($need_module, $need_version) = each %$prereq_pm) {
  4533.         my $nmo = $CPAN::META->instance("CPAN::Module",$need_module);
  4534.         # we were too demanding:
  4535.         next if $nmo->uptodate;
  4536.  
  4537.         # if they have not specified a version, we accept any installed one
  4538.         if (not defined $need_version or
  4539.            $need_version == 0 or
  4540.            $need_version eq "undef") {
  4541.             next if defined $nmo->inst_file;
  4542.         }
  4543.  
  4544.         # We only want to install prereqs if either they're not installed
  4545.         # or if the installed version is too old. We cannot omit this
  4546.         # check, because if 'force' is in effect, nobody else will check.
  4547.         {
  4548.             local($^W) = 0;
  4549.             if (
  4550.                 defined $nmo->inst_file &&
  4551.                 ! CPAN::Version->vgt($need_version, $nmo->inst_version)
  4552.                ){
  4553.                 CPAN->debug(sprintf "id[%s]inst_file[%s]inst_version[%s]need_version[%s]",
  4554.                             $nmo->id,
  4555.                             $nmo->inst_file,
  4556.                             $nmo->inst_version,
  4557.                             CPAN::Version->readable($need_version)
  4558.                            );
  4559.                 next NEED;
  4560.             }
  4561.         }
  4562.  
  4563.         if ($self->{sponsored_mods}{$need_module}++){
  4564.             # We have already sponsored it and for some reason it's still
  4565.             # not available. So we do nothing. Or what should we do?
  4566.             # if we push it again, we have a potential infinite loop
  4567.             next;
  4568.         }
  4569.         push @need, $need_module;
  4570.     }
  4571.     @need;
  4572. }
  4573.  
  4574. #-> sub CPAN::Distribution::prereq_pm ;
  4575. sub prereq_pm {
  4576.   my($self) = @_;
  4577.   return $self->{prereq_pm} if
  4578.       exists $self->{prereq_pm_detected} && $self->{prereq_pm_detected};
  4579.   return unless $self->{writemakefile}; # no need to have succeeded
  4580.                                         # but we must have run it
  4581.   my $build_dir = $self->{build_dir} or die "Panic: no build_dir?";
  4582.   my $makefile = File::Spec->catfile($build_dir,"Makefile");
  4583.   my(%p) = ();
  4584.   my $fh;
  4585.   if (-f $makefile
  4586.       and
  4587.       $fh = FileHandle->new("<$makefile\0")) {
  4588.  
  4589.       local($/) = "\n";
  4590.  
  4591.       #  A.Speer @p -> %p, where %p is $p{Module::Name}=Required_Version
  4592.       while (<$fh>) {
  4593.           last if /MakeMaker post_initialize section/;
  4594.           my($p) = m{^[\#]
  4595.          \s+PREREQ_PM\s+=>\s+(.+)
  4596.          }x;
  4597.           next unless $p;
  4598.           # warn "Found prereq expr[$p]";
  4599.  
  4600.           #  Regexp modified by A.Speer to remember actual version of file
  4601.           #  PREREQ_PM hash key wants, then add to
  4602.           while ( $p =~ m/(?:\s)([\w\:]+)=>q\[(.*?)\],?/g ){
  4603.               # In case a prereq is mentioned twice, complain.
  4604.               if ( defined $p{$1} ) {
  4605.                   warn "Warning: PREREQ_PM mentions $1 more than once, last mention wins";
  4606.               }
  4607.               $p{$1} = $2;
  4608.           }
  4609.           last;
  4610.       }
  4611.   }
  4612.   $self->{prereq_pm_detected}++;
  4613.   return $self->{prereq_pm} = \%p;
  4614. }
  4615.  
  4616. #-> sub CPAN::Distribution::test ;
  4617. sub test {
  4618.     my($self) = @_;
  4619.     $self->make;
  4620.     if ($CPAN::Signal){
  4621.       delete $self->{force_update};
  4622.       return;
  4623.     }
  4624.     $CPAN::Frontend->myprint("Running make test\n");
  4625.     if (my @prereq = $self->unsat_prereq){
  4626.       return 1 if $self->follow_prereqs(@prereq); # signal success to the queuerunner
  4627.     }
  4628.   EXCUSE: {
  4629.     my @e;
  4630.     exists $self->{make} or exists $self->{later} or push @e,
  4631.     "Make had some problems, maybe interrupted? Won't test";
  4632.  
  4633.     exists $self->{'make'} and
  4634.         $self->{'make'} eq 'NO' and
  4635.         push @e, "Can't test without successful make";
  4636.  
  4637.     exists $self->{build_dir} or push @e, "Has no own directory";
  4638.         $self->{badtestcnt} ||= 0;
  4639.         $self->{badtestcnt} > 0 and
  4640.             push @e, "Won't repeat unsuccessful test during this command";
  4641.  
  4642.         exists $self->{later} and length($self->{later}) and
  4643.             push @e, $self->{later};
  4644.  
  4645.     $CPAN::Frontend->myprint(join "", map {"  $_\n"} @e) and return if @e;
  4646.     }
  4647.     chdir $self->{'build_dir'} or
  4648.     Carp::croak("Couldn't chdir to $self->{'build_dir'}");
  4649.     $self->debug("Changed directory to $self->{'build_dir'}")
  4650.     if $CPAN::DEBUG;
  4651.  
  4652.     if ($^O eq 'MacOS') {
  4653.         Mac::BuildTools::make_test($self);
  4654.         return;
  4655.     }
  4656.  
  4657.     local $ENV{PERL5LIB} = $ENV{PERL5LIB} || "";
  4658.     $CPAN::META->set_perl5lib;
  4659.     my $system = join " ", $CPAN::Config->{'make'}, "test";
  4660.     if (system($system) == 0) {
  4661.      $CPAN::Frontend->myprint("  $system -- OK\n");
  4662.      $CPAN::META->is_tested($self->{'build_dir'});
  4663.      $self->{make_test} = "YES";
  4664.     } else {
  4665.      $self->{make_test} = "NO";
  4666.          $self->{badtestcnt}++;
  4667.      $CPAN::Frontend->myprint("  $system -- NOT OK\n");
  4668.     }
  4669. }
  4670.  
  4671. #-> sub CPAN::Distribution::clean ;
  4672. sub clean {
  4673.     my($self) = @_;
  4674.     $CPAN::Frontend->myprint("Running make clean\n");
  4675.   EXCUSE: {
  4676.     my @e;
  4677.         exists $self->{make_clean} and $self->{make_clean} eq "YES" and
  4678.             push @e, "make clean already called once";
  4679.     exists $self->{build_dir} or push @e, "Has no own directory";
  4680.     $CPAN::Frontend->myprint(join "", map {"  $_\n"} @e) and return if @e;
  4681.     }
  4682.     chdir $self->{'build_dir'} or
  4683.     Carp::croak("Couldn't chdir to $self->{'build_dir'}");
  4684.     $self->debug("Changed directory to $self->{'build_dir'}") if $CPAN::DEBUG;
  4685.  
  4686.     if ($^O eq 'MacOS') {
  4687.         Mac::BuildTools::make_clean($self);
  4688.         return;
  4689.     }
  4690.  
  4691.     my $system = join " ", $CPAN::Config->{'make'}, "clean";
  4692.     if (system($system) == 0) {
  4693.       $CPAN::Frontend->myprint("  $system -- OK\n");
  4694.  
  4695.       # $self->force;
  4696.  
  4697.       # Jost Krieger pointed out that this "force" was wrong because
  4698.       # it has the effect that the next "install" on this distribution
  4699.       # will untar everything again. Instead we should bring the
  4700.       # object's state back to where it is after untarring.
  4701.  
  4702.       delete $self->{force_update};
  4703.       delete $self->{install};
  4704.       delete $self->{writemakefile};
  4705.       delete $self->{make};
  4706.       delete $self->{make_test}; # no matter if yes or no, tests must be redone
  4707.       $self->{make_clean} = "YES";
  4708.  
  4709.     } else {
  4710.       # Hmmm, what to do if make clean failed?
  4711.  
  4712.       $CPAN::Frontend->myprint(qq{  $system -- NOT OK
  4713.  
  4714. make clean did not succeed, marking directory as unusable for further work.
  4715. });
  4716.       $self->force("make"); # so that this directory won't be used again
  4717.  
  4718.     }
  4719. }
  4720.  
  4721. #-> sub CPAN::Distribution::install ;
  4722. sub install {
  4723.     my($self) = @_;
  4724.     $self->test;
  4725.     if ($CPAN::Signal){
  4726.       delete $self->{force_update};
  4727.       return;
  4728.     }
  4729.     $CPAN::Frontend->myprint("Running make install\n");
  4730.   EXCUSE: {
  4731.     my @e;
  4732.     exists $self->{build_dir} or push @e, "Has no own directory";
  4733.  
  4734.     exists $self->{make} or exists $self->{later} or push @e,
  4735.     "Make had some problems, maybe interrupted? Won't install";
  4736.  
  4737.     exists $self->{'make'} and
  4738.         $self->{'make'} eq 'NO' and
  4739.         push @e, "make had returned bad status, install seems impossible";
  4740.  
  4741.     push @e, "make test had returned bad status, ".
  4742.         "won't install without force"
  4743.         if exists $self->{'make_test'} and
  4744.         $self->{'make_test'} eq 'NO' and
  4745.         ! $self->{'force_update'};
  4746.  
  4747.     exists $self->{'install'} and push @e,
  4748.     $self->{'install'} eq "YES" ?
  4749.         "Already done" : "Already tried without success";
  4750.  
  4751.         exists $self->{later} and length($self->{later}) and
  4752.             push @e, $self->{later};
  4753.  
  4754.     $CPAN::Frontend->myprint(join "", map {"  $_\n"} @e) and return if @e;
  4755.     }
  4756.     chdir $self->{'build_dir'} or
  4757.     Carp::croak("Couldn't chdir to $self->{'build_dir'}");
  4758.     $self->debug("Changed directory to $self->{'build_dir'}")
  4759.     if $CPAN::DEBUG;
  4760.  
  4761.     if ($^O eq 'MacOS') {
  4762.         Mac::BuildTools::make_install($self);
  4763.         return;
  4764.     }
  4765.  
  4766.     my $system = join(" ", $CPAN::Config->{'make'},
  4767.               "install", $CPAN::Config->{make_install_arg});
  4768.     my($stderr) = $^O =~ /Win/i ? "" : " 2>&1 ";
  4769.     my($pipe) = FileHandle->new("$system $stderr |");
  4770.     my($makeout) = "";
  4771.     while (<$pipe>){
  4772.     $CPAN::Frontend->myprint($_);
  4773.     $makeout .= $_;
  4774.     }
  4775.     $pipe->close;
  4776.     if ($?==0) {
  4777.      $CPAN::Frontend->myprint("  $system -- OK\n");
  4778.      $CPAN::META->is_installed($self->{'build_dir'});
  4779.      return $self->{'install'} = "YES";
  4780.     } else {
  4781.      $self->{'install'} = "NO";
  4782.      $CPAN::Frontend->myprint("  $system -- NOT OK\n");
  4783.      if ($makeout =~ /permission/s && $> > 0) {
  4784.          $CPAN::Frontend->myprint(qq{    You may have to su }.
  4785.                       qq{to root to install the package\n});
  4786.      }
  4787.     }
  4788.     delete $self->{force_update};
  4789. }
  4790.  
  4791. #-> sub CPAN::Distribution::dir ;
  4792. sub dir {
  4793.     shift->{'build_dir'};
  4794. }
  4795.  
  4796. package CPAN::Bundle;
  4797.  
  4798. sub look {
  4799.     my $self = shift;
  4800.     $CPAN::Frontend->myprint(
  4801.                              qq{ look() commmand on bundles not}.
  4802.                              qq{ implemented (What should it do?)}
  4803.                             );
  4804. }
  4805.  
  4806. sub undelay {
  4807.     my $self = shift;
  4808.     delete $self->{later};
  4809.     for my $c ( $self->contains ) {
  4810.         my $obj = CPAN::Shell->expandany($c) or next;
  4811.         $obj->undelay;
  4812.     }
  4813. }
  4814.  
  4815. #-> sub CPAN::Bundle::color_cmd_tmps ;
  4816. sub color_cmd_tmps {
  4817.     my($self) = shift;
  4818.     my($depth) = shift || 0;
  4819.     my($color) = shift || 0;
  4820.     # a module needs to recurse to its cpan_file, a distribution needs
  4821.     # to recurse into its prereq_pms, a bundle needs to recurse into its modules
  4822.  
  4823.     return if exists $self->{incommandcolor}
  4824.         && $self->{incommandcolor}==$color;
  4825.     $CPAN::Frontend->mydie(sprintf("CPAN.pm panic: deep recursion in ".
  4826.                                    "color_cmd_tmps depth[%s] self[%s] id[%s]",
  4827.                                    $depth,
  4828.                                    $self,
  4829.                                    $self->id
  4830.                                   )) if $depth>=100;
  4831.     ##### warn "color_cmd_tmps $depth $color " . $self->id; # sleep 1;
  4832.  
  4833.     for my $c ( $self->contains ) {
  4834.         my $obj = CPAN::Shell->expandany($c) or next;
  4835.         CPAN->debug("c[$c]obj[$obj]") if $CPAN::DEBUG;
  4836.         $obj->color_cmd_tmps($depth+1,$color);
  4837.     }
  4838.     if ($color==0) {
  4839.         delete $self->{badtestcnt};
  4840.     }
  4841.     $self->{incommandcolor} = $color;
  4842. }
  4843.  
  4844. #-> sub CPAN::Bundle::as_string ;
  4845. sub as_string {
  4846.     my($self) = @_;
  4847.     $self->contains;
  4848.     # following line must be "=", not "||=" because we have a moving target
  4849.     $self->{INST_VERSION} = $self->inst_version;
  4850.     return $self->SUPER::as_string;
  4851. }
  4852.  
  4853. #-> sub CPAN::Bundle::contains ;
  4854. sub contains {
  4855.     my($self) = @_;
  4856.     my($inst_file) = $self->inst_file || "";
  4857.     my($id) = $self->id;
  4858.     $self->debug("inst_file[$inst_file]id[$id]") if $CPAN::DEBUG;
  4859.     unless ($inst_file) {
  4860.         # Try to get at it in the cpan directory
  4861.         $self->debug("no inst_file") if $CPAN::DEBUG;
  4862.         my $cpan_file;
  4863.         $CPAN::Frontend->mydie("I don't know a bundle with ID $id\n") unless
  4864.               $cpan_file = $self->cpan_file;
  4865.         if ($cpan_file eq "N/A") {
  4866.             $CPAN::Frontend->mydie("Bundle $id not found on disk and not on CPAN.
  4867.   Maybe stale symlink? Maybe removed during session? Giving up.\n");
  4868.         }
  4869.         my $dist = $CPAN::META->instance('CPAN::Distribution',
  4870.                                          $self->cpan_file);
  4871.         $dist->get;
  4872.         $self->debug($dist->as_string) if $CPAN::DEBUG;
  4873.         my($todir) = $CPAN::Config->{'cpan_home'};
  4874.         my(@me,$from,$to,$me);
  4875.         @me = split /::/, $self->id;
  4876.         $me[-1] .= ".pm";
  4877.         $me = File::Spec->catfile(@me);
  4878.         $from = $self->find_bundle_file($dist->{'build_dir'},$me);
  4879.         $to = File::Spec->catfile($todir,$me);
  4880.         File::Path::mkpath(File::Basename::dirname($to));
  4881.         File::Copy::copy($from, $to)
  4882.               or Carp::confess("Couldn't copy $from to $to: $!");
  4883.         $inst_file = $to;
  4884.     }
  4885.     my @result;
  4886.     my $fh = FileHandle->new;
  4887.     local $/ = "\n";
  4888.     open($fh,$inst_file) or die "Could not open '$inst_file': $!";
  4889.     my $in_cont = 0;
  4890.     $self->debug("inst_file[$inst_file]") if $CPAN::DEBUG;
  4891.     while (<$fh>) {
  4892.         $in_cont = m/^=(?!head1\s+CONTENTS)/ ? 0 :
  4893.             m/^=head1\s+CONTENTS/ ? 1 : $in_cont;
  4894.         next unless $in_cont;
  4895.         next if /^=/;
  4896.         s/\#.*//;
  4897.         next if /^\s+$/;
  4898.         chomp;
  4899.         push @result, (split " ", $_, 2)[0];
  4900.     }
  4901.     close $fh;
  4902.     delete $self->{STATUS};
  4903.     $self->{CONTAINS} = \@result;
  4904.     $self->debug("CONTAINS[@result]") if $CPAN::DEBUG;
  4905.     unless (@result) {
  4906.         $CPAN::Frontend->mywarn(qq{
  4907. The bundle file "$inst_file" may be a broken
  4908. bundlefile. It seems not to contain any bundle definition.
  4909. Please check the file and if it is bogus, please delete it.
  4910. Sorry for the inconvenience.
  4911. });
  4912.     }
  4913.     @result;
  4914. }
  4915.  
  4916. #-> sub CPAN::Bundle::find_bundle_file
  4917. sub find_bundle_file {
  4918.     my($self,$where,$what) = @_;
  4919.     $self->debug("where[$where]what[$what]") if $CPAN::DEBUG;
  4920. ### The following two lines let CPAN.pm become Bundle/CPAN.pm :-(
  4921. ###    my $bu = File::Spec->catfile($where,$what);
  4922. ###    return $bu if -f $bu;
  4923.     my $manifest = File::Spec->catfile($where,"MANIFEST");
  4924.     unless (-f $manifest) {
  4925.     require ExtUtils::Manifest;
  4926.     my $cwd = CPAN::anycwd();
  4927.     chdir $where or $CPAN::Frontend->mydie(qq{Could not chdir to "$where": $!});
  4928.     ExtUtils::Manifest::mkmanifest();
  4929.     chdir $cwd or $CPAN::Frontend->mydie(qq{Could not chdir to "$cwd": $!});
  4930.     }
  4931.     my $fh = FileHandle->new($manifest)
  4932.     or Carp::croak("Couldn't open $manifest: $!");
  4933.     local($/) = "\n";
  4934.     my $what2 = $what;
  4935.     if ($^O eq 'MacOS') {
  4936.       $what =~ s/^://;
  4937.       $what =~ tr|:|/|;
  4938.       $what2 =~ s/:Bundle://;
  4939.       $what2 =~ tr|:|/|;
  4940.     } else {
  4941.     $what2 =~ s|Bundle[/\\]||;
  4942.     }
  4943.     my $bu;
  4944.     while (<$fh>) {
  4945.     next if /^\s*\#/;
  4946.     my($file) = /(\S+)/;
  4947.     if ($file =~ m|\Q$what\E$|) {
  4948.         $bu = $file;
  4949.         # return File::Spec->catfile($where,$bu); # bad
  4950.         last;
  4951.     }
  4952.     # retry if she managed to
  4953.     # have no Bundle directory
  4954.     $bu = $file if $file =~ m|\Q$what2\E$|;
  4955.     }
  4956.     $bu =~ tr|/|:| if $^O eq 'MacOS';
  4957.     return File::Spec->catfile($where, $bu) if $bu;
  4958.     Carp::croak("Couldn't find a Bundle file in $where");
  4959. }
  4960.  
  4961. # needs to work quite differently from Module::inst_file because of
  4962. # cpan_home/Bundle/ directory and the possibility that we have
  4963. # shadowing effect. As it makes no sense to take the first in @INC for
  4964. # Bundles, we parse them all for $VERSION and take the newest.
  4965.  
  4966. #-> sub CPAN::Bundle::inst_file ;
  4967. sub inst_file {
  4968.     my($self) = @_;
  4969.     my($inst_file);
  4970.     my(@me);
  4971.     @me = split /::/, $self->id;
  4972.     $me[-1] .= ".pm";
  4973.     my($incdir,$bestv);
  4974.     foreach $incdir ($CPAN::Config->{'cpan_home'},@INC) {
  4975.         my $bfile = File::Spec->catfile($incdir, @me);
  4976.         CPAN->debug("bfile[$bfile]") if $CPAN::DEBUG;
  4977.         next unless -f $bfile;
  4978.         my $foundv = MM->parse_version($bfile);
  4979.         if (!$bestv || CPAN::Version->vgt($foundv,$bestv)) {
  4980.             $self->{INST_FILE} = $bfile;
  4981.             $self->{INST_VERSION} = $bestv = $foundv;
  4982.         }
  4983.     }
  4984.     $self->{INST_FILE};
  4985. }
  4986.  
  4987. #-> sub CPAN::Bundle::inst_version ;
  4988. sub inst_version {
  4989.     my($self) = @_;
  4990.     $self->inst_file; # finds INST_VERSION as side effect
  4991.     $self->{INST_VERSION};
  4992. }
  4993.  
  4994. #-> sub CPAN::Bundle::rematein ;
  4995. sub rematein {
  4996.     my($self,$meth) = @_;
  4997.     $self->debug("self[$self] meth[$meth]") if $CPAN::DEBUG;
  4998.     my($id) = $self->id;
  4999.     Carp::croak "Can't $meth $id, don't have an associated bundle file. :-(\n"
  5000.     unless $self->inst_file || $self->cpan_file;
  5001.     my($s,%fail);
  5002.     for $s ($self->contains) {
  5003.     my($type) = $s =~ m|/| ? 'CPAN::Distribution' :
  5004.         $s =~ m|^Bundle::| ? 'CPAN::Bundle' : 'CPAN::Module';
  5005.     if ($type eq 'CPAN::Distribution') {
  5006.         $CPAN::Frontend->mywarn(qq{
  5007. The Bundle }.$self->id.qq{ contains
  5008. explicitly a file $s.
  5009. });
  5010.         sleep 3;
  5011.     }
  5012.     # possibly noisy action:
  5013.         $self->debug("type[$type] s[$s]") if $CPAN::DEBUG;
  5014.     my $obj = $CPAN::META->instance($type,$s);
  5015.     $obj->$meth();
  5016.         if ($obj->isa(CPAN::Bundle)
  5017.             &&
  5018.             exists $obj->{install_failed}
  5019.             &&
  5020.             ref($obj->{install_failed}) eq "HASH"
  5021.            ) {
  5022.           for (keys %{$obj->{install_failed}}) {
  5023.             $self->{install_failed}{$_} = undef; # propagate faiure up
  5024.                                                  # to me in a
  5025.                                                  # recursive call
  5026.             $fail{$s} = 1; # the bundle itself may have succeeded but
  5027.                            # not all children
  5028.           }
  5029.         } else {
  5030.           my $success;
  5031.           $success = $obj->can("uptodate") ? $obj->uptodate : 0;
  5032.           $success ||= $obj->{'install'} && $obj->{'install'} eq "YES";
  5033.           if ($success) {
  5034.             delete $self->{install_failed}{$s};
  5035.           } else {
  5036.             $fail{$s} = 1;
  5037.           }
  5038.         }
  5039.     }
  5040.  
  5041.     # recap with less noise
  5042.     if ( $meth eq "install" ) {
  5043.     if (%fail) {
  5044.         require Text::Wrap;
  5045.         my $raw = sprintf(qq{Bundle summary:
  5046. The following items in bundle %s had installation problems:},
  5047.                   $self->id
  5048.                  );
  5049.         $CPAN::Frontend->myprint(Text::Wrap::fill("","",$raw));
  5050.         $CPAN::Frontend->myprint("\n");
  5051.         my $paragraph = "";
  5052.             my %reported;
  5053.         for $s ($self->contains) {
  5054.               if ($fail{$s}){
  5055.         $paragraph .= "$s ";
  5056.                 $self->{install_failed}{$s} = undef;
  5057.                 $reported{$s} = undef;
  5058.               }
  5059.         }
  5060.             my $report_propagated;
  5061.             for $s (sort keys %{$self->{install_failed}}) {
  5062.               next if exists $reported{$s};
  5063.               $paragraph .= "and the following items had problems
  5064. during recursive bundle calls: " unless $report_propagated++;
  5065.               $paragraph .= "$s ";
  5066.             }
  5067.         $CPAN::Frontend->myprint(Text::Wrap::fill("  ","  ",$paragraph));
  5068.         $CPAN::Frontend->myprint("\n");
  5069.     } else {
  5070.         $self->{'install'} = 'YES';
  5071.     }
  5072.     }
  5073. }
  5074.  
  5075. #sub CPAN::Bundle::xs_file
  5076. sub xs_file {
  5077.     # If a bundle contains another that contains an xs_file we have
  5078.     # here, we just don't bother I suppose
  5079.     return 0;
  5080. }
  5081.  
  5082. #-> sub CPAN::Bundle::force ;
  5083. sub force   { shift->rematein('force',@_); }
  5084. #-> sub CPAN::Bundle::get ;
  5085. sub get     { shift->rematein('get',@_); }
  5086. #-> sub CPAN::Bundle::make ;
  5087. sub make    { shift->rematein('make',@_); }
  5088. #-> sub CPAN::Bundle::test ;
  5089. sub test    {
  5090.     my $self = shift;
  5091.     $self->{badtestcnt} ||= 0;
  5092.     $self->rematein('test',@_);
  5093. }
  5094. #-> sub CPAN::Bundle::install ;
  5095. sub install {
  5096.   my $self = shift;
  5097.   $self->rematein('install',@_);
  5098. }
  5099. #-> sub CPAN::Bundle::clean ;
  5100. sub clean   { shift->rematein('clean',@_); }
  5101.  
  5102. #-> sub CPAN::Bundle::uptodate ;
  5103. sub uptodate {
  5104.     my($self) = @_;
  5105.     return 0 unless $self->SUPER::uptodate; # we mut have the current Bundle def
  5106.     my $c;
  5107.     foreach $c ($self->contains) {
  5108.         my $obj = CPAN::Shell->expandany($c);
  5109.         return 0 unless $obj->uptodate;
  5110.     }
  5111.     return 1;
  5112. }
  5113.  
  5114. #-> sub CPAN::Bundle::readme ;
  5115. sub readme  {
  5116.     my($self) = @_;
  5117.     my($file) = $self->cpan_file or $CPAN::Frontend->myprint(qq{
  5118. No File found for bundle } . $self->id . qq{\n}), return;
  5119.     $self->debug("self[$self] file[$file]") if $CPAN::DEBUG;
  5120.     $CPAN::META->instance('CPAN::Distribution',$file)->readme;
  5121. }
  5122.  
  5123. package CPAN::Module;
  5124.  
  5125. # Accessors
  5126. # sub cpan_userid { shift->{RO}{CPAN_USERID} }
  5127. sub userid {
  5128.     my $self = shift;
  5129.     return unless exists $self->{RO}; # should never happen
  5130.     return $self->{RO}{CPAN_USERID} || $self->{RO}{userid};
  5131. }
  5132. sub description { shift->{RO}{description} }
  5133.  
  5134. sub undelay {
  5135.     my $self = shift;
  5136.     delete $self->{later};
  5137.     if ( my $dist = CPAN::Shell->expand("Distribution", $self->cpan_file) ) {
  5138.         $dist->undelay;
  5139.     }
  5140. }
  5141.  
  5142. #-> sub CPAN::Module::color_cmd_tmps ;
  5143. sub color_cmd_tmps {
  5144.     my($self) = shift;
  5145.     my($depth) = shift || 0;
  5146.     my($color) = shift || 0;
  5147.     # a module needs to recurse to its cpan_file
  5148.  
  5149.     return if exists $self->{incommandcolor}
  5150.         && $self->{incommandcolor}==$color;
  5151.     $CPAN::Frontend->mydie(sprintf("CPAN.pm panic: deep recursion in ".
  5152.                                    "color_cmd_tmps depth[%s] self[%s] id[%s]",
  5153.                                    $depth,
  5154.                                    $self,
  5155.                                    $self->id
  5156.                                   )) if $depth>=100;
  5157.     ##### warn "color_cmd_tmps $depth $color " . $self->id; # sleep 1;
  5158.  
  5159.     if ( my $dist = CPAN::Shell->expand("Distribution", $self->cpan_file) ) {
  5160.         $dist->color_cmd_tmps($depth+1,$color);
  5161.     }
  5162.     if ($color==0) {
  5163.         delete $self->{badtestcnt};
  5164.     }
  5165.     $self->{incommandcolor} = $color;
  5166. }
  5167.  
  5168. #-> sub CPAN::Module::as_glimpse ;
  5169. sub as_glimpse {
  5170.     my($self) = @_;
  5171.     my(@m);
  5172.     my $class = ref($self);
  5173.     $class =~ s/^CPAN:://;
  5174.     my $color_on = "";
  5175.     my $color_off = "";
  5176.     if (
  5177.         $CPAN::Shell::COLOR_REGISTERED
  5178.         &&
  5179.         $CPAN::META->has_inst("Term::ANSIColor")
  5180.         &&
  5181.         $self->{RO}{description}
  5182.        ) {
  5183.         $color_on = Term::ANSIColor::color("green");
  5184.         $color_off = Term::ANSIColor::color("reset");
  5185.     }
  5186.     push @m, sprintf("%-15s %s%-15s%s (%s)\n",
  5187.                      $class,
  5188.                      $color_on,
  5189.                      $self->id,
  5190.                      $color_off,
  5191.              $self->cpan_file);
  5192.     join "", @m;
  5193. }
  5194.  
  5195. #-> sub CPAN::Module::as_string ;
  5196. sub as_string {
  5197.     my($self) = @_;
  5198.     my(@m);
  5199.     CPAN->debug($self) if $CPAN::DEBUG;
  5200.     my $class = ref($self);
  5201.     $class =~ s/^CPAN:://;
  5202.     local($^W) = 0;
  5203.     push @m, $class, " id = $self->{ID}\n";
  5204.     my $sprintf = "    %-12s %s\n";
  5205.     push @m, sprintf($sprintf, 'DESCRIPTION', $self->description)
  5206.     if $self->description;
  5207.     my $sprintf2 = "    %-12s %s (%s)\n";
  5208.     my($userid);
  5209.     if ($userid = $self->cpan_userid || $self->userid){
  5210.     my $author;
  5211.     if ($author = CPAN::Shell->expand('Author',$userid)) {
  5212.       my $email = "";
  5213.       my $m; # old perls
  5214.       if ($m = $author->email) {
  5215.             $email = " <$m>";
  5216.           }
  5217.       push @m, sprintf(
  5218.                $sprintf2,
  5219.                'CPAN_USERID',
  5220.                $userid,
  5221.                $author->fullname . $email
  5222.               );
  5223.     }
  5224.     }
  5225.     push @m, sprintf($sprintf, 'CPAN_VERSION', $self->cpan_version)
  5226.     if $self->cpan_version;
  5227.     push @m, sprintf($sprintf, 'CPAN_FILE', $self->cpan_file)
  5228.     if $self->cpan_file;
  5229.     my $sprintf3 = "    %-12s %1s%1s%1s%1s (%s,%s,%s,%s)\n";
  5230.     my(%statd,%stats,%statl,%stati);
  5231.     @statd{qw,? i c a b R M S,} = qw,unknown idea
  5232.     pre-alpha alpha beta released mature standard,;
  5233.     @stats{qw,? m d u n,}       = qw,unknown mailing-list
  5234.     developer comp.lang.perl.* none,;
  5235.     @statl{qw,? p c + o h,}       = qw,unknown perl C C++ other hybrid,;
  5236.     @stati{qw,? f r O h,}         = qw,unknown functions
  5237.     references+ties object-oriented hybrid,;
  5238.     $statd{' '} = 'unknown';
  5239.     $stats{' '} = 'unknown';
  5240.     $statl{' '} = 'unknown';
  5241.     $stati{' '} = 'unknown';
  5242.     push @m, sprintf(
  5243.              $sprintf3,
  5244.              'DSLI_STATUS',
  5245.              $self->{RO}{statd},
  5246.              $self->{RO}{stats},
  5247.              $self->{RO}{statl},
  5248.              $self->{RO}{stati},
  5249.              $statd{$self->{RO}{statd}},
  5250.              $stats{$self->{RO}{stats}},
  5251.              $statl{$self->{RO}{statl}},
  5252.              $stati{$self->{RO}{stati}}
  5253.             ) if $self->{RO}{statd};
  5254.     my $local_file = $self->inst_file;
  5255.     unless ($self->{MANPAGE}) {
  5256.         if ($local_file) {
  5257.             $self->{MANPAGE} = $self->manpage_headline($local_file);
  5258.         } else {
  5259.             # If we have already untarred it, we should look there
  5260.             my $dist = $CPAN::META->instance('CPAN::Distribution',
  5261.                                              $self->cpan_file);
  5262.             # warn "dist[$dist]";
  5263.             # mff=manifest file; mfh=manifest handle
  5264.             my($mff,$mfh);
  5265.             if (
  5266.                 $dist->{build_dir}
  5267.                 and
  5268.                 (-f  ($mff = File::Spec->catfile($dist->{build_dir}, "MANIFEST")))
  5269.                 and
  5270.                 $mfh = FileHandle->new($mff)
  5271.                ) {
  5272.                 CPAN->debug("mff[$mff]") if $CPAN::DEBUG;
  5273.                 my $lfre = $self->id; # local file RE
  5274.                 $lfre =~ s/::/./g;
  5275.                 $lfre .= "\\.pm\$";
  5276.                 my($lfl); # local file file
  5277.                 local $/ = "\n";
  5278.                 my(@mflines) = <$mfh>;
  5279.                 for (@mflines) {
  5280.                     s/^\s+//;
  5281.                     s/\s.*//s;
  5282.                 }
  5283.                 while (length($lfre)>5 and !$lfl) {
  5284.                     ($lfl) = grep /$lfre/, @mflines;
  5285.                     CPAN->debug("lfl[$lfl]lfre[$lfre]") if $CPAN::DEBUG;
  5286.                     $lfre =~ s/.+?\.//;
  5287.                 }
  5288.                 $lfl =~ s/\s.*//; # remove comments
  5289.                 $lfl =~ s/\s+//g; # chomp would maybe be too system-specific
  5290.                 my $lfl_abs = File::Spec->catfile($dist->{build_dir},$lfl);
  5291.                 # warn "lfl_abs[$lfl_abs]";
  5292.                 if (-f $lfl_abs) {
  5293.                     $self->{MANPAGE} = $self->manpage_headline($lfl_abs);
  5294.                 }
  5295.             }
  5296.         }
  5297.     }
  5298.     my($item);
  5299.     for $item (qw/MANPAGE/) {
  5300.     push @m, sprintf($sprintf, $item, $self->{$item})
  5301.         if exists $self->{$item};
  5302.     }
  5303.     for $item (qw/CONTAINS/) {
  5304.     push @m, sprintf($sprintf, $item, join(" ",@{$self->{$item}}))
  5305.         if exists $self->{$item} && @{$self->{$item}};
  5306.     }
  5307.     push @m, sprintf($sprintf, 'INST_FILE',
  5308.              $local_file || "(not installed)");
  5309.     push @m, sprintf($sprintf, 'INST_VERSION',
  5310.              $self->inst_version) if $local_file;
  5311.     join "", @m, "\n";
  5312. }
  5313.  
  5314. sub manpage_headline {
  5315.   my($self,$local_file) = @_;
  5316.   my(@local_file) = $local_file;
  5317.   $local_file =~ s/\.pm(?!\n)\Z/.pod/;
  5318.   push @local_file, $local_file;
  5319.   my(@result,$locf);
  5320.   for $locf (@local_file) {
  5321.     next unless -f $locf;
  5322.     my $fh = FileHandle->new($locf)
  5323.     or $Carp::Frontend->mydie("Couldn't open $locf: $!");
  5324.     my $inpod = 0;
  5325.     local $/ = "\n";
  5326.     while (<$fh>) {
  5327.       $inpod = m/^=(?!head1\s+NAME\s*$)/ ? 0 :
  5328.       m/^=head1\s+NAME\s*$/ ? 1 : $inpod;
  5329.       next unless $inpod;
  5330.       next if /^=/;
  5331.       next if /^\s+$/;
  5332.       chomp;
  5333.       push @result, $_;
  5334.     }
  5335.     close $fh;
  5336.     last if @result;
  5337.   }
  5338.   join " ", @result;
  5339. }
  5340.  
  5341. #-> sub CPAN::Module::cpan_file ;
  5342. # Note: also inherited by CPAN::Bundle
  5343. sub cpan_file {
  5344.     my $self = shift;
  5345.     CPAN->debug(sprintf "id[%s]", $self->id) if $CPAN::DEBUG;
  5346.     unless (defined $self->{RO}{CPAN_FILE}) {
  5347.     CPAN::Index->reload;
  5348.     }
  5349.     if (exists $self->{RO}{CPAN_FILE} && defined $self->{RO}{CPAN_FILE}){
  5350.     return $self->{RO}{CPAN_FILE};
  5351.     } else {
  5352.         my $userid = $self->userid;
  5353.         if ( $userid ) {
  5354.             if ($CPAN::META->exists("CPAN::Author",$userid)) {
  5355.                 my $author = $CPAN::META->instance("CPAN::Author",
  5356.                                                    $userid);
  5357.                 my $fullname = $author->fullname;
  5358.                 my $email = $author->email;
  5359.                 unless (defined $fullname && defined $email) {
  5360.                     return sprintf("Contact Author %s",
  5361.                                    $userid,
  5362.                                   );
  5363.                 }
  5364.                 return "Contact Author $fullname <$email>";
  5365.             } else {
  5366.                 return "UserID $userid";
  5367.             }
  5368.         } else {
  5369.             return "N/A";
  5370.         }
  5371.     }
  5372. }
  5373.  
  5374. #-> sub CPAN::Module::cpan_version ;
  5375. sub cpan_version {
  5376.     my $self = shift;
  5377.  
  5378.     $self->{RO}{CPAN_VERSION} = 'undef'
  5379.     unless defined $self->{RO}{CPAN_VERSION};
  5380.     # I believe this is always a bug in the index and should be reported
  5381.     # as such, but usually I find out such an error and do not want to
  5382.     # provoke too many bugreports
  5383.  
  5384.     $self->{RO}{CPAN_VERSION};
  5385. }
  5386.  
  5387. #-> sub CPAN::Module::force ;
  5388. sub force {
  5389.     my($self) = @_;
  5390.     $self->{'force_update'}++;
  5391. }
  5392.  
  5393. #-> sub CPAN::Module::rematein ;
  5394. sub rematein {
  5395.     my($self,$meth) = @_;
  5396.     $CPAN::Frontend->myprint(sprintf("Running %s for module %s\n",
  5397.                                      $meth,
  5398.                                      $self->id));
  5399.     my $cpan_file = $self->cpan_file;
  5400.     if ($cpan_file eq "N/A" || $cpan_file =~ /^Contact Author/){
  5401.       $CPAN::Frontend->mywarn(sprintf qq{
  5402.   The module %s isn\'t available on CPAN.
  5403.  
  5404.   Either the module has not yet been uploaded to CPAN, or it is
  5405.   temporary unavailable. Please contact the author to find out
  5406.   more about the status. Try 'i %s'.
  5407. },
  5408.                   $self->id,
  5409.                   $self->id,
  5410.                  );
  5411.       return;
  5412.     }
  5413.     my $pack = $CPAN::META->instance('CPAN::Distribution',$cpan_file);
  5414.     $pack->called_for($self->id);
  5415.     $pack->force($meth) if exists $self->{'force_update'};
  5416.     $pack->$meth();
  5417.     $pack->unforce if $pack->can("unforce") && exists $self->{'force_update'};
  5418.     delete $self->{'force_update'};
  5419. }
  5420.  
  5421. #-> sub CPAN::Module::readme ;
  5422. sub readme { shift->rematein('readme') }
  5423. #-> sub CPAN::Module::look ;
  5424. sub look { shift->rematein('look') }
  5425. #-> sub CPAN::Module::cvs_import ;
  5426. sub cvs_import { shift->rematein('cvs_import') }
  5427. #-> sub CPAN::Module::get ;
  5428. sub get    { shift->rematein('get',@_); }
  5429. #-> sub CPAN::Module::make ;
  5430. sub make   {
  5431.     my $self = shift;
  5432.     $self->rematein('make');
  5433. }
  5434. #-> sub CPAN::Module::test ;
  5435. sub test   {
  5436.     my $self = shift;
  5437.     $self->{badtestcnt} ||= 0;
  5438.     $self->rematein('test',@_);
  5439. }
  5440. #-> sub CPAN::Module::uptodate ;
  5441. sub uptodate {
  5442.     my($self) = @_;
  5443.     my($latest) = $self->cpan_version;
  5444.     $latest ||= 0;
  5445.     my($inst_file) = $self->inst_file;
  5446.     my($have) = 0;
  5447.     if (defined $inst_file) {
  5448.     $have = $self->inst_version;
  5449.     }
  5450.     local($^W)=0;
  5451.     if ($inst_file
  5452.     &&
  5453.     ! CPAN::Version->vgt($latest, $have)
  5454.        ) {
  5455.         CPAN->debug("returning uptodate. inst_file[$inst_file] ".
  5456.                     "latest[$latest] have[$have]") if $CPAN::DEBUG;
  5457.         return 1;
  5458.     }
  5459.     return;
  5460. }
  5461. #-> sub CPAN::Module::install ;
  5462. sub install {
  5463.     my($self) = @_;
  5464.     my($doit) = 0;
  5465.     if ($self->uptodate
  5466.     &&
  5467.     not exists $self->{'force_update'}
  5468.        ) {
  5469.     $CPAN::Frontend->myprint( $self->id. " is up to date.\n");
  5470.     } else {
  5471.     $doit = 1;
  5472.     }
  5473.     $self->rematein('install') if $doit;
  5474. }
  5475. #-> sub CPAN::Module::clean ;
  5476. sub clean  { shift->rematein('clean') }
  5477.  
  5478. #-> sub CPAN::Module::inst_file ;
  5479. sub inst_file {
  5480.     my($self) = @_;
  5481.     my($dir,@packpath);
  5482.     @packpath = split /::/, $self->{ID};
  5483.     $packpath[-1] .= ".pm";
  5484.     foreach $dir (@INC) {
  5485.     my $pmfile = File::Spec->catfile($dir,@packpath);
  5486.     if (-f $pmfile){
  5487.         return $pmfile;
  5488.     }
  5489.     }
  5490.     return;
  5491. }
  5492.  
  5493. #-> sub CPAN::Module::xs_file ;
  5494. sub xs_file {
  5495.     my($self) = @_;
  5496.     my($dir,@packpath);
  5497.     @packpath = split /::/, $self->{ID};
  5498.     push @packpath, $packpath[-1];
  5499.     $packpath[-1] .= "." . $Config::Config{'dlext'};
  5500.     foreach $dir (@INC) {
  5501.     my $xsfile = File::Spec->catfile($dir,'auto',@packpath);
  5502.     if (-f $xsfile){
  5503.         return $xsfile;
  5504.     }
  5505.     }
  5506.     return;
  5507. }
  5508.  
  5509. #-> sub CPAN::Module::inst_version ;
  5510. sub inst_version {
  5511.     my($self) = @_;
  5512.     my $parsefile = $self->inst_file or return;
  5513.     local($^W) = 0 if $] < 5.00303 && $ExtUtils::MakeMaker::VERSION < 5.38;
  5514.     my $have;
  5515.  
  5516.     # there was a bug in 5.6.0 that let lots of unini warnings out of
  5517.     # parse_version. Fixed shortly after 5.6.0 by PMQS. We can remove
  5518.     # the following workaround after 5.6.1 is out.
  5519.     local($SIG{__WARN__}) =  sub { my $w = shift;
  5520.                                    return if $w =~ /uninitialized/i;
  5521.                                    warn $w;
  5522.                                  };
  5523.  
  5524.     $have = MM->parse_version($parsefile) || "undef";
  5525.     $have =~ s/^ //; # since the %vd hack these two lines here are needed
  5526.     $have =~ s/ $//; # trailing whitespace happens all the time
  5527.  
  5528.     # My thoughts about why %vd processing should happen here
  5529.  
  5530.     # Alt1 maintain it as string with leading v:
  5531.     # read index files     do nothing
  5532.     # compare it           use utility for compare
  5533.     # print it             do nothing
  5534.  
  5535.     # Alt2 maintain it as what it is
  5536.     # read index files     convert
  5537.     # compare it           use utility because there's still a ">" vs "gt" issue
  5538.     # print it             use CPAN::Version for print
  5539.  
  5540.     # Seems cleaner to hold it in memory as a string starting with a "v"
  5541.  
  5542.     # If the author of this module made a mistake and wrote a quoted
  5543.     # "v1.13" instead of v1.13, we simply leave it at that with the
  5544.     # effect that *we* will treat it like a v-tring while the rest of
  5545.     # perl won't. Seems sensible when we consider that any action we
  5546.     # could take now would just add complexity.
  5547.  
  5548.     $have = CPAN::Version->readable($have);
  5549.  
  5550.     $have =~ s/\s*//g; # stringify to float around floating point issues
  5551.     $have; # no stringify needed, \s* above matches always
  5552. }
  5553.  
  5554. package CPAN::Tarzip;
  5555.  
  5556. # CPAN::Tarzip::gzip
  5557. sub gzip {
  5558.   my($class,$read,$write) = @_;
  5559.   if ($CPAN::META->has_inst("Compress::Zlib")) {
  5560.     my($buffer,$fhw);
  5561.     $fhw = FileHandle->new($read)
  5562.     or $CPAN::Frontend->mydie("Could not open $read: $!");
  5563.     my $gz = Compress::Zlib::gzopen($write, "wb")
  5564.     or $CPAN::Frontend->mydie("Cannot gzopen $write: $!\n");
  5565.     $gz->gzwrite($buffer)
  5566.     while read($fhw,$buffer,4096) > 0 ;
  5567.     $gz->gzclose() ;
  5568.     $fhw->close;
  5569.     return 1;
  5570.   } else {
  5571.     system("$CPAN::Config->{gzip} -c $read > $write")==0;
  5572.   }
  5573. }
  5574.  
  5575.  
  5576. # CPAN::Tarzip::gunzip
  5577. sub gunzip {
  5578.   my($class,$read,$write) = @_;
  5579.   if ($CPAN::META->has_inst("Compress::Zlib")) {
  5580.     my($buffer,$fhw);
  5581.     $fhw = FileHandle->new(">$write")
  5582.     or $CPAN::Frontend->mydie("Could not open >$write: $!");
  5583.     my $gz = Compress::Zlib::gzopen($read, "rb")
  5584.     or $CPAN::Frontend->mydie("Cannot gzopen $read: $!\n");
  5585.     $fhw->print($buffer)
  5586.     while $gz->gzread($buffer) > 0 ;
  5587.     $CPAN::Frontend->mydie("Error reading from $read: $!\n")
  5588.     if $gz->gzerror != Compress::Zlib::Z_STREAM_END();
  5589.     $gz->gzclose() ;
  5590.     $fhw->close;
  5591.     return 1;
  5592.   } else {
  5593.     system("$CPAN::Config->{gzip} -dc $read > $write")==0;
  5594.   }
  5595. }
  5596.  
  5597.  
  5598. # CPAN::Tarzip::gtest
  5599. sub gtest {
  5600.   my($class,$read) = @_;
  5601.   # After I had reread the documentation in zlib.h, I discovered that
  5602.   # uncompressed files do not lead to an gzerror (anymore?).
  5603.   if ( $CPAN::META->has_inst("Compress::Zlib") ) {
  5604.     my($buffer,$len);
  5605.     $len = 0;
  5606.     my $gz = Compress::Zlib::gzopen($read, "rb")
  5607.     or $CPAN::Frontend->mydie(sprintf("Cannot gzopen %s: %s\n",
  5608.                                           $read,
  5609.                                           $Compress::Zlib::gzerrno));
  5610.     while ($gz->gzread($buffer) > 0 ){
  5611.         $len += length($buffer);
  5612.         $buffer = "";
  5613.     }
  5614.     my $err = $gz->gzerror;
  5615.     my $success = ! $err || $err == Compress::Zlib::Z_STREAM_END();
  5616.     if ($len == -s $read){
  5617.         $success = 0;
  5618.         CPAN->debug("hit an uncompressed file") if $CPAN::DEBUG;
  5619.     }
  5620.     $gz->gzclose();
  5621.     CPAN->debug("err[$err]success[$success]") if $CPAN::DEBUG;
  5622.     return $success;
  5623.   } else {
  5624.       return system("$CPAN::Config->{gzip} -dt $read")==0;
  5625.   }
  5626. }
  5627.  
  5628.  
  5629. # CPAN::Tarzip::TIEHANDLE
  5630. sub TIEHANDLE {
  5631.   my($class,$file) = @_;
  5632.   my $ret;
  5633.   $class->debug("file[$file]");
  5634.   if ($CPAN::META->has_inst("Compress::Zlib")) {
  5635.     my $gz = Compress::Zlib::gzopen($file,"rb") or
  5636.     die "Could not gzopen $file";
  5637.     $ret = bless {GZ => $gz}, $class;
  5638.   } else {
  5639.     my $pipe = "$CPAN::Config->{gzip} --decompress --stdout $file |";
  5640.     my $fh = FileHandle->new($pipe) or die "Could not pipe[$pipe]: $!";
  5641.     binmode $fh;
  5642.     $ret = bless {FH => $fh}, $class;
  5643.   }
  5644.   $ret;
  5645. }
  5646.  
  5647.  
  5648. # CPAN::Tarzip::READLINE
  5649. sub READLINE {
  5650.   my($self) = @_;
  5651.   if (exists $self->{GZ}) {
  5652.     my $gz = $self->{GZ};
  5653.     my($line,$bytesread);
  5654.     $bytesread = $gz->gzreadline($line);
  5655.     return undef if $bytesread <= 0;
  5656.     return $line;
  5657.   } else {
  5658.     my $fh = $self->{FH};
  5659.     return scalar <$fh>;
  5660.   }
  5661. }
  5662.  
  5663.  
  5664. # CPAN::Tarzip::READ
  5665. sub READ {
  5666.   my($self,$ref,$length,$offset) = @_;
  5667.   die "read with offset not implemented" if defined $offset;
  5668.   if (exists $self->{GZ}) {
  5669.     my $gz = $self->{GZ};
  5670.     my $byteread = $gz->gzread($$ref,$length);# 30eaf79e8b446ef52464b5422da328a8
  5671.     return $byteread;
  5672.   } else {
  5673.     my $fh = $self->{FH};
  5674.     return read($fh,$$ref,$length);
  5675.   }
  5676. }
  5677.  
  5678.  
  5679. # CPAN::Tarzip::DESTROY
  5680. sub DESTROY {
  5681.     my($self) = @_;
  5682.     if (exists $self->{GZ}) {
  5683.         my $gz = $self->{GZ};
  5684.         $gz->gzclose() if defined $gz; # hard to say if it is allowed
  5685.                                        # to be undef ever. AK, 2000-09
  5686.     } else {
  5687.         my $fh = $self->{FH};
  5688.         $fh->close if defined $fh;
  5689.     }
  5690.     undef $self;
  5691. }
  5692.  
  5693.  
  5694. # CPAN::Tarzip::untar
  5695. sub untar {
  5696.   my($class,$file) = @_;
  5697.   my($prefer) = 0;
  5698.  
  5699.   if (0) { # makes changing order easier
  5700.   } elsif ($BUGHUNTING){
  5701.       $prefer=2;
  5702.   } elsif (MM->maybe_command($CPAN::Config->{gzip})
  5703.            &&
  5704.            MM->maybe_command($CPAN::Config->{'tar'})) {
  5705.       # should be default until Archive::Tar is fixed
  5706.       $prefer = 1;
  5707.   } elsif (
  5708.            $CPAN::META->has_inst("Archive::Tar")
  5709.            &&
  5710.            $CPAN::META->has_inst("Compress::Zlib") ) {
  5711.       $prefer = 2;
  5712.   } else {
  5713.     $CPAN::Frontend->mydie(qq{
  5714. CPAN.pm needs either both external programs tar and gzip installed or
  5715. both the modules Archive::Tar and Compress::Zlib. Neither prerequisite
  5716. is available. Can\'t continue.
  5717. });
  5718.   }
  5719.   if ($prefer==1) { # 1 => external gzip+tar
  5720.     my($system);
  5721.     my $is_compressed = $class->gtest($file);
  5722.     if ($is_compressed) {
  5723.         $system = "$CPAN::Config->{gzip} --decompress --stdout " .
  5724.             "< $file | $CPAN::Config->{tar} xvf -";
  5725.     } else {
  5726.         $system = "$CPAN::Config->{tar} xvf $file";
  5727.     }
  5728.     if (system($system) != 0) {
  5729.         # people find the most curious tar binaries that cannot handle
  5730.         # pipes
  5731.         if ($is_compressed) {
  5732.             (my $ungzf = $file) =~ s/\.gz(?!\n)\Z//;
  5733.             if (CPAN::Tarzip->gunzip($file, $ungzf)) {
  5734.                 $CPAN::Frontend->myprint(qq{Uncompressed $file successfully\n});
  5735.             } else {
  5736.                 $CPAN::Frontend->mydie(qq{Couldn\'t uncompress $file\n});
  5737.             }
  5738.             $file = $ungzf;
  5739.         }
  5740.         $system = "$CPAN::Config->{tar} xvf $file";
  5741.         $CPAN::Frontend->myprint(qq{Using Tar:$system:\n});
  5742.         if (system($system)==0) {
  5743.             $CPAN::Frontend->myprint(qq{Untarred $file successfully\n});
  5744.         } else {
  5745.             $CPAN::Frontend->mydie(qq{Couldn\'t untar $file\n});
  5746.         }
  5747.         return 1;
  5748.     } else {
  5749.         return 1;
  5750.     }
  5751.   } elsif ($prefer==2) { # 2 => modules
  5752.     my $tar = Archive::Tar->new($file,1);
  5753.     my $af; # archive file
  5754.     my @af;
  5755.     if ($BUGHUNTING) {
  5756.         # RCS 1.337 had this code, it turned out unacceptable slow but
  5757.         # it revealed a bug in Archive::Tar. Code is only here to hunt
  5758.         # the bug again. It should never be enabled in published code.
  5759.         # GDGraph3d-0.53 was an interesting case according to Larry
  5760.         # Virden.
  5761.         warn(">>>Bughunting code enabled<<< " x 20);
  5762.         for $af ($tar->list_files) {
  5763.             if ($af =~ m!^(/|\.\./)!) {
  5764.                 $CPAN::Frontend->mydie("ALERT: Archive contains ".
  5765.                                        "illegal member [$af]");
  5766.             }
  5767.             $CPAN::Frontend->myprint("$af\n");
  5768.             $tar->extract($af); # slow but effective for finding the bug
  5769.             return if $CPAN::Signal;
  5770.         }
  5771.     } else {
  5772.         for $af ($tar->list_files) {
  5773.             if ($af =~ m!^(/|\.\./)!) {
  5774.                 $CPAN::Frontend->mydie("ALERT: Archive contains ".
  5775.                                        "illegal member [$af]");
  5776.             }
  5777.             $CPAN::Frontend->myprint("$af\n");
  5778.             push @af, $af;
  5779.             return if $CPAN::Signal;
  5780.         }
  5781.         $tar->extract(@af);
  5782.     }
  5783.  
  5784.     Mac::BuildTools::convert_files([$tar->list_files], 1)
  5785.         if ($^O eq 'MacOS');
  5786.  
  5787.     return 1;
  5788.   }
  5789. }
  5790.  
  5791. sub unzip {
  5792.     my($class,$file) = @_;
  5793.     if ($CPAN::META->has_inst("Archive::Zip")) {
  5794.         # blueprint of the code from Archive::Zip::Tree::extractTree();
  5795.         my $zip = Archive::Zip->new();
  5796.         my $status;
  5797.         $status = $zip->read($file);
  5798.         die "Read of file[$file] failed\n" if $status != Archive::Zip::AZ_OK();
  5799.         $CPAN::META->debug("Successfully read file[$file]") if $CPAN::DEBUG;
  5800.         my @members = $zip->members();
  5801.         for my $member ( @members ) {
  5802.             my $af = $member->fileName();
  5803.             if ($af =~ m!^(/|\.\./)!) {
  5804.                 $CPAN::Frontend->mydie("ALERT: Archive contains ".
  5805.                                        "illegal member [$af]");
  5806.             }
  5807.             my $status = $member->extractToFileNamed( $af );
  5808.             $CPAN::META->debug("af[$af]status[$status]") if $CPAN::DEBUG;
  5809.             die "Extracting of file[$af] from zipfile[$file] failed\n" if
  5810.                 $status != Archive::Zip::AZ_OK();
  5811.             return if $CPAN::Signal;
  5812.         }
  5813.         return 1;
  5814.     } else {
  5815.         my $unzip = $CPAN::Config->{unzip} or
  5816.             $CPAN::Frontend->mydie("Cannot unzip, no unzip program available");
  5817.         my @system = ($unzip, $file);
  5818.         return system(@system) == 0;
  5819.     }
  5820. }
  5821.  
  5822.  
  5823. package CPAN::Version;
  5824. # CPAN::Version::vcmp courtesy Jost Krieger
  5825. sub vcmp {
  5826.   my($self,$l,$r) = @_;
  5827.   local($^W) = 0;
  5828.   CPAN->debug("l[$l] r[$r]") if $CPAN::DEBUG;
  5829.  
  5830.   return 0 if $l eq $r; # short circuit for quicker success
  5831.  
  5832.   if ($l=~/^v/ <=> $r=~/^v/) {
  5833.       for ($l,$r) {
  5834.           next if /^v/;
  5835.           $_ = $self->float2vv($_);
  5836.       }
  5837.   }
  5838.  
  5839.   return
  5840.       ($l ne "undef") <=> ($r ne "undef") ||
  5841.           ($] >= 5.006 &&
  5842.            $l =~ /^v/ &&
  5843.            $r =~ /^v/ &&
  5844.            $self->vstring($l) cmp $self->vstring($r)) ||
  5845.                $l <=> $r ||
  5846.                    $l cmp $r;
  5847. }
  5848.  
  5849. sub vgt {
  5850.   my($self,$l,$r) = @_;
  5851.   $self->vcmp($l,$r) > 0;
  5852. }
  5853.  
  5854. sub vstring {
  5855.   my($self,$n) = @_;
  5856.   $n =~ s/^v// or die "CPAN::Version::vstring() called with invalid arg [$n]";
  5857.   pack "U*", split /\./, $n;
  5858. }
  5859.  
  5860. # vv => visible vstring
  5861. sub float2vv {
  5862.     my($self,$n) = @_;
  5863.     my($rev) = int($n);
  5864.     $rev ||= 0;
  5865.     my($mantissa) = $n =~ /\.(\d{1,12})/; # limit to 12 digits to limit
  5866.                                           # architecture influence
  5867.     $mantissa ||= 0;
  5868.     $mantissa .= "0" while length($mantissa)%3;
  5869.     my $ret = "v" . $rev;
  5870.     while ($mantissa) {
  5871.         $mantissa =~ s/(\d{1,3})// or
  5872.             die "Panic: length>0 but not a digit? mantissa[$mantissa]";
  5873.         $ret .= ".".int($1);
  5874.     }
  5875.     # warn "n[$n]ret[$ret]";
  5876.     $ret;
  5877. }
  5878.  
  5879. sub readable {
  5880.   my($self,$n) = @_;
  5881.   $n =~ /^([\w\-\+\.]+)/;
  5882.  
  5883.   return $1 if defined $1 && length($1)>0;
  5884.   # if the first user reaches version v43, he will be treated as "+".
  5885.   # We'll have to decide about a new rule here then, depending on what
  5886.   # will be the prevailing versioning behavior then.
  5887.  
  5888.   if ($] < 5.006) { # or whenever v-strings were introduced
  5889.     # we get them wrong anyway, whatever we do, because 5.005 will
  5890.     # have already interpreted 0.2.4 to be "0.24". So even if he
  5891.     # indexer sends us something like "v0.2.4" we compare wrongly.
  5892.  
  5893.     # And if they say v1.2, then the old perl takes it as "v12"
  5894.  
  5895.     $CPAN::Frontend->mywarn("Suspicious version string seen [$n]");
  5896.     return $n;
  5897.   }
  5898.   my $better = sprintf "v%vd", $n;
  5899.   CPAN->debug("n[$n] better[$better]") if $CPAN::DEBUG;
  5900.   return $better;
  5901. }
  5902.  
  5903. package CPAN;
  5904.  
  5905. 1;
  5906.  
  5907. __END__
  5908.  
  5909. =head1 NAME
  5910.  
  5911. CPAN - query, download and build perl modules from CPAN sites
  5912.  
  5913. =head1 SYNOPSIS
  5914.  
  5915. Interactive mode:
  5916.  
  5917.   perl -MCPAN -e shell;
  5918.  
  5919. Batch mode:
  5920.  
  5921.   use CPAN;
  5922.  
  5923.   autobundle, clean, install, make, recompile, test
  5924.  
  5925. =head1 DESCRIPTION
  5926.  
  5927. The CPAN module is designed to automate the make and install of perl
  5928. modules and extensions. It includes some searching capabilities and
  5929. knows how to use Net::FTP or LWP (or lynx or an external ftp client)
  5930. to fetch the raw data from the net.
  5931.  
  5932. Modules are fetched from one or more of the mirrored CPAN
  5933. (Comprehensive Perl Archive Network) sites and unpacked in a dedicated
  5934. directory.
  5935.  
  5936. The CPAN module also supports the concept of named and versioned
  5937. I<bundles> of modules. Bundles simplify the handling of sets of
  5938. related modules. See Bundles below.
  5939.  
  5940. The package contains a session manager and a cache manager. There is
  5941. no status retained between sessions. The session manager keeps track
  5942. of what has been fetched, built and installed in the current
  5943. session. The cache manager keeps track of the disk space occupied by
  5944. the make processes and deletes excess space according to a simple FIFO
  5945. mechanism.
  5946.  
  5947. For extended searching capabilities there's a plugin for CPAN available,
  5948. L<C<CPAN::WAIT>|CPAN::WAIT>. C<CPAN::WAIT> is a full-text search engine
  5949. that indexes all documents available in CPAN authors directories. If
  5950. C<CPAN::WAIT> is installed on your system, the interactive shell of
  5951. CPAN.pm will enable the C<wq>, C<wr>, C<wd>, C<wl>, and C<wh> commands
  5952. which send queries to the WAIT server that has been configured for your
  5953. installation.
  5954.  
  5955. All other methods provided are accessible in a programmer style and in an
  5956. interactive shell style.
  5957.  
  5958. =head2 Interactive Mode
  5959.  
  5960. The interactive mode is entered by running
  5961.  
  5962.     perl -MCPAN -e shell
  5963.  
  5964. which puts you into a readline interface. You will have the most fun if
  5965. you install Term::ReadKey and Term::ReadLine to enjoy both history and
  5966. command completion.
  5967.  
  5968. Once you are on the command line, type 'h' and the rest should be
  5969. self-explanatory.
  5970.  
  5971. The function call C<shell> takes two optional arguments, one is the
  5972. prompt, the second is the default initial command line (the latter
  5973. only works if a real ReadLine interface module is installed).
  5974.  
  5975. The most common uses of the interactive modes are
  5976.  
  5977. =over 2
  5978.  
  5979. =item Searching for authors, bundles, distribution files and modules
  5980.  
  5981. There are corresponding one-letter commands C<a>, C<b>, C<d>, and C<m>
  5982. for each of the four categories and another, C<i> for any of the
  5983. mentioned four. Each of the four entities is implemented as a class
  5984. with slightly differing methods for displaying an object.
  5985.  
  5986. Arguments you pass to these commands are either strings exactly matching
  5987. the identification string of an object or regular expressions that are
  5988. then matched case-insensitively against various attributes of the
  5989. objects. The parser recognizes a regular expression only if you
  5990. enclose it between two slashes.
  5991.  
  5992. The principle is that the number of found objects influences how an
  5993. item is displayed. If the search finds one item, the result is
  5994. displayed with the rather verbose method C<as_string>, but if we find
  5995. more than one, we display each object with the terse method
  5996. <as_glimpse>.
  5997.  
  5998. =item make, test, install, clean  modules or distributions
  5999.  
  6000. These commands take any number of arguments and investigate what is
  6001. necessary to perform the action. If the argument is a distribution
  6002. file name (recognized by embedded slashes), it is processed. If it is
  6003. a module, CPAN determines the distribution file in which this module
  6004. is included and processes that, following any dependencies named in
  6005. the module's Makefile.PL (this behavior is controlled by
  6006. I<prerequisites_policy>.)
  6007.  
  6008. Any C<make> or C<test> are run unconditionally. An
  6009.  
  6010.   install <distribution_file>
  6011.  
  6012. also is run unconditionally. But for
  6013.  
  6014.   install <module>
  6015.  
  6016. CPAN checks if an install is actually needed for it and prints
  6017. I<module up to date> in the case that the distribution file containing
  6018. the module doesn't need to be updated.
  6019.  
  6020. CPAN also keeps track of what it has done within the current session
  6021. and doesn't try to build a package a second time regardless if it
  6022. succeeded or not. The C<force> command takes as a first argument the
  6023. method to invoke (currently: C<make>, C<test>, or C<install>) and executes the
  6024. command from scratch.
  6025.  
  6026. Example:
  6027.  
  6028.     cpan> install OpenGL
  6029.     OpenGL is up to date.
  6030.     cpan> force install OpenGL
  6031.     Running make
  6032.     OpenGL-0.4/
  6033.     OpenGL-0.4/COPYRIGHT
  6034.     [...]
  6035.  
  6036. A C<clean> command results in a
  6037.  
  6038.   make clean
  6039.  
  6040. being executed within the distribution file's working directory.
  6041.  
  6042. =item get, readme, look module or distribution
  6043.  
  6044. C<get> downloads a distribution file without further action. C<readme>
  6045. displays the README file of the associated distribution. C<Look> gets
  6046. and untars (if not yet done) the distribution file, changes to the
  6047. appropriate directory and opens a subshell process in that directory.
  6048.  
  6049. =item ls author
  6050.  
  6051. C<ls> lists all distribution files in and below an author's CPAN
  6052. directory. Only those files that contain modules are listed and if
  6053. there is more than one for any given module, only the most recent one
  6054. is listed.
  6055.  
  6056. =item Signals
  6057.  
  6058. CPAN.pm installs signal handlers for SIGINT and SIGTERM. While you are
  6059. in the cpan-shell it is intended that you can press C<^C> anytime and
  6060. return to the cpan-shell prompt. A SIGTERM will cause the cpan-shell
  6061. to clean up and leave the shell loop. You can emulate the effect of a
  6062. SIGTERM by sending two consecutive SIGINTs, which usually means by
  6063. pressing C<^C> twice.
  6064.  
  6065. CPAN.pm ignores a SIGPIPE. If the user sets inactivity_timeout, a
  6066. SIGALRM is used during the run of the C<perl Makefile.PL> subprocess.
  6067.  
  6068. =back
  6069.  
  6070. =head2 CPAN::Shell
  6071.  
  6072. The commands that are available in the shell interface are methods in
  6073. the package CPAN::Shell. If you enter the shell command, all your
  6074. input is split by the Text::ParseWords::shellwords() routine which
  6075. acts like most shells do. The first word is being interpreted as the
  6076. method to be called and the rest of the words are treated as arguments
  6077. to this method. Continuation lines are supported if a line ends with a
  6078. literal backslash.
  6079.  
  6080. =head2 autobundle
  6081.  
  6082. C<autobundle> writes a bundle file into the
  6083. C<$CPAN::Config-E<gt>{cpan_home}/Bundle> directory. The file contains
  6084. a list of all modules that are both available from CPAN and currently
  6085. installed within @INC. The name of the bundle file is based on the
  6086. current date and a counter.
  6087.  
  6088. =head2 recompile
  6089.  
  6090. recompile() is a very special command in that it takes no argument and
  6091. runs the make/test/install cycle with brute force over all installed
  6092. dynamically loadable extensions (aka XS modules) with 'force' in
  6093. effect. The primary purpose of this command is to finish a network
  6094. installation. Imagine, you have a common source tree for two different
  6095. architectures. You decide to do a completely independent fresh
  6096. installation. You start on one architecture with the help of a Bundle
  6097. file produced earlier. CPAN installs the whole Bundle for you, but
  6098. when you try to repeat the job on the second architecture, CPAN
  6099. responds with a C<"Foo up to date"> message for all modules. So you
  6100. invoke CPAN's recompile on the second architecture and you're done.
  6101.  
  6102. Another popular use for C<recompile> is to act as a rescue in case your
  6103. perl breaks binary compatibility. If one of the modules that CPAN uses
  6104. is in turn depending on binary compatibility (so you cannot run CPAN
  6105. commands), then you should try the CPAN::Nox module for recovery.
  6106.  
  6107. =head2 The four C<CPAN::*> Classes: Author, Bundle, Module, Distribution
  6108.  
  6109. Although it may be considered internal, the class hierarchy does matter
  6110. for both users and programmer. CPAN.pm deals with above mentioned four
  6111. classes, and all those classes share a set of methods. A classical
  6112. single polymorphism is in effect. A metaclass object registers all
  6113. objects of all kinds and indexes them with a string. The strings
  6114. referencing objects have a separated namespace (well, not completely
  6115. separated):
  6116.  
  6117.          Namespace                         Class
  6118.  
  6119.    words containing a "/" (slash)      Distribution
  6120.     words starting with Bundle::          Bundle
  6121.           everything else            Module or Author
  6122.  
  6123. Modules know their associated Distribution objects. They always refer
  6124. to the most recent official release. Developers may mark their releases
  6125. as unstable development versions (by inserting an underbar into the
  6126. module version number which will also be reflected in the distribution
  6127. name when you run 'make dist'), so the really hottest and newest 
  6128. distribution is not always the default.  If a module Foo circulates 
  6129. on CPAN in both version 1.23 and 1.23_90, CPAN.pm offers a convenient 
  6130. way to install version 1.23 by saying
  6131.  
  6132.     install Foo
  6133.  
  6134. This would install the complete distribution file (say
  6135. BAR/Foo-1.23.tar.gz) with all accompanying material. But if you would
  6136. like to install version 1.23_90, you need to know where the
  6137. distribution file resides on CPAN relative to the authors/id/
  6138. directory. If the author is BAR, this might be BAR/Foo-1.23_90.tar.gz;
  6139. so you would have to say
  6140.  
  6141.     install BAR/Foo-1.23_90.tar.gz
  6142.  
  6143. The first example will be driven by an object of the class
  6144. CPAN::Module, the second by an object of class CPAN::Distribution.
  6145.  
  6146. =head2 Programmer's interface
  6147.  
  6148. If you do not enter the shell, the available shell commands are both
  6149. available as methods (C<CPAN::Shell-E<gt>install(...)>) and as
  6150. functions in the calling package (C<install(...)>).
  6151.  
  6152. There's currently only one class that has a stable interface -
  6153. CPAN::Shell. All commands that are available in the CPAN shell are
  6154. methods of the class CPAN::Shell. Each of the commands that produce
  6155. listings of modules (C<r>, C<autobundle>, C<u>) also return a list of
  6156. the IDs of all modules within the list.
  6157.  
  6158. =over 2
  6159.  
  6160. =item expand($type,@things)
  6161.  
  6162. The IDs of all objects available within a program are strings that can
  6163. be expanded to the corresponding real objects with the
  6164. C<CPAN::Shell-E<gt>expand("Module",@things)> method. Expand returns a
  6165. list of CPAN::Module objects according to the C<@things> arguments
  6166. given. In scalar context it only returns the first element of the
  6167. list.
  6168.  
  6169. =item expandany(@things)
  6170.  
  6171. Like expand, but returns objects of the appropriate type, i.e.
  6172. CPAN::Bundle objects for bundles, CPAN::Module objects for modules and
  6173. CPAN::Distribution objects fro distributions.
  6174.  
  6175. =item Programming Examples
  6176.  
  6177. This enables the programmer to do operations that combine
  6178. functionalities that are available in the shell.
  6179.  
  6180.     # install everything that is outdated on my disk:
  6181.     perl -MCPAN -e 'CPAN::Shell->install(CPAN::Shell->r)'
  6182.  
  6183.     # install my favorite programs if necessary:
  6184.     for $mod (qw(Net::FTP Digest::MD5 Data::Dumper)){
  6185.         my $obj = CPAN::Shell->expand('Module',$mod);
  6186.         $obj->install;
  6187.     }
  6188.  
  6189.     # list all modules on my disk that have no VERSION number
  6190.     for $mod (CPAN::Shell->expand("Module","/./")){
  6191.     next unless $mod->inst_file;
  6192.         # MakeMaker convention for undefined $VERSION:
  6193.     next unless $mod->inst_version eq "undef";
  6194.     print "No VERSION in ", $mod->id, "\n";
  6195.     }
  6196.  
  6197.     # find out which distribution on CPAN contains a module:
  6198.     print CPAN::Shell->expand("Module","Apache::Constants")->cpan_file
  6199.  
  6200. Or if you want to write a cronjob to watch The CPAN, you could list
  6201. all modules that need updating. First a quick and dirty way:
  6202.  
  6203.     perl -e 'use CPAN; CPAN::Shell->r;'
  6204.  
  6205. If you don't want to get any output in the case that all modules are
  6206. up to date, you can parse the output of above command for the regular
  6207. expression //modules are up to date// and decide to mail the output
  6208. only if it doesn't match. Ick?
  6209.  
  6210. If you prefer to do it more in a programmer style in one single
  6211. process, maybe something like this suits you better:
  6212.  
  6213.   # list all modules on my disk that have newer versions on CPAN
  6214.   for $mod (CPAN::Shell->expand("Module","/./")){
  6215.     next unless $mod->inst_file;
  6216.     next if $mod->uptodate;
  6217.     printf "Module %s is installed as %s, could be updated to %s from CPAN\n",
  6218.         $mod->id, $mod->inst_version, $mod->cpan_version;
  6219.   }
  6220.  
  6221. If that gives you too much output every day, you maybe only want to
  6222. watch for three modules. You can write
  6223.  
  6224.   for $mod (CPAN::Shell->expand("Module","/Apache|LWP|CGI/")){
  6225.  
  6226. as the first line instead. Or you can combine some of the above
  6227. tricks:
  6228.  
  6229.   # watch only for a new mod_perl module
  6230.   $mod = CPAN::Shell->expand("Module","mod_perl");
  6231.   exit if $mod->uptodate;
  6232.   # new mod_perl arrived, let me know all update recommendations
  6233.   CPAN::Shell->r;
  6234.  
  6235. =back
  6236.  
  6237. =head2 Methods in the other Classes
  6238.  
  6239. The programming interface for the classes CPAN::Module,
  6240. CPAN::Distribution, CPAN::Bundle, and CPAN::Author is still considered
  6241. beta and partially even alpha. In the following paragraphs only those
  6242. methods are documented that have proven useful over a longer time and
  6243. thus are unlikely to change.
  6244.  
  6245. =over 4
  6246.  
  6247. =item CPAN::Author::as_glimpse()
  6248.  
  6249. Returns a one-line description of the author
  6250.  
  6251. =item CPAN::Author::as_string()
  6252.  
  6253. Returns a multi-line description of the author
  6254.  
  6255. =item CPAN::Author::email()
  6256.  
  6257. Returns the author's email address
  6258.  
  6259. =item CPAN::Author::fullname()
  6260.  
  6261. Returns the author's name
  6262.  
  6263. =item CPAN::Author::name()
  6264.  
  6265. An alias for fullname
  6266.  
  6267. =item CPAN::Bundle::as_glimpse()
  6268.  
  6269. Returns a one-line description of the bundle
  6270.  
  6271. =item CPAN::Bundle::as_string()
  6272.  
  6273. Returns a multi-line description of the bundle
  6274.  
  6275. =item CPAN::Bundle::clean()
  6276.  
  6277. Recursively runs the C<clean> method on all items contained in the bundle.
  6278.  
  6279. =item CPAN::Bundle::contains()
  6280.  
  6281. Returns a list of objects' IDs contained in a bundle. The associated
  6282. objects may be bundles, modules or distributions.
  6283.  
  6284. =item CPAN::Bundle::force($method,@args)
  6285.  
  6286. Forces CPAN to perform a task that normally would have failed. Force
  6287. takes as arguments a method name to be called and any number of
  6288. additional arguments that should be passed to the called method. The
  6289. internals of the object get the needed changes so that CPAN.pm does
  6290. not refuse to take the action. The C<force> is passed recursively to
  6291. all contained objects.
  6292.  
  6293. =item CPAN::Bundle::get()
  6294.  
  6295. Recursively runs the C<get> method on all items contained in the bundle
  6296.  
  6297. =item CPAN::Bundle::inst_file()
  6298.  
  6299. Returns the highest installed version of the bundle in either @INC or
  6300. C<$CPAN::Config->{cpan_home}>. Note that this is different from
  6301. CPAN::Module::inst_file.
  6302.  
  6303. =item CPAN::Bundle::inst_version()
  6304.  
  6305. Like CPAN::Bundle::inst_file, but returns the $VERSION
  6306.  
  6307. =item CPAN::Bundle::uptodate()
  6308.  
  6309. Returns 1 if the bundle itself and all its members are uptodate.
  6310.  
  6311. =item CPAN::Bundle::install()
  6312.  
  6313. Recursively runs the C<install> method on all items contained in the bundle
  6314.  
  6315. =item CPAN::Bundle::make()
  6316.  
  6317. Recursively runs the C<make> method on all items contained in the bundle
  6318.  
  6319. =item CPAN::Bundle::readme()
  6320.  
  6321. Recursively runs the C<readme> method on all items contained in the bundle
  6322.  
  6323. =item CPAN::Bundle::test()
  6324.  
  6325. Recursively runs the C<test> method on all items contained in the bundle
  6326.  
  6327. =item CPAN::Distribution::as_glimpse()
  6328.  
  6329. Returns a one-line description of the distribution
  6330.  
  6331. =item CPAN::Distribution::as_string()
  6332.  
  6333. Returns a multi-line description of the distribution
  6334.  
  6335. =item CPAN::Distribution::clean()
  6336.  
  6337. Changes to the directory where the distribution has been unpacked and
  6338. runs C<make clean> there.
  6339.  
  6340. =item CPAN::Distribution::containsmods()
  6341.  
  6342. Returns a list of IDs of modules contained in a distribution file.
  6343. Only works for distributions listed in the 02packages.details.txt.gz
  6344. file. This typically means that only the most recent version of a
  6345. distribution is covered.
  6346.  
  6347. =item CPAN::Distribution::cvs_import()
  6348.  
  6349. Changes to the directory where the distribution has been unpacked and
  6350. runs something like
  6351.  
  6352.     cvs -d $cvs_root import -m $cvs_log $cvs_dir $userid v$version
  6353.  
  6354. there.
  6355.  
  6356. =item CPAN::Distribution::dir()
  6357.  
  6358. Returns the directory into which this distribution has been unpacked.
  6359.  
  6360. =item CPAN::Distribution::force($method,@args)
  6361.  
  6362. Forces CPAN to perform a task that normally would have failed. Force
  6363. takes as arguments a method name to be called and any number of
  6364. additional arguments that should be passed to the called method. The
  6365. internals of the object get the needed changes so that CPAN.pm does
  6366. not refuse to take the action.
  6367.  
  6368. =item CPAN::Distribution::get()
  6369.  
  6370. Downloads the distribution from CPAN and unpacks it. Does nothing if
  6371. the distribution has already been downloaded and unpacked within the
  6372. current session.
  6373.  
  6374. =item CPAN::Distribution::install()
  6375.  
  6376. Changes to the directory where the distribution has been unpacked and
  6377. runs the external command C<make install> there. If C<make> has not
  6378. yet been run, it will be run first. A C<make test> will be issued in
  6379. any case and if this fails, the install will be canceled. The
  6380. cancellation can be avoided by letting C<force> run the C<install> for
  6381. you.
  6382.  
  6383. =item CPAN::Distribution::isa_perl()
  6384.  
  6385. Returns 1 if this distribution file seems to be a perl distribution.
  6386. Normally this is derived from the file name only, but the index from
  6387. CPAN can contain a hint to achieve a return value of true for other
  6388. filenames too.
  6389.  
  6390. =item CPAN::Distribution::look()
  6391.  
  6392. Changes to the directory where the distribution has been unpacked and
  6393. opens a subshell there. Exiting the subshell returns.
  6394.  
  6395. =item CPAN::Distribution::make()
  6396.  
  6397. First runs the C<get> method to make sure the distribution is
  6398. downloaded and unpacked. Changes to the directory where the
  6399. distribution has been unpacked and runs the external commands C<perl
  6400. Makefile.PL> and C<make> there.
  6401.  
  6402. =item CPAN::Distribution::prereq_pm()
  6403.  
  6404. Returns the hash reference that has been announced by a distribution
  6405. as the PREREQ_PM hash in the Makefile.PL. Note: works only after an
  6406. attempt has been made to C<make> the distribution. Returns undef
  6407. otherwise.
  6408.  
  6409. =item CPAN::Distribution::readme()
  6410.  
  6411. Downloads the README file associated with a distribution and runs it
  6412. through the pager specified in C<$CPAN::Config->{pager}>.
  6413.  
  6414. =item CPAN::Distribution::test()
  6415.  
  6416. Changes to the directory where the distribution has been unpacked and
  6417. runs C<make test> there.
  6418.  
  6419. =item CPAN::Distribution::uptodate()
  6420.  
  6421. Returns 1 if all the modules contained in the distribution are
  6422. uptodate. Relies on containsmods.
  6423.  
  6424. =item CPAN::Index::force_reload()
  6425.  
  6426. Forces a reload of all indices.
  6427.  
  6428. =item CPAN::Index::reload()
  6429.  
  6430. Reloads all indices if they have been read more than
  6431. C<$CPAN::Config->{index_expire}> days.
  6432.  
  6433. =item CPAN::InfoObj::dump()
  6434.  
  6435. CPAN::Author, CPAN::Bundle, CPAN::Module, and CPAN::Distribution
  6436. inherit this method. It prints the data structure associated with an
  6437. object. Useful for debugging. Note: the data structure is considered
  6438. internal and thus subject to change without notice.
  6439.  
  6440. =item CPAN::Module::as_glimpse()
  6441.  
  6442. Returns a one-line description of the module
  6443.  
  6444. =item CPAN::Module::as_string()
  6445.  
  6446. Returns a multi-line description of the module
  6447.  
  6448. =item CPAN::Module::clean()
  6449.  
  6450. Runs a clean on the distribution associated with this module.
  6451.  
  6452. =item CPAN::Module::cpan_file()
  6453.  
  6454. Returns the filename on CPAN that is associated with the module.
  6455.  
  6456. =item CPAN::Module::cpan_version()
  6457.  
  6458. Returns the latest version of this module available on CPAN.
  6459.  
  6460. =item CPAN::Module::cvs_import()
  6461.  
  6462. Runs a cvs_import on the distribution associated with this module.
  6463.  
  6464. =item CPAN::Module::description()
  6465.  
  6466. Returns a 44 character description of this module. Only available for
  6467. modules listed in The Module List (CPAN/modules/00modlist.long.html
  6468. or 00modlist.long.txt.gz)
  6469.  
  6470. =item CPAN::Module::force($method,@args)
  6471.  
  6472. Forces CPAN to perform a task that normally would have failed. Force
  6473. takes as arguments a method name to be called and any number of
  6474. additional arguments that should be passed to the called method. The
  6475. internals of the object get the needed changes so that CPAN.pm does
  6476. not refuse to take the action.
  6477.  
  6478. =item CPAN::Module::get()
  6479.  
  6480. Runs a get on the distribution associated with this module.
  6481.  
  6482. =item CPAN::Module::inst_file()
  6483.  
  6484. Returns the filename of the module found in @INC. The first file found
  6485. is reported just like perl itself stops searching @INC when it finds a
  6486. module.
  6487.  
  6488. =item CPAN::Module::inst_version()
  6489.  
  6490. Returns the version number of the module in readable format.
  6491.  
  6492. =item CPAN::Module::install()
  6493.  
  6494. Runs an C<install> on the distribution associated with this module.
  6495.  
  6496. =item CPAN::Module::look()
  6497.  
  6498. Changes to the directory where the distribution associated with this
  6499. module has been unpacked and opens a subshell there. Exiting the
  6500. subshell returns.
  6501.  
  6502. =item CPAN::Module::make()
  6503.  
  6504. Runs a C<make> on the distribution associated with this module.
  6505.  
  6506. =item CPAN::Module::manpage_headline()
  6507.  
  6508. If module is installed, peeks into the module's manpage, reads the
  6509. headline and returns it. Moreover, if the module has been downloaded
  6510. within this session, does the equivalent on the downloaded module even
  6511. if it is not installed.
  6512.  
  6513. =item CPAN::Module::readme()
  6514.  
  6515. Runs a C<readme> on the distribution associated with this module.
  6516.  
  6517. =item CPAN::Module::test()
  6518.  
  6519. Runs a C<test> on the distribution associated with this module.
  6520.  
  6521. =item CPAN::Module::uptodate()
  6522.  
  6523. Returns 1 if the module is installed and up-to-date.
  6524.  
  6525. =item CPAN::Module::userid()
  6526.  
  6527. Returns the author's ID of the module.
  6528.  
  6529. =back
  6530.  
  6531. =head2 Cache Manager
  6532.  
  6533. Currently the cache manager only keeps track of the build directory
  6534. ($CPAN::Config->{build_dir}). It is a simple FIFO mechanism that
  6535. deletes complete directories below C<build_dir> as soon as the size of
  6536. all directories there gets bigger than $CPAN::Config->{build_cache}
  6537. (in MB). The contents of this cache may be used for later
  6538. re-installations that you intend to do manually, but will never be
  6539. trusted by CPAN itself. This is due to the fact that the user might
  6540. use these directories for building modules on different architectures.
  6541.  
  6542. There is another directory ($CPAN::Config->{keep_source_where}) where
  6543. the original distribution files are kept. This directory is not
  6544. covered by the cache manager and must be controlled by the user. If
  6545. you choose to have the same directory as build_dir and as
  6546. keep_source_where directory, then your sources will be deleted with
  6547. the same fifo mechanism.
  6548.  
  6549. =head2 Bundles
  6550.  
  6551. A bundle is just a perl module in the namespace Bundle:: that does not
  6552. define any functions or methods. It usually only contains documentation.
  6553.  
  6554. It starts like a perl module with a package declaration and a $VERSION
  6555. variable. After that the pod section looks like any other pod with the
  6556. only difference being that I<one special pod section> exists starting with
  6557. (verbatim):
  6558.  
  6559.     =head1 CONTENTS
  6560.  
  6561. In this pod section each line obeys the format
  6562.  
  6563.         Module_Name [Version_String] [- optional text]
  6564.  
  6565. The only required part is the first field, the name of a module
  6566. (e.g. Foo::Bar, ie. I<not> the name of the distribution file). The rest
  6567. of the line is optional. The comment part is delimited by a dash just
  6568. as in the man page header.
  6569.  
  6570. The distribution of a bundle should follow the same convention as
  6571. other distributions.
  6572.  
  6573. Bundles are treated specially in the CPAN package. If you say 'install
  6574. Bundle::Tkkit' (assuming such a bundle exists), CPAN will install all
  6575. the modules in the CONTENTS section of the pod. You can install your
  6576. own Bundles locally by placing a conformant Bundle file somewhere into
  6577. your @INC path. The autobundle() command which is available in the
  6578. shell interface does that for you by including all currently installed
  6579. modules in a snapshot bundle file.
  6580.  
  6581. =head2 Prerequisites
  6582.  
  6583. If you have a local mirror of CPAN and can access all files with
  6584. "file:" URLs, then you only need a perl better than perl5.003 to run
  6585. this module. Otherwise Net::FTP is strongly recommended. LWP may be
  6586. required for non-UNIX systems or if your nearest CPAN site is
  6587. associated with a URL that is not C<ftp:>.
  6588.  
  6589. If you have neither Net::FTP nor LWP, there is a fallback mechanism
  6590. implemented for an external ftp command or for an external lynx
  6591. command.
  6592.  
  6593. =head2 Finding packages and VERSION
  6594.  
  6595. This module presumes that all packages on CPAN
  6596.  
  6597. =over 2
  6598.  
  6599. =item *
  6600.  
  6601. declare their $VERSION variable in an easy to parse manner. This
  6602. prerequisite can hardly be relaxed because it consumes far too much
  6603. memory to load all packages into the running program just to determine
  6604. the $VERSION variable. Currently all programs that are dealing with
  6605. version use something like this
  6606.  
  6607.     perl -MExtUtils::MakeMaker -le \
  6608.         'print MM->parse_version(shift)' filename
  6609.  
  6610. If you are author of a package and wonder if your $VERSION can be
  6611. parsed, please try the above method.
  6612.  
  6613. =item *
  6614.  
  6615. come as compressed or gzipped tarfiles or as zip files and contain a
  6616. Makefile.PL (well, we try to handle a bit more, but without much
  6617. enthusiasm).
  6618.  
  6619. =back
  6620.  
  6621. =head2 Debugging
  6622.  
  6623. The debugging of this module is a bit complex, because we have
  6624. interferences of the software producing the indices on CPAN, of the
  6625. mirroring process on CPAN, of packaging, of configuration, of
  6626. synchronicity, and of bugs within CPAN.pm.
  6627.  
  6628. For code debugging in interactive mode you can try "o debug" which
  6629. will list options for debugging the various parts of the code. You
  6630. should know that "o debug" has built-in completion support.
  6631.  
  6632. For data debugging there is the C<dump> command which takes the same
  6633. arguments as make/test/install and outputs the object's Data::Dumper
  6634. dump.
  6635.  
  6636. =head2 Floppy, Zip, Offline Mode
  6637.  
  6638. CPAN.pm works nicely without network too. If you maintain machines
  6639. that are not networked at all, you should consider working with file:
  6640. URLs. Of course, you have to collect your modules somewhere first. So
  6641. you might use CPAN.pm to put together all you need on a networked
  6642. machine. Then copy the $CPAN::Config->{keep_source_where} (but not
  6643. $CPAN::Config->{build_dir}) directory on a floppy. This floppy is kind
  6644. of a personal CPAN. CPAN.pm on the non-networked machines works nicely
  6645. with this floppy. See also below the paragraph about CD-ROM support.
  6646.  
  6647. =head1 CONFIGURATION
  6648.  
  6649. When the CPAN module is installed, a site wide configuration file is
  6650. created as CPAN/Config.pm. The default values defined there can be
  6651. overridden in another configuration file: CPAN/MyConfig.pm. You can
  6652. store this file in $HOME/.cpan/CPAN/MyConfig.pm if you want, because
  6653. $HOME/.cpan is added to the search path of the CPAN module before the
  6654. use() or require() statements.
  6655.  
  6656. Currently the following keys in the hash reference $CPAN::Config are
  6657. defined:
  6658.  
  6659.   build_cache        size of cache for directories to build modules
  6660.   build_dir          locally accessible directory to build modules
  6661.   index_expire       after this many days refetch index files
  6662.   cache_metadata     use serializer to cache metadata
  6663.   cpan_home          local directory reserved for this package
  6664.   dontload_hash      anonymous hash: modules in the keys will not be
  6665.                      loaded by the CPAN::has_inst() routine
  6666.   gzip             location of external program gzip
  6667.   inactivity_timeout breaks interactive Makefile.PLs after this
  6668.                      many seconds inactivity. Set to 0 to never break.
  6669.   inhibit_startup_message
  6670.                      if true, does not print the startup message
  6671.   keep_source_where  directory in which to keep the source (if we do)
  6672.   make               location of external make program
  6673.   make_arg         arguments that should always be passed to 'make'
  6674.   make_install_arg   same as make_arg for 'make install'
  6675.   makepl_arg         arguments passed to 'perl Makefile.PL'
  6676.   pager              location of external program more (or any pager)
  6677.   prerequisites_policy
  6678.                      what to do if you are missing module prerequisites
  6679.                      ('follow' automatically, 'ask' me, or 'ignore')
  6680.   proxy_user         username for accessing an authenticating proxy
  6681.   proxy_pass         password for accessing an authenticating proxy
  6682.   scan_cache         controls scanning of cache ('atstart' or 'never')
  6683.   tar                location of external program tar
  6684.   term_is_latin      if true internal UTF-8 is translated to ISO-8859-1
  6685.                      (and nonsense for characters outside latin range)
  6686.   unzip              location of external program unzip
  6687.   urllist         arrayref to nearby CPAN sites (or equivalent locations)
  6688.   wait_list          arrayref to a wait server to try (See CPAN::WAIT)
  6689.   ftp_proxy,      }  the three usual variables for configuring
  6690.     http_proxy,   }  proxy requests. Both as CPAN::Config variables
  6691.     no_proxy      }  and as environment variables configurable.
  6692.  
  6693. You can set and query each of these options interactively in the cpan
  6694. shell with the command set defined within the C<o conf> command:
  6695.  
  6696. =over 2
  6697.  
  6698. =item C<o conf E<lt>scalar optionE<gt>>
  6699.  
  6700. prints the current value of the I<scalar option>
  6701.  
  6702. =item C<o conf E<lt>scalar optionE<gt> E<lt>valueE<gt>>
  6703.  
  6704. Sets the value of the I<scalar option> to I<value>
  6705.  
  6706. =item C<o conf E<lt>list optionE<gt>>
  6707.  
  6708. prints the current value of the I<list option> in MakeMaker's
  6709. neatvalue format.
  6710.  
  6711. =item C<o conf E<lt>list optionE<gt> [shift|pop]>
  6712.  
  6713. shifts or pops the array in the I<list option> variable
  6714.  
  6715. =item C<o conf E<lt>list optionE<gt> [unshift|push|splice] E<lt>listE<gt>>
  6716.  
  6717. works like the corresponding perl commands.
  6718.  
  6719. =back
  6720.  
  6721. =head2 Note on urllist parameter's format
  6722.  
  6723. urllist parameters are URLs according to RFC 1738. We do a little
  6724. guessing if your URL is not compliant, but if you have problems with
  6725. file URLs, please try the correct format. Either:
  6726.  
  6727.     file://localhost/whatever/ftp/pub/CPAN/
  6728.  
  6729. or
  6730.  
  6731.     file:///home/ftp/pub/CPAN/
  6732.  
  6733. =head2 urllist parameter has CD-ROM support
  6734.  
  6735. The C<urllist> parameter of the configuration table contains a list of
  6736. URLs that are to be used for downloading. If the list contains any
  6737. C<file> URLs, CPAN always tries to get files from there first. This
  6738. feature is disabled for index files. So the recommendation for the
  6739. owner of a CD-ROM with CPAN contents is: include your local, possibly
  6740. outdated CD-ROM as a C<file> URL at the end of urllist, e.g.
  6741.  
  6742.   o conf urllist push file://localhost/CDROM/CPAN
  6743.  
  6744. CPAN.pm will then fetch the index files from one of the CPAN sites
  6745. that come at the beginning of urllist. It will later check for each
  6746. module if there is a local copy of the most recent version.
  6747.  
  6748. Another peculiarity of urllist is that the site that we could
  6749. successfully fetch the last file from automatically gets a preference
  6750. token and is tried as the first site for the next request. So if you
  6751. add a new site at runtime it may happen that the previously preferred
  6752. site will be tried another time. This means that if you want to disallow
  6753. a site for the next transfer, it must be explicitly removed from
  6754. urllist.
  6755.  
  6756. =head1 SECURITY
  6757.  
  6758. There's no strong security layer in CPAN.pm. CPAN.pm helps you to
  6759. install foreign, unmasked, unsigned code on your machine. We compare
  6760. to a checksum that comes from the net just as the distribution file
  6761. itself. If somebody has managed to tamper with the distribution file,
  6762. they may have as well tampered with the CHECKSUMS file. Future
  6763. development will go towards strong authentication.
  6764.  
  6765. =head1 EXPORT
  6766.  
  6767. Most functions in package CPAN are exported per default. The reason
  6768. for this is that the primary use is intended for the cpan shell or for
  6769. one-liners.
  6770.  
  6771. =head1 POPULATE AN INSTALLATION WITH LOTS OF MODULES
  6772.  
  6773. Populating a freshly installed perl with my favorite modules is pretty
  6774. easy if you maintain a private bundle definition file. To get a useful
  6775. blueprint of a bundle definition file, the command autobundle can be used
  6776. on the CPAN shell command line. This command writes a bundle definition
  6777. file for all modules that are installed for the currently running perl
  6778. interpreter. It's recommended to run this command only once and from then
  6779. on maintain the file manually under a private name, say
  6780. Bundle/my_bundle.pm. With a clever bundle file you can then simply say
  6781.  
  6782.     cpan> install Bundle::my_bundle
  6783.  
  6784. then answer a few questions and then go out for a coffee.
  6785.  
  6786. Maintaining a bundle definition file means keeping track of two
  6787. things: dependencies and interactivity. CPAN.pm sometimes fails on
  6788. calculating dependencies because not all modules define all MakeMaker
  6789. attributes correctly, so a bundle definition file should specify
  6790. prerequisites as early as possible. On the other hand, it's a bit
  6791. annoying that many distributions need some interactive configuring. So
  6792. what I try to accomplish in my private bundle file is to have the
  6793. packages that need to be configured early in the file and the gentle
  6794. ones later, so I can go out after a few minutes and leave CPAN.pm
  6795. untended.
  6796.  
  6797. =head1 WORKING WITH CPAN.pm BEHIND FIREWALLS
  6798.  
  6799. Thanks to Graham Barr for contributing the following paragraphs about
  6800. the interaction between perl, and various firewall configurations. For
  6801. further informations on firewalls, it is recommended to consult the
  6802. documentation that comes with the ncftp program. If you are unable to
  6803. go through the firewall with a simple Perl setup, it is very likely
  6804. that you can configure ncftp so that it works for your firewall.
  6805.  
  6806. =head2 Three basic types of firewalls
  6807.  
  6808. Firewalls can be categorized into three basic types.
  6809.  
  6810. =over 4
  6811.  
  6812. =item http firewall
  6813.  
  6814. This is where the firewall machine runs a web server and to access the
  6815. outside world you must do it via the web server. If you set environment
  6816. variables like http_proxy or ftp_proxy to a values beginning with http://
  6817. or in your web browser you have to set proxy information then you know
  6818. you are running an http firewall.
  6819.  
  6820. To access servers outside these types of firewalls with perl (even for
  6821. ftp) you will need to use LWP.
  6822.  
  6823. =item ftp firewall
  6824.  
  6825. This where the firewall machine runs an ftp server. This kind of
  6826. firewall will only let you access ftp servers outside the firewall.
  6827. This is usually done by connecting to the firewall with ftp, then
  6828. entering a username like "user@outside.host.com"
  6829.  
  6830. To access servers outside these type of firewalls with perl you
  6831. will need to use Net::FTP.
  6832.  
  6833. =item One way visibility
  6834.  
  6835. I say one way visibility as these firewalls try to make themselves look
  6836. invisible to the users inside the firewall. An FTP data connection is
  6837. normally created by sending the remote server your IP address and then
  6838. listening for the connection. But the remote server will not be able to
  6839. connect to you because of the firewall. So for these types of firewall
  6840. FTP connections need to be done in a passive mode.
  6841.  
  6842. There are two that I can think off.
  6843.  
  6844. =over 4
  6845.  
  6846. =item SOCKS
  6847.  
  6848. If you are using a SOCKS firewall you will need to compile perl and link
  6849. it with the SOCKS library, this is what is normally called a 'socksified'
  6850. perl. With this executable you will be able to connect to servers outside
  6851. the firewall as if it is not there.
  6852.  
  6853. =item IP Masquerade
  6854.  
  6855. This is the firewall implemented in the Linux kernel, it allows you to
  6856. hide a complete network behind one IP address. With this firewall no
  6857. special compiling is needed as you can access hosts directly.
  6858.  
  6859. =back
  6860.  
  6861. =back
  6862.  
  6863. =head2 Configuring lynx or ncftp for going through a firewall
  6864.  
  6865. If you can go through your firewall with e.g. lynx, presumably with a
  6866. command such as
  6867.  
  6868.     /usr/local/bin/lynx -pscott:tiger
  6869.  
  6870. then you would configure CPAN.pm with the command
  6871.  
  6872.     o conf lynx "/usr/local/bin/lynx -pscott:tiger"
  6873.  
  6874. That's all. Similarly for ncftp or ftp, you would configure something
  6875. like
  6876.  
  6877.     o conf ncftp "/usr/bin/ncftp -f /home/scott/ncftplogin.cfg"
  6878.  
  6879. Your mileage may vary...
  6880.  
  6881. =head1 FAQ
  6882.  
  6883. =over 4
  6884.  
  6885. =item 1)
  6886.  
  6887. I installed a new version of module X but CPAN keeps saying,
  6888. I have the old version installed
  6889.  
  6890. Most probably you B<do> have the old version installed. This can
  6891. happen if a module installs itself into a different directory in the
  6892. @INC path than it was previously installed. This is not really a
  6893. CPAN.pm problem, you would have the same problem when installing the
  6894. module manually. The easiest way to prevent this behaviour is to add
  6895. the argument C<UNINST=1> to the C<make install> call, and that is why
  6896. many people add this argument permanently by configuring
  6897.  
  6898.   o conf make_install_arg UNINST=1
  6899.  
  6900. =item 2)
  6901.  
  6902. So why is UNINST=1 not the default?
  6903.  
  6904. Because there are people who have their precise expectations about who
  6905. may install where in the @INC path and who uses which @INC array. In
  6906. fine tuned environments C<UNINST=1> can cause damage.
  6907.  
  6908. =item 3)
  6909.  
  6910. I want to clean up my mess, and install a new perl along with
  6911. all modules I have. How do I go about it?
  6912.  
  6913. Run the autobundle command for your old perl and optionally rename the
  6914. resulting bundle file (e.g. Bundle/mybundle.pm), install the new perl
  6915. with the Configure option prefix, e.g.
  6916.  
  6917.     ./Configure -Dprefix=/usr/local/perl-5.6.78.9
  6918.  
  6919. Install the bundle file you produced in the first step with something like
  6920.  
  6921.     cpan> install Bundle::mybundle
  6922.  
  6923. and you're done.
  6924.  
  6925. =item 4)
  6926.  
  6927. When I install bundles or multiple modules with one command
  6928. there is too much output to keep track of.
  6929.  
  6930. You may want to configure something like
  6931.  
  6932.   o conf make_arg "| tee -ai /root/.cpan/logs/make.out"
  6933.   o conf make_install_arg "| tee -ai /root/.cpan/logs/make_install.out"
  6934.  
  6935. so that STDOUT is captured in a file for later inspection.
  6936.  
  6937.  
  6938. =item 5)
  6939.  
  6940. I am not root, how can I install a module in a personal directory?
  6941.  
  6942. You will most probably like something like this:
  6943.  
  6944.   o conf makepl_arg "LIB=~/myperl/lib \
  6945.                     INSTALLMAN1DIR=~/myperl/man/man1 \
  6946.                     INSTALLMAN3DIR=~/myperl/man/man3"
  6947.   install Sybase::Sybperl
  6948.  
  6949. You can make this setting permanent like all C<o conf> settings with
  6950. C<o conf commit>.
  6951.  
  6952. You will have to add ~/myperl/man to the MANPATH environment variable
  6953. and also tell your perl programs to look into ~/myperl/lib, e.g. by
  6954. including
  6955.  
  6956.   use lib "$ENV{HOME}/myperl/lib";
  6957.  
  6958. or setting the PERL5LIB environment variable.
  6959.  
  6960. Another thing you should bear in mind is that the UNINST parameter
  6961. should never be set if you are not root.
  6962.  
  6963. =item 6)
  6964.  
  6965. How to get a package, unwrap it, and make a change before building it?
  6966.  
  6967.   look Sybase::Sybperl
  6968.  
  6969. =item 7)
  6970.  
  6971. I installed a Bundle and had a couple of fails. When I
  6972. retried, everything resolved nicely. Can this be fixed to work
  6973. on first try?
  6974.  
  6975. The reason for this is that CPAN does not know the dependencies of all
  6976. modules when it starts out. To decide about the additional items to
  6977. install, it just uses data found in the generated Makefile. An
  6978. undetected missing piece breaks the process. But it may well be that
  6979. your Bundle installs some prerequisite later than some depending item
  6980. and thus your second try is able to resolve everything. Please note,
  6981. CPAN.pm does not know the dependency tree in advance and cannot sort
  6982. the queue of things to install in a topologically correct order. It
  6983. resolves perfectly well IFF all modules declare the prerequisites
  6984. correctly with the PREREQ_PM attribute to MakeMaker. For bundles which
  6985. fail and you need to install often, it is recommended sort the Bundle
  6986. definition file manually. It is planned to improve the metadata
  6987. situation for dependencies on CPAN in general, but this will still
  6988. take some time.
  6989.  
  6990. =item 8)
  6991.  
  6992. In our intranet we have many modules for internal use. How
  6993. can I integrate these modules with CPAN.pm but without uploading
  6994. the modules to CPAN?
  6995.  
  6996. Have a look at the CPAN::Site module.
  6997.  
  6998. =item 9)
  6999.  
  7000. When I run CPAN's shell, I get error msg about line 1 to 4,
  7001. setting meta input/output via the /etc/inputrc file.
  7002.  
  7003. Some versions of readline are picky about capitalization in the
  7004. /etc/inputrc file and specifically RedHat 6.2 comes with a
  7005. /etc/inputrc that contains the word C<on> in lowercase. Change the
  7006. occurrences of C<on> to C<On> and the bug should disappear.
  7007.  
  7008. =item 10)
  7009.  
  7010. Some authors have strange characters in their names.
  7011.  
  7012. Internally CPAN.pm uses the UTF-8 charset. If your terminal is
  7013. expecting ISO-8859-1 charset, a converter can be activated by setting
  7014. term_is_latin to a true value in your config file. One way of doing so
  7015. would be
  7016.  
  7017.     cpan> ! $CPAN::Config->{term_is_latin}=1
  7018.  
  7019. Extended support for converters will be made available as soon as perl
  7020. becomes stable with regard to charset issues.
  7021.  
  7022. =back
  7023.  
  7024. =head1 BUGS
  7025.  
  7026. We should give coverage for B<all> of the CPAN and not just the PAUSE
  7027. part, right? In this discussion CPAN and PAUSE have become equal --
  7028. but they are not. PAUSE is authors/, modules/ and scripts/. CPAN is
  7029. PAUSE plus the clpa/, doc/, misc/, ports/, and src/.
  7030.  
  7031. Future development should be directed towards a better integration of
  7032. the other parts.
  7033.  
  7034. If a Makefile.PL requires special customization of libraries, prompts
  7035. the user for special input, etc. then you may find CPAN is not able to
  7036. build the distribution. In that case, you should attempt the
  7037. traditional method of building a Perl module package from a shell.
  7038.  
  7039. =head1 AUTHOR
  7040.  
  7041. Andreas Koenig E<lt>andreas.koenig@anima.deE<gt>
  7042.  
  7043. =head1 TRANSLATIONS
  7044.  
  7045. Kawai,Takanori provides a Japanese translation of this manpage at
  7046. http://member.nifty.ne.jp/hippo2000/perltips/CPAN.htm
  7047.  
  7048. =head1 SEE ALSO
  7049.  
  7050. perl(1), CPAN::Nox(3)
  7051.  
  7052. =cut
  7053.  
  7054.