[PATCH] Git.pm: Make File::Spec and File::Temp requirement lazy

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This will ensure that the API at large is accessible to nearly
all Perl versions, while only the temp file caching API is tied to
the File::Temp and File::Spec modules being available.

Signed-off-by: Marcus Griep <marcus@xxxxxxxx>
---

 Eric Wong wrote:
 > What about just lazy requiring inside _temp_cache() so it
 > won't get loaded by folks that don't need it? (completely untested):
 > 
 > 		eval { require File::Temp };
 > 		if ($@) {
 > 			throw Error::Simple("couldn't require File::Temp: $@");
 > 		}
 > 		eval { require File::Spec };
 > 		if ($@) {
 > 			throw Error::Simple("couldn't require File::Spec: $@");
 > 		}
 > 
 > It'll also remove the minor performance hit CGI/gitweb users got since
 > we won't load these extra modules during startup.

 This recommendation is implemented with this patch, but in such a way that only
 the first test will be used, and that result cached.  That way we aren't doing
 a compile _every_ time we want a temporary file, just the first time.

 perl/Git.pm |   14 ++++++++++++--
 1 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/perl/Git.pm b/perl/Git.pm
index 405f68f..9b6b637 100644
--- a/perl/Git.pm
+++ b/perl/Git.pm
@@ -100,8 +100,6 @@ use Carp qw(carp croak); # but croak is bad - throw instead
 use Error qw(:try);
 use Cwd qw(abs_path);
 use IPC::Open2 qw(open2);
-use File::Temp ();
-require File::Spec;
 use Fcntl qw(SEEK_SET SEEK_CUR);
 }
 
@@ -940,6 +938,7 @@ sub _close_cat_blob {
 { # %TEMP_* Lexical Context
 
 my (%TEMP_LOCKS, %TEMP_FILES);
+my $require_test;
 
 =item temp_acquire ( NAME )
 
@@ -1009,6 +1008,8 @@ sub temp_release {
 sub _temp_cache {
 	my ($name) = @_;
 
+	_verify_require();
+
 	my $temp_fd = \$TEMP_FILES{$name};
 	if (defined $$temp_fd and $$temp_fd->opened) {
 		if ($TEMP_LOCKS{$$temp_fd}) {
@@ -1031,6 +1032,15 @@ sub _temp_cache {
 	$$temp_fd;
 }
 
+sub _verify_require {
+	unless (defined $require_test) {
+		$require_test = "";
+		eval { require File::Temp; require File::Spec; };
+		$require_test .= "$@";
+	}
+	$require_test and throw Error::Simple($require_test);
+}
+
 =item temp_reset ( FILEHANDLE )
 
 Truncates and resets the position of the C<FILEHANDLE>.
-- 
1.6.0.rc2.6.g8eda3

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux