1568c177fed0c9612dfc3bce4a903c74e303fed3
   1package Git::LoadCPAN;
   2use 5.008;
   3use strict;
   4use warnings;
   5
   6=head1 NAME
   7
   8Git::LoadCPAN - Wrapper for loading modules from the CPAN (OS) or Git's own copy
   9
  10=head1 DESCRIPTION
  11
  12The Perl code in Git depends on some modules from the CPAN, but we
  13don't want to make those a hard requirement for anyone building from
  14source.
  15
  16Therefore the L<Git::LoadCPAN> namespace shipped with Git contains
  17wrapper modules like C<Git::LoadCPAN::Module::Name> that will first
  18attempt to load C<Module::Name> from the OS, and if that doesn't work
  19will fall back on C<Git::FromCPAN::Module::Name> shipped with Git
  20itself.
  21
  22Usually distributors will not ship with Git's Git::FromCPAN tree at
  23all, preferring to use their own packaging of CPAN modules instead.
  24
  25This module is only intended to be used for code shipping in the
  26C<git.git> repository. Use it for anything else at your peril!
  27
  28=cut
  29
  30sub import {
  31        shift;
  32        my $caller = caller;
  33        my %args = @_;
  34        my $module = exists $args{module} ? delete $args{module} : die "BUG: Expected 'module' parameter!";
  35        my $import = exists $args{import} ? delete $args{import} : die "BUG: Expected 'import' parameter!";
  36        die "BUG: Too many arguments!" if keys %args;
  37
  38        # Foo::Bar to Foo/Bar.pm
  39        my $package_pm = $module;
  40        $package_pm =~ s[::][/]g;
  41        $package_pm .= '.pm';
  42
  43        eval {
  44                require $package_pm;
  45                1;
  46        } or do {
  47                my $error = $@ || "Zombie Error";
  48
  49                my $Git_LoadCPAN_pm_path = $INC{"Git/LoadCPAN.pm"} || die "BUG: Should have our own path from %INC!";
  50
  51                require File::Basename;
  52                my $Git_LoadCPAN_pm_root = File::Basename::dirname($Git_LoadCPAN_pm_path) || die "BUG: Can't figure out lib/Git dirname from '$Git_LoadCPAN_pm_path'!";
  53
  54                require File::Spec;
  55                my $Git_pm_FromCPAN_root = File::Spec->catdir($Git_LoadCPAN_pm_root, 'FromCPAN');
  56                die "BUG: '$Git_pm_FromCPAN_root' should be a directory!" unless -d $Git_pm_FromCPAN_root;
  57
  58                local @INC = ($Git_pm_FromCPAN_root, @INC);
  59                require $package_pm;
  60        };
  61
  62        if ($import) {
  63                no strict 'refs';
  64                *{"${caller}::import"} = sub {
  65                        shift;
  66                        use strict 'refs';
  67                        unshift @_, $module;
  68                        goto &{"${module}::import"};
  69                };
  70                use strict 'refs';
  71        }
  72}
  73
  741;