[Bps-public-commit] Class-Load branch, master, updated. 02f71d1bb3233c5c27702457fa4dff1b2e327969

Shawn Moore sartak at bestpractical.com
Mon Nov 15 13:04:04 EST 2010


The branch, master has been updated
       via  02f71d1bb3233c5c27702457fa4dff1b2e327969 (commit)
       via  7d659d5f2185cf9e1d325d631d7c28aebc374a52 (commit)
       via  78aa6328efc3d3dbfce70c4d076b246c74829572 (commit)
       via  6b8e3cef98bc7541f7f376c43d3e3814744b38d0 (commit)
      from  e4d7d52f60c458ef100fd766f122a5662453d8a3 (commit)

Summary of changes:
 Changes               |    7 ++++++
 lib/Class/Load.pm     |   53 ++++++++++++++++++++++++++++++++++++++++++++----
 t/005-load-optional.t |   35 ++++++++++++++++++++++++++++++++
 3 files changed, 90 insertions(+), 5 deletions(-)
 create mode 100644 t/005-load-optional.t

- Log -----------------------------------------------------------------
commit 6b8e3cef98bc7541f7f376c43d3e3814744b38d0
Author: Kent Fredric <kentfredric at gmail.com>
Date:   Sun Nov 14 09:02:51 2010 +1300

    +Added load optional code and docs

diff --git a/lib/Class/Load.pm b/lib/Class/Load.pm
index 62584f4..dec5d7c 100644
--- a/lib/Class/Load.pm
+++ b/lib/Class/Load.pm
@@ -28,20 +28,37 @@ sub load_class {
     Carp::croak $ERROR;
 }
 
