git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v3 10/13] perl: generalize the Git::LoadCPAN facility

From
Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Date
Mar 3, 2018, 15:38 UTC
Message-ID
<20180303153817.20270-11-avarab@gmail.com>
In-Reply-To
<20180303153817.20270-1-avarab@gmail.com>

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 20d2a30f8f ("Makefile: replace perl/Makefile.PL with simple make rules", 2017-12-10), and shortly afterwards Matthieu Moy added a wrapper for Mail::Address in bd869f67b9 ("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 <avarab@gmail.com>
---
 perl/Git/LoadCPAN.pm              | 74 +++++++++++++++++++++++++++++++++++++++
 perl/Git/LoadCPAN/Error.pm        | 44 +++--------------------
 perl/Git/LoadCPAN/Mail/Address.pm | 22 +++---------
 3 files changed, 82 insertions(+), 58 deletions(-)
 create mode 100644 perl/Git/LoadCPAN.pm
diff --git a/perl/Git/LoadCPAN.pm b/perl/Git/LoadCPAN.pm
new file mode 100644
index 0000000000..1568c177fe
--- /dev/null
+++ b/perl/Git/LoadCPAN.pm
@@ -0,0 +1,74 @@
+package Git::LoadCPAN;
+use 5.008;
+use strict;
+use warnings;
+
+=head1 NAME
+
+Git::LoadCPAN - Wrapper for loading modules from the CPAN (OS) or Git's own copy
+
+=head1 DESCRIPTION
+
+The Perl code in Git depends on some modules from the CPAN, but we
+don't want to make those a hard requirement for anyone building from
+source.
+
+Therefore the L<Git::LoadCPAN> namespace shipped with Git contains
+wrapper modules like C<Git::LoadCPAN::Module::Name> that will first
+attempt to load C<Module::Name> from the OS, and if that doesn't work
+will fall back on C<Git::FromCPAN::Module::Name> shipped with Git
+itself.
+
+Usually distributors will not ship with Git's Git::FromCPAN tree at
+all, preferring to use their own packaging of CPAN modules instead.
+
+This module is only intended to be used for code shipping in the
+C<git.git> repository. Use it for anything else at your peril!
+
+=cut
+
+sub import {
+	shift;
+	my $caller = caller;
+	my %args = @_;
+	my $module = exists $args{module} ? delete $args{module} : die "BUG: Expected 'module' parameter!";
+	my $import = exists $args{import} ? delete $args{import} : die "BUG: Expected 'import' parameter!";
+	die "BUG: Too many arguments!" if keys %args;
+
+	# Foo::Bar to Foo/Bar.pm
+	my $package_pm = $module;
+	$package_pm =~ s[::][/]g;
+	$package_pm .= '.pm';
+
+	eval {
+		require $package_pm;
+		1;
+	} or do {
+		my $error = $@ || "Zombie Error";
+
+		my $Git_LoadCPAN_pm_path = $INC{"Git/LoadCPAN.pm"} || die "BUG: Should have our own path from %INC!";
+
+		require File::Basename;
+		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'!";
+
+		require File::Spec;
+		my $Git_pm_FromCPAN_root = File::Spec->catdir($Git_LoadCPAN_pm_root, 'FromCPAN');
+		die "BUG: '$Git_pm_FromCPAN_root' should be a directory!" unless -d $Git_pm_FromCPAN_root;
+
+		local @INC = ($Git_pm_FromCPAN_root, @INC);
+		require $package_pm;
+	};
+
+	if ($import) {
+		no strict 'refs';
+		*{"${caller}::import"} = sub {
+			shift;
+			use strict 'refs';
+			unshift @_, $module;
+			goto &{"${module}::import"};
+		};
+		use strict 'refs';
+	}
+}
+
+1;
diff --git a/perl/Git/LoadCPAN/Error.pm b/perl/Git/LoadCPAN/Error.pm
index 3513fe745b..c6d2c45d80 100644
--- a/perl/Git/LoadCPAN/Error.pm
+++ b/perl/Git/LoadCPAN/Error.pm
@@ -2,45 +2,9 @@ package Git::LoadCPAN::Error;
 use 5.008;
 use strict;
 use warnings;
-
-=head1 NAME
-
-Git::LoadCPAN::Error - Wrapper for the L<Error> module, in case it's not installed
-
-=head1 DESCRIPTION
-
-Wraps the import function for the L<Error> module.
-
-This module is only intended to be used for code shipping in the
-C<git.git> repository. Use it for anything else at your peril!
-
-=cut
-
-sub import {
-    shift;
-    my $caller = caller;
-
-    eval {
-	require Error;
-	1;
-    } or do {
-	my $error = $@ || "Zombie Error";
-
-	my $Git_Error_pm_path = $INC{"Git/LoadCPAN/Error.pm"} || die "BUG: Should have our own path from %INC!";
-
-	require File::Basename;
-	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'!";
-
-	require File::Spec;
-	my $Git_pm_FromCPAN_root = File::Spec->catdir($Git_Error_pm_root, '..', 'FromCPAN');
-	die "BUG: '$Git_pm_FromCPAN_root' should be a directory!" unless -d $Git_pm_FromCPAN_root;
-
-	local @INC = ($Git_pm_FromCPAN_root, @INC);
-	require Error;
-    };
-
-    unshift @_, $caller;
-    goto &Error::import;
-}
+use Git::LoadCPAN (
+	module => 'Error',
+	import => 1,
+);
 
 1;
diff --git a/perl/Git/LoadCPAN/Mail/Address.pm b/perl/Git/LoadCPAN/Mail/Address.pm
index 879c2f5cd1..f70a4f064c 100644
--- a/perl/Git/LoadCPAN/Mail/Address.pm
+++ b/perl/Git/LoadCPAN/Mail/Address.pm
@@ -2,23 +2,9 @@ package Git::LoadCPAN::Mail::Address;
 use 5.008;
 use strict;
 use warnings;
-
-=head1 NAME
-
-Git::LoadCPAN::Mail::Address - Wrapper for the L<Mail::Address> module, in case it's not installed
-
-=head1 DESCRIPTION
-
-This module is only intended to be used for code shipping in the
-C<git.git> repository. Use it for anything else at your peril!
-
-=cut
-
-eval {
-    require Mail::Address;
-    1;
-} or do {
-    require Git::FromCPAN::Mail::Address;
-};
+use Git::LoadCPAN (
+	module => 'Mail::Address',
+	import => 0,
+);
 
 1;
-- 
2.15.1.424.g9478a66081
Previous: Ævar Arnfjörð BjarmasonNext: Ævar Arnfjörð Bjarmason
Message 59 of 66 in “various perl fixes”
  1. 0/8 various perl fixesÆvar Arnfjörð Bjarmason, Feb 14, 2018
  2. 1/8 perl: *.pm files should not have the executable bitÆvar Arnfjörð Bjarmason, Feb 14, 2018
  3. Jonathan NiederFeb 14, 2018
  4. Ævar Arnfjörð BjarmasonFeb 25, 2018
  5. 2/8 perl: move CPAN loader wrappers to another namespaceÆvar Arnfjörð Bjarmason, Feb 14, 2018
  6. Jonathan NiederFeb 14, 2018
  7. 4/8 perl: update our ancient copy of Error.pmÆvar Arnfjörð Bjarmason, Feb 14, 2018
  8. Jonathan NiederFeb 14, 2018
  9. Ævar Arnfjörð BjarmasonFeb 15, 2018
  10. 3/8 perl: generalize the Git::LoadCPAN facilityÆvar Arnfjörð Bjarmason, Feb 14, 2018
  11. Jonathan NiederFeb 14, 2018
  12. Todd ZullingerFeb 15, 2018
  13. Ævar Arnfjörð BjarmasonFeb 25, 2018
  14. Randall S. BeckerFeb 25, 2018
  15. Todd ZullingerFeb 15, 2018
  16. Ævar Arnfjörð BjarmasonFeb 15, 2018
  17. Todd ZullingerFeb 15, 2018
  18. Ævar Arnfjörð BjarmasonFeb 16, 2018
  19. Todd ZullingerFeb 16, 2018
  20. Jonathan NiederFeb 16, 2018
  21. Todd ZullingerFeb 17, 2018
  22. Todd ZullingerFeb 17, 2018
  23. 6/8 git-send-email: unconditionally use Net::{SMTP,Domain}Ævar Arnfjörð Bjarmason, Feb 14, 2018
  24. Jonathan NiederFeb 14, 2018
  25. Ævar Arnfjörð BjarmasonFeb 15, 2018
  26. 7/8 gitweb: hard-depend on the Digest::MD5 5.8 moduleÆvar Arnfjörð Bjarmason, Feb 14, 2018
  27. Jonathan NiederFeb 14, 2018
  28. 5/8 perl: update our copy of Mail::AddressÆvar Arnfjörð Bjarmason, Feb 14, 2018
  29. Jonathan NiederFeb 14, 2018
  30. Matthieu MoyFeb 15, 2018
  31. Ævar Arnfjörð BjarmasonFeb 15, 2018
  32. 8/8 perl: hard-depend on the File::{Temp,Spec} modulesÆvar Arnfjörð Bjarmason, Feb 14, 2018
  33. Jonathan NiederFeb 14, 2018
  34. Ævar Arnfjörð BjarmasonFeb 15, 2018
  35. Junio C HamanoFeb 15, 2018
  36. 00/13 various perl fixesÆvar Arnfjörð Bjarmason, Feb 25, 2018
  37. 01/13 perl: *.pm files should not have the executable bitÆvar Arnfjörð Bjarmason, Feb 25, 2018
  38. 02/13 Git.pm: remove redundant "use strict" from sub-packageÆvar Arnfjörð Bjarmason, Feb 25, 2018
  39. 03/13 Git.pm: add the "use warnings" pragmaÆvar Arnfjörð Bjarmason, Feb 25, 2018
  40. 04/13 gitweb: hard-depend on the Digest::MD5 5.8 moduleÆvar Arnfjörð Bjarmason, Feb 25, 2018
  41. Eric SunshineFeb 25, 2018
  42. 05/13 Git.pm: hard-depend on the File::{Temp,Spec} modulesÆvar Arnfjörð Bjarmason, Feb 25, 2018
  43. 07/13 perl: update our ancient copy of Error.pmÆvar Arnfjörð Bjarmason, Feb 25, 2018
  44. 06/13 git-send-email: unconditionally use Net::{SMTP,Domain}Ævar Arnfjörð Bjarmason, Feb 25, 2018
  45. Eric SunshineFeb 25, 2018
  46. 09/13 perl: move CPAN loader wrappers to another namespaceÆvar Arnfjörð Bjarmason, Feb 25, 2018
  47. 08/13 perl: update our copy of Mail::AddressÆvar Arnfjörð Bjarmason, Feb 25, 2018
  48. 10/13 perl: generalize the Git::LoadCPAN facilityÆvar Arnfjörð Bjarmason, Feb 25, 2018
  49. 11/13 perl: move the perl/Git/FromCPAN tree to perl/FromCPANÆvar Arnfjörð Bjarmason, Feb 25, 2018
  50. 13/13 perl Git::LoadCPAN: emit better errors under NO_PERL_CPAN_FALLBACKSÆvar Arnfjörð Bjarmason, Feb 25, 2018
  51. 12/13 Makefile: add NO_PERL_CPAN_FALLBACKS knobÆvar Arnfjörð Bjarmason, Feb 25, 2018
  52. 00/13 various perl fixesÆvar Arnfjörð Bjarmason, Mar 3, 2018
  53. 01/13 perl: *.pm files should not have the executable bitÆvar Arnfjörð Bjarmason, Mar 3, 2018
  54. 02/13 Git.pm: remove redundant "use strict" from sub-packageÆvar Arnfjörð Bjarmason, Mar 3, 2018
  55. 04/13 gitweb: hard-depend on the Digest::MD5 5.8 moduleÆvar Arnfjörð Bjarmason, Mar 3, 2018
  56. 05/13 Git.pm: hard-depend on the File::{Temp,Spec} modulesÆvar Arnfjörð Bjarmason, Mar 3, 2018
  57. 03/13 Git.pm: add the "use warnings" pragmaÆvar Arnfjörð Bjarmason, Mar 3, 2018
  58. 06/13 git-send-email: unconditionally use Net::{SMTP,Domain}Ævar Arnfjörð Bjarmason, Mar 3, 2018
  59. 10/13 perl: generalize the Git::LoadCPAN facilityÆvar Arnfjörð Bjarmason, Mar 3, 2018
  60. 11/13 perl: move the perl/Git/FromCPAN tree to perl/FromCPANÆvar Arnfjörð Bjarmason, Mar 3, 2018
  61. 12/13 Makefile: add NO_PERL_CPAN_FALLBACKS knobÆvar Arnfjörð Bjarmason, Mar 3, 2018
  62. 13/13 perl Git::LoadCPAN: emit better errors under NO_PERL_CPAN_FALLBACKSÆvar Arnfjörð Bjarmason, Mar 3, 2018
  63. 08/13 perl: update our copy of Mail::AddressÆvar Arnfjörð Bjarmason, Mar 3, 2018
  64. 09/13 perl: move CPAN loader wrappers to another namespaceÆvar Arnfjörð Bjarmason, Mar 3, 2018
  65. 07/13 perl: update our ancient copy of Error.pmÆvar Arnfjörð Bjarmason, Mar 3, 2018
  66. Junio C HamanoMar 5, 2018

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.