Skip to content

Commit edfb7b9

Browse files
avargitster
authored andcommitted
perl: generalize the Git::LoadCPAN facility
Change the two wrappers that load from CPAN (local OS) or our own copy to do so via the same codepath. I added the Error.pm wrapper in 20d2a30 ("Makefile: replace perl/Makefile.PL with simple make rules", 2017-12-10), and shortly afterwards Matthieu Moy added a wrapper for Mail::Address in bd869f6 ("send-email: add and use a local copy of Mail::Address", 2018-01-05). His loader was simpler since Mail::Address doesn't have an "import" method, but didn't do the same sanity checking; For example, a missing FromCPAN directory (which OS packages are likely not to have) wouldn't be explicitly warned about as a "BUG: ...". Update both to use a common implementation based on the previous Error.pm loader. Which has been amended to take the module to load as parameter, as well as whether or not that module has an import method. This loader should be generic enough to handle almost all CPAN modules out there, some use some crazy loading magic and wouldn't like being wrapped like this, but that would be immediately obvious, and we'd find out right away since the module wouldn't work at all. Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2865467 commit edfb7b9

File tree

3 files changed

+82
-58
lines changed

3 files changed

+82
-58
lines changed

perl/Git/LoadCPAN.pm

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package Git::LoadCPAN;
2+
use 5.008;
3+
use strict;
4+
use warnings;
5+
6+
=head1 NAME
7+
8+
Git::LoadCPAN - Wrapper for loading modules from the CPAN (OS) or Git's own copy
9+
10+
=head1 DESCRIPTION
11+
12+
The Perl code in Git depends on some modules from the CPAN, but we
13+
don't want to make those a hard requirement for anyone building from
14+
source.
15+
16+
Therefore the L<Git::LoadCPAN> namespace shipped with Git contains
17+
wrapper modules like C<Git::LoadCPAN::Module::Name> that will first
18+
attempt to load C<Module::Name> from the OS, and if that doesn't work
19+
will fall back on C<Git::FromCPAN::Module::Name> shipped with Git
20+
itself.
21+
22+
Usually distributors will not ship with Git's Git::FromCPAN tree at
23+
all, preferring to use their own packaging of CPAN modules instead.
24+
25+
This module is only intended to be used for code shipping in the
26+
C<git.git> repository. Use it for anything else at your peril!
27+
28+
=cut
29+
30+
sub 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+
74+
1;

perl/Git/LoadCPAN/Error.pm

Lines changed: 4 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,9 @@ package Git::LoadCPAN::Error;
22
use 5.008;
33
use strict;
44
use warnings;
5-
6-
=head1 NAME
7-
8-
Git::LoadCPAN::Error - Wrapper for the L<Error> module, in case it's not installed
9-
10-
=head1 DESCRIPTION
11-
12-
Wraps the import function for the L<Error> module.
13-
14-
This module is only intended to be used for code shipping in the
15-
C<git.git> repository. Use it for anything else at your peril!
16-
17-
=cut
18-
19-
sub import {
20-
shift;
21-
my $caller = caller;
22-
23-
eval {
24-
require Error;
25-
1;
26-
} or do {
27-
my $error = $@ || "Zombie Error";
28-
29-
my $Git_Error_pm_path = $INC{"Git/LoadCPAN/Error.pm"} || die "BUG: Should have our own path from %INC!";
30-
31-
require File::Basename;
32-
my $Git_Error_pm_root = File::Basename::dirname($Git_Error_pm_path) || die "BUG: Can't figure out lib/Git dirname from '$Git_Error_pm_path'!";
33-
34-
require File::Spec;
35-
my $Git_pm_FromCPAN_root = File::Spec->catdir($Git_Error_pm_root, '..', 'FromCPAN');
36-
die "BUG: '$Git_pm_FromCPAN_root' should be a directory!" unless -d $Git_pm_FromCPAN_root;
37-
38-
local @INC = ($Git_pm_FromCPAN_root, @INC);
39-
require Error;
40-
};
41-
42-
unshift @_, $caller;
43-
goto &Error::import;
44-
}
5+
use Git::LoadCPAN (
6+
module => 'Error',
7+
import => 1,
8+
);
459

4610
1;

perl/Git/LoadCPAN/Mail/Address.pm

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,9 @@ package Git::LoadCPAN::Mail::Address;
22
use 5.008;
33
use strict;
44
use warnings;
5-
6-
=head1 NAME
7-
8-
Git::LoadCPAN::Mail::Address - Wrapper for the L<Mail::Address> module, in case it's not installed
9-
10-
=head1 DESCRIPTION
11-
12-
This module is only intended to be used for code shipping in the
13-
C<git.git> repository. Use it for anything else at your peril!
14-
15-
=cut
16-
17-
eval {
18-
require Mail::Address;
19-
1;
20-
} or do {
21-
require Git::FromCPAN::Mail::Address;
22-
};
5+
use Git::LoadCPAN (
6+
module => 'Mail::Address',
7+
import => 0,
8+
);
239

2410
1;

0 commit comments

Comments
 (0)