Subject: | [PATCH] Make load_class() return the same error as require() |
Make load_class() return the same error as require()
try_load_class() will now only return the actual error, without an
" at FOO line BAR." suffix, which would only point to Class::Load's
code anyway.
load_class() and load_optional_class() now chomp the error returned
by try_load_class() before croaking it.
Users can now replace require() in their code with load_class() and
they will get the same error message on failure. E.g.
Can't locate POE/Component/IRC/Foo.pm in @inc (@inc contains: lib
/home/hinrik/perl5/perlbrew/perls/perl-5.14.0/lib/site_perl/5.14.0/x86_64-linux
/home/hinrik/perl5/perlbrew/perls/perl-5.14.0/lib/site_perl/5.14.0
/home/hinrik/perl5/perlbrew/perls/perl-5.14.0/lib/5.14.0/x86_64-linux
/home/hinrik/perl5/perlbrew/perls/perl-5.14.0/lib/5.14.0 .) at
lib/App/Pocoirc.pm line 250
versus:
Can't locate POE/Component/IRC/Foo.pm in @inc (@inc contains: lib
/home/hinrik/perl5/perlbrew/perls/perl-5.14.0/lib/site_perl/5.14.0/x86_64-linux
/home/hinrik/perl5/perlbrew/perls/perl-5.14.0/lib/site_perl/5.14.0
/home/hinrik/perl5/perlbrew/perls/perl-5.14.0/lib/5.14.0/x86_64-linux
/home/hinrik/perl5/perlbrew/perls/perl-5.14.0/lib/5.14.0 .) at
/home/hinrik/perl5/perlbrew/perls/perl-5.14.0/lib/site_perl/5.14.0/Class/Load.pm
line 83.
at lib/App/Pocoirc.pm line 250
Subject: | 0001-Make-load_class-return-the-same-error-as-require.patch |
From d16cb9a1d81e8bb044568ee31c087cfd230e2b12 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Hinrik=20=C3=96rn=20Sigur=C3=B0sson?= <hinrik.sig@gmail.com>
Date: Wed, 25 May 2011 08:16:54 +0000
Subject: [PATCH] Make load_class() return the same error as require()
try_load_class() will now only return the actual error, without an
" at FOO line BAR." suffix, which would only point to Class::Load's
code anyway.
load_class() and load_optional_class() now chomp the error returned
by try_load_class() before croaking it.
Users can now replace require() in their code with load_class() and
they will get the same error message on failure. E.g.
Can't locate POE/Component/IRC/Foo.pm in @INC (@INC contains: lib /home/hinrik/perl5/perlbrew/perls/perl-5.14.0/lib/site_perl/5.14.0/x86_64-linux /home/hinrik/perl5/perlbrew/perls/perl-5.14.0/lib/site_perl/5.14.0 /home/hinrik/perl5/perlbrew/perls/perl-5.14.0/lib/5.14.0/x86_64-linux /home/hinrik/perl5/perlbrew/perls/perl-5.14.0/lib/5.14.0 .) at lib/App/Pocoirc.pm line 250
versus:
Can't locate POE/Component/IRC/Foo.pm in @INC (@INC contains: lib /home/hinrik/perl5/perlbrew/perls/perl-5.14.0/lib/site_perl/5.14.0/x86_64-linux /home/hinrik/perl5/perlbrew/perls/perl-5.14.0/lib/site_perl/5.14.0 /home/hinrik/perl5/perlbrew/perls/perl-5.14.0/lib/5.14.0/x86_64-linux /home/hinrik/perl5/perlbrew/perls/perl-5.14.0/lib/5.14.0 .) at /home/hinrik/perl5/perlbrew/perls/perl-5.14.0/lib/site_perl/5.14.0/Class/Load.pm line 83.
at lib/App/Pocoirc.pm line 250
---
Changes | 6 ++++++
lib/Class/Load.pm | 10 ++++++++--
t/003-load-class.t | 15 ++++++++++++++-
3 files changed, 28 insertions(+), 3 deletions(-)
diff --git a/Changes b/Changes
index 7256b06..b96531b 100644
--- a/Changes
+++ b/Changes
@@ -1,5 +1,11 @@
Revision history for Class-Load
+0.07
+ Strip the "at /foo/Class/Load.pm line bar." from the error returned
+ by try_load_class(), and make load_class() and load_optional_class()
+ chomp it before croaking it back to the user. This means that
+ load_class() now returns the same error as require() would.
+
0.06 Mon Nov 15 13:35:37 EST 2010
BACK COMPAT CHANGE: Remove $Class::Load::ERROR in favor of a
contextual return value ( Jesse Luehrs )
diff --git a/lib/Class/Load.pm b/lib/Class/Load.pm
index 2d822d3..8de92f3 100644
--- a/lib/Class/Load.pm
+++ b/lib/Class/Load.pm
@@ -27,13 +27,15 @@ sub load_class {
return 1 if $res;
require Carp;
+ chomp $e;
Carp::croak $e;
}
sub load_optional_class {
my $class = shift;
# If success, then we report "Its there"
- return 1 if try_load_class($class);
+ my ($res, $e) = try_load_class($class);
+ return 1 if $res;
# My testing says that if its in INC, the file definately exists
# on disk. In all versions of Perl. The value isn't reliable,
@@ -42,7 +44,8 @@ sub load_optional_class {
return 0 unless exists $INC{$file};
require Carp;
- Carp::croak $ERROR;
+ chomp $e;
+ Carp::croak $e;
}
sub _mod2pm {
@@ -83,6 +86,9 @@ sub try_load_class {
1;
};
+ my $our_file = __FILE__;
+ $@ =~ s{ at \Q$our_file\E line \d+\.$}{};
+
$ERROR = $@;
return 0 unless wantarray;
return 0, $@;
diff --git a/t/003-load-class.t b/t/003-load-class.t
index 84e7680..814757e 100644
--- a/t/003-load-class.t
+++ b/t/003-load-class.t
@@ -1,7 +1,7 @@
#!/usr/bin/env perl
use strict;
use warnings;
-use Test::More tests => 14;
+use Test::More tests => 15;
use Class::Load ':all';
use Test::Fatal;
use lib 't/lib';
@@ -35,3 +35,16 @@ ok(load_class('Class::Load::Inlined'), "loaded class Inlined");
is($Class::Load::ERROR, undef);
ok(is_class_loaded('Class::Load::Inlined'));
+# line 999
+eval { load_class('this_class_does_not_exists') };
+my $load_class_error = $@;
+
+# line 999
+eval { require 'this_class_does_not_exists.pm' };
+my $require_error = $@;
+
+# This is needed because require() adds a full-stop at the end whereas
+# croak() does not.
+$require_error =~ s/\.$//;
+
+is($load_class_error, $require_error, 'load_class() fails like require()');
--
1.7.4.4