--- CDB_File-0.91/CDB_File.xs	Tue Dec 18 17:02:35 2001
+++ CDB_File-0.92/CDB_File.xs	Thu Oct 17 13:39:56 2002
@@ -73,7 +73,7 @@
 #endif
 
 struct cdb {
-	GV *glob;   /* */
+	PerlIO *fh;   /* */
 
 #ifdef HASMMAP
 	char *map;
@@ -221,11 +221,11 @@
 	}
 #endif
 
-	if (PerlIO_seek(IoIFP(GvIOn(c->glob)), pos, SEEK_SET) == -1) return -1;
+	if (PerlIO_seek(c->fh, pos, SEEK_SET) == -1) return -1;
 	while (len > 0) {
 		int r;
 		do
-			r = PerlIO_read(IoIFP(GvIOn(c->glob)), buf, len);
+			r = PerlIO_read(c->fh, buf, len);
 		while ((r == -1) && (errno == EINTR));
 		if (r == -1) return -1;
 		if (r == 0) {
@@ -357,7 +357,9 @@
  # it seems to work, but input from anybody with a deeper
  # understanding would be most welcome.
 
-SV *
+ # Additional: fixed by someone with a deeper understanding ;-) (Matt Sergeant)
+
+void
 cdb_handle(db)
 	SV *		db
 	
@@ -365,14 +367,19 @@
 
 	PREINIT:
 	struct cdb *this;
+        GV *gv;
+        char *packname;
 
 	CODE:
-	this = (struct cdb *)SvPV(SvRV(db), PL_na);
-	RETVAL = newRV_inc((SV *)GvIOn(this->glob));
-
-	OUTPUT:
-		RETVAL
-
+        this = (struct cdb *)SvPV(SvRV(db), PL_na);
+        packname = HvNAME(SvSTASH(SvRV(db)));
+        gv = (GV*)SvREFCNT_inc(newGVgen(packname));
+        hv_delete(GvSTASH(gv), GvNAME(gv), GvNAMELEN(gv), G_DISCARD);
+        /* here we dup the filehandle, because perl space will try and close
+           it when it goes out of scope */
+        IoIFP(GvIOn(gv)) = PerlIO_fdopen(PerlIO_fileno(this->fh), "r");
+        ST(0) = sv_2mortal(newRV_noinc((SV*)gv));
+        
 U32
 cdb_datalen(db)
 	SV *		db
@@ -410,11 +417,8 @@
 	struct cdb cdb;
 	SV *cdbp;
 
-	f = PerlIO_open(filename, "rb");
+	cdb.fh = f = PerlIO_open(filename, "rb");
 	if (!f) XSRETURN_NO;
-	cdb.glob = newGVgen("cdb");
-	io = GvIOn(cdb.glob);
-	IoIFP(io) = f;
 	cdb.end = 0;
 #ifdef HASMMAP
 	{
@@ -579,10 +583,7 @@
 			this->map = 0;
 		}
 #endif
-		io = GvIOn(this->glob);
-		PerlIO_close(IoIFP(io)); /* close() on O_RDONLY cannot fail */
-		IoIFP(io) = Nullfp;
-		SvREFCNT_dec((SV *)this->glob);
+		PerlIO_close(this->fh); /* close() on O_RDONLY cannot fail */
 	} else {
 		struct cdb_make *this;
 
