Subject: | "use lock" doesn't work... which is good. |
I noticed this bit of code in Class::Autouse...
# Add the AUTOLOAD hook and %INC lock to prevent 'use'ing
*{"${class}::AUTOLOAD"} = \&_AUTOLOAD;
$INC{$file} = 'Class::Autouse';
which worried me because the code base I'm applying Autouse to has
hybrid modules, they're both classes and export functions. Its a large,
crufty code base and not what I prefer, but I can't change that now. If
the feature above worked it would prevent this sort of thing...
Class::Autouse->autouse("Some::Thing");
use Some::Thing qw(function);
Fortunately it doesn't work. :) Here's a simple example.
#!/usr/bin/perl -wl
use strict;
use Class::Autouse;
Class::Autouse->autouse('Foo');
use Foo;
print $INC{"Foo.pm"};
print Foo::thing();
where Foo.pm is
package Foo;
sub thing { 42 }
1;
Autouse's attempt to prevent loading is defeated, I suspect, because
"use" calls import() which triggers the loading of the module. However,
require is defeated. This breaks certain techniques...
#!/usr/bin/perl -wl
use strict;
use Class::Autouse;
Class::Autouse->autouse('Foo');
require Foo;
Foo->import;
print $INC{"Foo.pm"};
print thing();
Where Foo.pm is
package Foo;
use base qw(Exporter);
use vars qw(@EXPORT);
@EXPORT = qw(thing);
sub thing { 42 }
1;
Comment out the autouse() and it works fine. Put it back and thing() is
undefined. I'm not sure why. If you go in with the debugger you'll
notice Foo->import is going directly into Exporter->import rather than
Autouse's loader even though the real Foo.pm remains unloaded. I don't
know why this is.
Anyhow, as the "use lock" is not doing its intended job and reduces the
transparency of Autouse in this case, I'd recommend removing it.