oops, backout bogus change#3545
[p5sagit/p5-mst-13.2.git] / lib / autouse.pm
index e2ef580..179c382 100644 (file)
@@ -3,9 +3,9 @@ package autouse;
 #use strict;           # debugging only
 use 5.003_90;          # ->can, for my $var
 
-$autouse::VERSION = '0.03';
+$autouse::VERSION = '1.02';
 
-my $DEBUG = $ENV{AUTOUSE_DEBUG};
+$autouse::DEBUG ||= 0;
 
 sub vet_import ($);
 
@@ -15,7 +15,8 @@ sub croak {
 }
 
 sub import {
-    shift;
+    my $class = @_ ? shift : 'autouse';
+    croak "usage: use $class MODULE [,SUBS...]" unless @_;
     my $module = shift;
 
     (my $pm = $module) =~ s{::}{/}g;
@@ -24,12 +25,12 @@ sub import {
        vet_import $module;
        local $Exporter::ExportLevel = $Exporter::ExportLevel + 1;
        # $Exporter::Verbose = 1;
-       return $module->import(map { (my $f = $_) =~ s/\(.*?\)$// } @_);
+       return $module->import(map { (my $f = $_) =~ s/\(.*?\)$//; $f } @_);
     }
 
     # It is not loaded: need to do real work.
     my $callpkg = caller(0);
-    print "autouse called from $callpkg\n" if $DEBUG;
+    print "autouse called from $callpkg\n" if $autouse::DEBUG;
 
     my $index;
     for my $f (@_) {
@@ -48,15 +49,15 @@ sub import {
        }
 
        my $load_sub = sub {
-           unless ($INC{pm}) {
-               require $pm;
-               die $@ if $@;
+           unless ($INC{$pm}) {
+               eval {require $pm};
+               die if $@;
                vet_import $module;
            }
            *$closure_import_func = \&{"${module}::$closure_func"};
            print "autousing $module; "
                  ."imported $closure_func as $closure_import_func\n"
-               if $DEBUG;
+               if $autouse::DEBUG;
            goto &$closure_import_func;
        };
 
@@ -72,7 +73,7 @@ sub vet_import ($) {
     my $module = shift;
     if (my $import = $module->can('import')) {
        croak "autoused module has unique import() method"
-           unless defined(\&Exporter::import)
+           unless defined(&Exporter::import)
                   && $import == \&Exporter::import;
     }
 }
@@ -145,15 +146,6 @@ The first line ensures that the errors in your argument specification
 are found early.  When you ship your application you should comment
 out the first line, since it makes the second one useless.
 
-=head1 BUGS
-
-If Module::func3() is autoused, and the module is loaded between the
-C<autouse> directive and a call to Module::func3(), warnings about
-redefinition would appear if warnings are enabled.
-
-If Module::func3() is autoused, warnings are disabled when loading the
-module via autoused functions.
-
 =head1 AUTHOR
 
 Ilya Zakharevich (ilya@math.ohio-state.edu)