Subject: | Speed enhancements |
Hi Jos,
I've changed things round a bit to speed up the load time of this module.
Please find the patches attached. I've got ahead a build a new dist as
well if you are happy with the patches.
File::Spec::Functions, File::Basename, XML::Simple and Config::IniFiles
are now only loaded if actually needed. YAML has been swapped for
YAML::Any which selects the fastest YAML module available. This has
meant a Makefile.PL dependency update from YAML => 0 to YAML => 0.67.
I also had to update the xml-unavailable.t test file to work with the
new code. I ended up creating a sample XML::Simple file and putting it
in the t folder. The MANIFEST has been updated to reflect this. Changes
file has also been updated.
Let me know if you have any questions. I've commented everything to try
and make my changes clearer.
Lyle
Subject: | Simple.pm |
package XML::Simple;
sub import { die };
1;
Subject: | Auto.pm.patch |
--- Auto.pm.old Mon Mar 09 14:44:33 2009
+++ Auto.pm Mon Mar 09 14:44:07 2009
@@ -2,15 +2,11 @@
use strict;
use warnings;
-use File::Spec::Functions;
-use File::Basename;
-#use XML::Simple; # this is now optional
-use Config::IniFiles;
use Carp;
use vars qw[$VERSION $DisablePerl $Untaint $Format];
-$VERSION = '0.20';
+$VERSION = '0.21';
$DisablePerl = 0;
$Untaint = 0;
@@ -27,10 +23,6 @@
yaml => \&yaml,
);
-### make sure we give good diagnostics when XML::Simple is not available,
-### but required to parse a config
-$methods{'xml'} = sub { croak "XML::Simple not available. Can not parse '@_'" }
- unless eval { require XML::Simple; XML::Simple->import; 1 };
sub parse {
my $file = shift;
@@ -46,7 +38,7 @@
### <21d48be50604271656n153e6db6m9b059f57548aaa32@mail.gmail.com>
# If a config file "$file" contains multibyte charactors like japanese,
# -B returns "true" in old version of perl such as 5.005_003. It seems
- # there is no problem in perl 5.6x or older.
+ # there is no problem in perl 5.6x or newer.
### so check -B and only return only if
return if -B $file and $] >= '5.006';
@@ -154,6 +146,13 @@
sub find_file {
my($file,$path) = @_;
+ ### Moved here so only loaded when looking for file
+ require File::Spec::Functions;
+ import File::Spec::Functions;
+
+ require File::Basename;
+ import File::Basename;
+
my $x;
my $whoami = basename($0);
my $bindir = dirname($0);
@@ -207,11 +206,37 @@
return $cfg;
}
-sub parse_xml { return XMLin(shift); }
-sub parse_ini { tie my %ini, 'Config::IniFiles', (-file=>$_[0]);
- return \%ini; }
+sub parse_xml {
+ ### Check if XML::Simple is already loaded
+ unless ( exists $INC{'XML/Simple.pm'} ) {
+ ### make sure we give good diagnostics when XML::Simple is not available,
+ ### but required to parse a config
+ eval { require XML::Simple; XML::Simple->import; 1 };
+ croak "XML::Simple not available. Can not parse '@_'" if $@;
+ }
+
+ ### Changed to XML::Simple::XMLin to save importing functions if calling
+ ### modules has already loaded XML::Simple
+ return XML::Simple::XMLin(shift);
+}
+
+sub parse_ini {
+ ### Check is Config::IniFiles is already loaded
+ unless ( exists $INC{'Config/IniFiles.pm'} ) {
+ ### make sure we give good diagnostics when Config::IniFiles is not available,
+ ### but required to parse a config
+ eval { require Config::IniFiles; Config::IniFiles->import; 1 };
+ croak "Config::IniFiles not available. Can not parse '@_'" if $@;
+ }
+
+ tie my %ini, 'Config::IniFiles', (-file=>$_[0]);
+ return \%ini;
+}
+
sub return_list { open my $fh, shift or die $!; return [<$fh>]; }
-sub yaml { require YAML; return YAML::LoadFile( shift ) }
+
+### Changed to YAML::Any which selects the fastest YAML parser available (req YAML 0.67)
+sub yaml { require YAML::Any; return YAML::Any::LoadFile( shift ) }
sub bind_style { croak "BIND8-style config not supported in this release" }
sub irssi_style { croak "irssi-style config not supported in this release" }
@@ -328,7 +353,6 @@
1;
__END__
-# Below is stub documentation for your module. You better edit it!
=head1 NAME
Subject: | Config-Auto-0.21.tar.gz |
Message body not shown because it is not plain text.
Subject: | Makefile.PL.patch |
--- Makefile.PL.old Mon Mar 09 14:43:33 2009
+++ Makefile.PL Mon Mar 09 14:14:57 2009
@@ -16,7 +16,7 @@
my $prereqs = {
'XML::Simple' => 0,
- 'YAML' => 0,
+ 'YAML' => 0.67,
'Config::IniFiles' => 0,
'File::Spec::Functions' => 0,
'Test::More' => 0,
Subject: | xml-unavailable.t.patch |
--- xml-unavailable.t.old Mon Mar 09 14:40:24 2009
+++ xml-unavailable.t Mon Mar 09 14:47:05 2009
@@ -4,11 +4,15 @@
use File::Spec::Functions;
### whitebox test, disabling XML::Simple
-{ package XML::Simple;
- $INC{'XML/Simple.pm'} = $0;
-
- sub import { die };
-}
+use lib 't';
+
+### This has been moved to a sample XML::Simple file in the inc folder
+### Due to the new way XML::Simple is loaded
+#{ package XML::Simple;
+# $INC{'XML/Simple.pm'} = $0;
+#
+# sub import { die };
+#}
use_ok('Config::Auto');
Subject: | Changes.patch |
--- Changes.old Mon Mar 09 14:43:11 2009
+++ Changes Mon Mar 09 14:50:25 2009
@@ -1,3 +1,8 @@
+0.21 Mon Mar 9 14:41:51 2009
+================================
+ - Speed enhancements to only load modules if they are
+ actually needed. By Lyle Hopkins
+
0.20 Sat Apr 7 15:13:51 2007
================================
- Address #25732: Close filehandles after parsing
Subject: | MANIFEST.patch |
--- MANIFEST.old Mon Mar 09 14:50:52 2009
+++ MANIFEST Mon Mar 09 14:48:29 2009
@@ -21,5 +21,6 @@
t/winini.t
t/xml-unavailable.t
t/xml.t
+t/XML/Simple.pm
t/yaml.t
META.yml Module meta-data (added by MakeMaker)