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

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


The branch, master has been updated
       via  e59e9e3eb464a7160b001c11eb7d0d9ad6c2a4a6 (commit)
       via  52db9cdfa680dd1dfb6a5f134ec9c19c285622cb (commit)
       via  f1179a7bae4bb3d6c59cbd6e9ec8ab25f24b4039 (commit)
       via  2f452f3d360149b34fa5d0f3a0ae6bfd99220845 (commit)
      from  02f71d1bb3233c5c27702457fa4dff1b2e327969 (commit)

Summary of changes:
 Makefile.PL             |    2 ++
 lib/Class/Load.pm       |   21 ++++++++++++++++-----
 t/001-is-class-loaded.t |   23 ++++++++++++++++++++++-
 t/006-returned-error.t  |   47 +++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 87 insertions(+), 6 deletions(-)
 create mode 100644 t/006-returned-error.t

- Log -----------------------------------------------------------------
commit 2f452f3d360149b34fa5d0f3a0ae6bfd99220845
Author: Jesse Luehrs <doy at tozt.net>
Date:   Sun Nov 14 19:07:32 2010 -0600

    global variables are gross

diff --git a/lib/Class/Load.pm b/lib/Class/Load.pm
index 4d2dcf6..53a3efc 100644
--- a/lib/Class/Load.pm
+++ b/lib/Class/Load.pm
@@ -22,9 +22,11 @@ BEGIN {
 sub load_class {
     my $class = shift;
 
-    return 1 if try_load_class($class);
+    my ($res, $e) = try_load_class($class);
+    return 1 if $res;
+
     require Carp;
-    Carp::croak $ERROR;
+    Carp::croak $e;
 }
 
 sub load_optional_class {
@@ -56,6 +58,7 @@ sub _mod2pm {
 sub try_load_class {
     my $class = shift;
 
+    local $@;
     undef $ERROR;
 
     return 1 if is_class_loaded($class);
@@ -80,7 +83,8 @@ sub try_load_class {
     };
 
     $ERROR = $@;
-    return 0;
+    return 0 unless wantarray;
+    return 0, $@;
 }
 
 sub _is_valid_class_name {
@@ -194,7 +198,7 @@ which C<require> does not check.
 =head2 try_load_class Class::Name -> 0|1
 
 Returns 1 if the class was loaded, 0 if it was not. If the class was not
-loaded, the error will be available in C<$Class::Load::ERROR>.
+loaded, the error will be returned as a second return value.
 
 Again, if C<Class::Name> is already loaded (checked with C<is_class_loaded>)
 then it will not try to load the class. This is useful when you have inner
diff --git a/t/006-returned-error.t b/t/006-returned-error.t
new file mode 100644
index 0000000..44f65d4
--- /dev/null
+++ b/t/006-returned-error.t
@@ -0,0 +1,47 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use Test::More tests => 15;
+use Class::Load ':all';
+use lib 't/lib';
+
+{
+    ok(try_load_class('Class::Load::OK'), "loaded class OK");
+    my ($r, $e) = try_load_class('Class::Load::OK');
+    is($e, undef);
+}
+
+{
+    ok(!try_load_class('Class::Load::Nonexistent'), "didn't load class Nonexistent");
+    my ($r, $e) = try_load_class('Class::Load::Nonexistent');
+    like($e, qr{^Can't locate Class/Load/Nonexistent.pm in \@INC});
+}
+
+{
+    ok(try_load_class('Class::Load::OK'), "loaded class OK");
+    my ($r, $e) = try_load_class('Class::Load::OK');
+    is($e, undef);
+}
+
+{
+    ok(!try_load_class('Class::Load::SyntaxError'), "didn't load class SyntaxError");
+    delete $INC{'Class/Load/SyntaxError.pm'};
+    my ($r, $e) = try_load_class('Class::Load::SyntaxError');
+    like($e, qr{^Missing right curly or square bracket at });
+}
+
+ok(is_class_loaded('Class::Load::OK'));
+ok(!is_class_loaded('Class::Load::Nonexistent'));
+ok(!is_class_loaded('Class::Load::SyntaxError'));
+
+{
+    $@ = "foo";
+    ok(try_load_class('Class::Load::OK'), "loaded class OK");
+    is($@, "foo");
+}
+
+{
+    $@ = "foo";
+    ok(!try_load_class('Class::Load::Nonexistent'), "didn't load class Nonexistent");
+    is($@, "foo");
+}

commit f1179a7bae4bb3d6c59cbd6e9ec8ab25f24b4039
Author: Jesse Luehrs <doy at tozt.net>
Date:   Sun Nov 14 19:18:55 2010 -0600

    add a few more special cases here

diff --git a/lib/Class/Load.pm b/lib/Class/Load.pm
index 53a3efc..b30cffb 100644
--- a/lib/Class/Load.pm
+++ b/lib/Class/Load.pm
@@ -137,9 +137,14 @@ sub is_class_loaded {
 
         # constant subs
         if ( IS_RUNNING_ON_5_10 ) {
-            return 1 if ref $glob eq 'SCALAR';
+            my $ref = ref($glob);
+            return 1 if $ref eq 'SCALAR' || $ref eq 'REF';
         }
 
+        # stubs
+        my $refref = ref(\$glob);
+        return 1 if $refref eq 'SCALAR';
+
         return 1 if defined *{$glob}{CODE};
     }
 
diff --git a/t/001-is-class-loaded.t b/t/001-is-class-loaded.t
index 3aacdc5..f422126 100644
--- a/t/001-is-class-loaded.t
+++ b/t/001-is-class-loaded.t
@@ -1,7 +1,7 @@
 #!/usr/bin/env perl
 use strict;
 use warnings;
-use Test::More tests => 10;
+use Test::More tests => 13;
 
 use Class::Load 'is_class_loaded';
 
@@ -66,4 +66,25 @@ do {
 };
 ok(is_class_loaded('Class::Load::WithConstant'), "defining a constant means the class is loaded");
 # }}}
+# use constant with reference (yes) {{{
+do {
+    package Class::Load::WithRefConstant;
+    use constant PI => \3;
+};
+ok(is_class_loaded('Class::Load::WithRefConstant'), "defining a constant as a reference means the class is loaded");
+# }}}
+# stub (yes) {{{
+do {
+    package Class::Load::WithStub;
+    sub foo;
+};
+ok(is_class_loaded('Class::Load::WithStub'), "defining a stub means the class is loaded");
+# }}}
+# stub with prototype (yes) {{{
+do {
+    package Class::Load::WithPrototypedStub;
+    sub foo (&);
+};
+ok(is_class_loaded('Class::Load::WithPrototypedStub'), "defining a stub with a prototype means the class is loaded");
+# }}}
 

commit 52db9cdfa680dd1dfb6a5f134ec9c19c285622cb
Author: Jesse Luehrs <doy at tozt.net>
Date:   Sun Nov 14 19:41:19 2010 -0600

    explicitly use Scalar::Util
    
    reftype was already being called below, but was never imported (probably
    compiling down to an indirect method call or some such nonsense)

diff --git a/Makefile.PL b/Makefile.PL
index 5d5af25..5841f29 100755
--- a/Makefile.PL
+++ b/Makefile.PL
@@ -4,6 +4,8 @@ name       'Class-Load';
 all_from   'lib/Class/Load.pm';
 repository 'http://github.com/bestpractical/class-load';
 
+requires 'Scalar::Util';
+
 build_requires 'Test::More';
 build_requires 'Test::Fatal';
 
diff --git a/lib/Class/Load.pm b/lib/Class/Load.pm
index b30cffb..6832b2e 100644
--- a/lib/Class/Load.pm
+++ b/lib/Class/Load.pm
@@ -3,6 +3,7 @@ use strict;
 use warnings;
 use base 'Exporter';
 use File::Spec;
+use Scalar::Util 'reftype';
 
 our $VERSION = '0.06';
 

commit e59e9e3eb464a7160b001c11eb7d0d9ad6c2a4a6
Author: Shawn M Moore <sartak at bestpractical.com>
Date:   Mon Nov 15 13:16:12 2010 -0500

    Be more explicit about try_load_class's contextual return

diff --git a/lib/Class/Load.pm b/lib/Class/Load.pm
index 6832b2e..fa0cc39 100644
--- a/lib/Class/Load.pm
+++ b/lib/Class/Load.pm
@@ -202,9 +202,10 @@ will not try to load the class. This is useful when you have inner packages
 which C<require> does not check.
 
 =head2 try_load_class Class::Name -> 0|1
+=head2 try_load_class Class::Name -> (0|1, error message)
 
 Returns 1 if the class was loaded, 0 if it was not. If the class was not
-loaded, the error will be returned as a second return value.
+loaded, the error will be returned as a second return value in list context.
 
 Again, if C<Class::Name> is already loaded (checked with C<is_class_loaded>)
 then it will not try to load the class. This is useful when you have inner

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



More information about the Bps-public-commit mailing list