-sub try_load_class {
+sub load_optional_class {
     my $class = shift;
+    # If success, then we report "Its there"
+    return 1 if try_load_class($class);
+    # My testing says that if its in INC, the file definately exists
+    # on disk. In all versions of Perl. The value isn't reliable,
+    # but it existing is.
+    return 0 unless exists $INC{ _mod2pm($class) };
+    require Carp;
+    Carp::croak $ERROR;
+}
 
-    undef $ERROR;
-
-    return 1 if is_class_loaded($class);
-
+sub _mod2pm {
+    my $mod = shift;
     # see rt.perl.org #19213
     my @parts = split '::', $class;
     my $file = $^O eq 'MSWin32'
              ? join '/', @parts
              : File::Spec->catfile(@parts);
     $file .= '.pm';
+    return $file;
+}
+
+sub try_load_class {
+    my $class = shift;
 
+    undef $ERROR;
+
+    return 1 if is_class_loaded($class);
+
+    my $file = _mod2pm($class);
     # This says "our diagnostics of the package
     # say perl's INC status about the file being loaded are
     # wrong", so we delete it from $INC, so when we call require(),
@@ -143,6 +160,10 @@ Class::Load - a working (require "Class::Name") and more
 
     is_class_loaded('Class::Name');
 
+    my $baseclass = load_optional_class('Class::Name::MightExist')
+        ? 'Class::Name::MightExist'
+        : 'Class::Name::Default';
+
 =head1 DESCRIPTION
 
 C<require EXPR> only accepts C<Class/Name.pm> style module names, not
@@ -183,6 +204,26 @@ This uses a number of heuristics to determine if the class C<Class::Name> is
 loaded. There heuristics were taken from L<Class::MOP>'s old pure-perl
 implementation.
 
+=head2 load_optional_class Class::Name -> 0|1
+
+C<load_optional_class> is lots like C<try_load_class>, but also lots like
+C<load_class>.
+
+If the class exists, and it works, then it will return 1.
+
+If the class doesn't exist, and it appears to not exist on disk either, it
+will return 0.
+
+If the class exists on disk, but loading from disk results in an error
+( ie: Syntax ), then it will C<croak> with that error.
+
+This is useful for using if you want a fallback module system, ie:
+
+    my $class = load_optional_class($foo) ? $foo : $default;
+
+That way, if $foo does exist, but can't be loaded due to error, you won't
+get the behaviour of it simply not existing.
+
 =head1 SEE ALSO
 
 =over 4

commit 78aa6328efc3d3dbfce70c4d076b246c74829572
Author: Kent Fredric <kentfredric at gmail.com>
Date:   Sun Nov 14 09:05:24 2010 +1300

    Update changelog

diff --git a/Changes b/Changes
index 4155950..c9f13da 100644
--- a/Changes
+++ b/Changes
@@ -1,6 +1,13 @@
 Revision history for Class-Load
 
 0.06
+        Add load_optional_class($class) that fails only if
+        there is a problem with the attempted-to-load $class.
+        ( Kent Fredric )
+
+        Force internal require to try-again if we are SURE the class
+        is not there. This produces reliable errors. ( Kent Fredric )
+
         Replace Test::Exception with Test::Fatal ( Kent Fredric )
 
 0.05    Wed Sep 2 06:18:13 EST 2009

commit 7d659d5f2185cf9e1d325d631d7c28aebc374a52
Author: Kent Fredric <kentfredric at gmail.com>
Date:   Sun Nov 14 09:20:21 2010 +1300

    Add to export

diff --git a/lib/Class/Load.pm b/lib/Class/Load.pm
index dec5d7c..ee681c4 100644
--- a/lib/Class/Load.pm
+++ b/lib/Class/Load.pm
@@ -6,7 +6,7 @@ use File::Spec;
 
 our $VERSION = '0.06';
 
-our @EXPORT_OK = qw/load_class try_load_class is_class_loaded/;
+our @EXPORT_OK = qw/load_class load_optional_class try_load_class is_class_loaded/;
 our %EXPORT_TAGS = (
     all => \@EXPORT_OK,
 );

commit 02f71d1bb3233c5c27702457fa4dff1b2e327969
Author: Kent Fredric <kentfredric at gmail.com>
Date:   Sun Nov 14 09:41:18 2010 +1300

    Fix errors, add tests

diff --git a/lib/Class/Load.pm b/lib/Class/Load.pm
index ee681c4..4d2dcf6 100644
--- a/lib/Class/Load.pm
+++ b/lib/Class/Load.pm
@@ -23,7 +23,6 @@ sub load_class {
     my $class = shift;
 
     return 1 if try_load_class($class);
-
     require Carp;
     Carp::croak $ERROR;
 }
@@ -32,16 +31,19 @@ sub load_optional_class {
     my $class = shift;
     # If success, then we report "Its there"
     return 1 if try_load_class($class);
+
     # My testing says that if its in INC, the file definately exists
     # on disk. In all versions of Perl. The value isn't reliable,
     # but it existing is.
-    return 0 unless exists $INC{ _mod2pm($class) };
+    my $file = _mod2pm( $class );
+    return 0 unless exists $INC{$file};
+
     require Carp;
     Carp::croak $ERROR;
 }
 
 sub _mod2pm {
-    my $mod = shift;
+    my $class = shift;
     # see rt.perl.org #19213
     my @parts = split '::', $class;
     my $file = $^O eq 'MSWin32'
diff --git a/t/005-load-optional.t b/t/005-load-optional.t
new file mode 100644
index 0000000..2b261c4
--- /dev/null
+++ b/t/005-load-optional.t
@@ -0,0 +1,35 @@
+
+use strict;
+use warnings;
+
+use Test::More tests => 5;
+use Test::Fatal;
+use Class::Load qw( :all );
+use lib 't/lib';
+
+is(
+  exception {
+    load_optional_class('Class::Load::OK');
+  },
+  undef,
+  'No failure loading a good class'
+);
+
+is(
+  exception {
+    load_optional_class('Class::Load::IDONOTEXIST');
+  },
+  undef,
+  'No failure loading a missing class'
+);
+
+isnt(
+  exception {
+      load_optional_class('Class::Load::SyntaxError');
+  },
+  undef,
+  'Loading a broken class breaks'
+);
+
+is( load_optional_class('Class::Load::OK'), 1 , 'Existing Class => 1');
+is( load_optional_class('Class::Load::IDONOTEXIST'), 0, 'Missing Class => 0');

-----------------------------------------------------------------------



More information about the Bps-public-commit mailing list