r12983@zaphod: kd | 2008-04-28 18:10:27 +1000
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Utils.pm
index 0598f53..d27fa9c 100644 (file)
@@ -8,6 +8,7 @@ use Path::Class;
 use URI;
 use Class::Inspector;
 use Carp qw/croak/;
+use Cwd;
 
 =head1 NAME
 
@@ -160,6 +161,7 @@ sub home {
 
             # find the @INC entry in which $file was found
             (my $path = $inc_entry) =~ s/$file$//;
+            $path ||= cwd() if !defined $path || !length $path;
             my $home = dir($path)->absolute->cleanup;
 
             # pop off /lib and /blib if they're there
@@ -171,8 +173,9 @@ sub home {
                 # clean up relative path:
                 # MyApp/script/.. -> MyApp
 
-                my ($lastdir) = $home->dir_list( -1, 1 );
-                if ( $lastdir eq '..' ) {
+                my $dir;
+                my @dir_list = $home->dir_list();
+                while (($dir = pop(@dir_list)) && $dir eq '..') {
                     $home = dir($home)->parent->parent;
                 }
 
@@ -233,10 +236,15 @@ sub request {
     return $request;
 }
 
-=head2 ensure_class_loaded($class_name)
+=head2 ensure_class_loaded($class_name, \%opts)
 
 Loads the class unless it already has been loaded.
 
+If $opts{ignore_loaded} is true always tries the require whether the package
+already exists or not. Only pass this if you're either (a) sure you know the
+file exists on disk or (b) have code to catch the file not found exception
+that will result if it doesn't.
+
 =cut
 
 sub ensure_class_loaded {
@@ -246,6 +254,12 @@ sub ensure_class_loaded {
     croak "Malformed class Name $class"
         if $class =~ m/(?:\b\:\b|\:{3,})/;
 
+    croak "Malformed class Name $class"
+        if $class =~ m/[^\w:]/;
+
+    croak "ensure_class_loaded should be given a classname, not a filename ($class)"
+        if $class =~ m/\.pm$/;
+
     return if !$opts->{ ignore_loaded }
         && Class::Inspector->loaded( $class ); # if a symbol entry exists we don't load again
 
@@ -253,7 +267,9 @@ sub ensure_class_loaded {
     my $error;
     {
         local $@;
-        eval "require $class";
+        my $file = $class . '.pm';
+        $file =~ s{::}{/}g;
+        eval { CORE::require($file) };
         $error = $@;
     }
 
@@ -315,10 +331,9 @@ sub env_value {
     return;
 }
 
-=head1 AUTHOR
+=head1 AUTHORS
 
-Sebastian Riedel, C<sri@cpan.org>
-Yuval Kogman, C<nothingmuch@woobling.org>
+Catalyst Contributors, see Catalyst.pm
 
 =head1 COPYRIGHT