A recent thread on p5p about the undocumented behavior of sort() in scalar
context revealed that "return sort" does not DWYM when the subroutine is
used in scalar context (you get undef, not the number of elements).
A quick scan showed DBI->available_drivers falls into this trap.
$ perl -MDBI -wle 'print join "\n", DBI->available_drivers; printf "I have %d drivers
installed\n", scalar DBI->available_drivers'
CSV
ExampleP
File
Proxy
Use of uninitialized value in printf at -e line 1.
I have 0 drivers installed
Attached is a work around.
PS I can never find where I'm supposed to send these patches. Neither the
DBI man page nor the README says where patches should go, or even has your
address. Perhaps this is by design. :)
--
This sig file temporarily out of order.
--- DBI.pm 2002/06/12 22:38:11 1.1
+++ DBI.pm 2002/06/12 22:39:22
@@ -806,7 +806,9 @@
}
closedir(DBI::DIR);
}
- return sort @drivers;
+
+ # "return sort @drivers" will not DWIM in scalar context.
+ return wantarray ? sort @drivers : @drivers;
}
sub data_sources {
--- t/01basics.t 2002/06/12 22:39:50 1.1
+++ t/01basics.t 2002/06/12 22:40:35
@@ -40,6 +40,10 @@
ok(0, @drivers);
ok(0, "@drivers" =~ m/ExampleP/i); # ignore case for VMS & Win32
+# Due to a subtle "feature" of sort, this was broken.
+my $num_drivers = DBI->available_drivers;
+ok(0, $num_drivers);
+
$switch->debug(0);
ok(0, 1);
$switch->{DebugDispatch} = 0; # handled by Switch
@@ -100,5 +104,5 @@
ok(0, DBI::hash("foo2",1) == -1263462437, DBI::hash("foo2",1));
}
-BEGIN { $tests = 38 }
+BEGIN { $tests = 39 }
exit 0;