Class-Autouse
view release on metacpan or search on metacpan
lib/Class/Autouse.pm view on Meta::CPAN
# Compile-time Initialisation and Optimisation
BEGIN {
# Become an exporter so we don't get complaints when we act as a pragma.
# I don't fully understand the reason for this, but it works and I can't
# recall how to replicate the problem, so leaving it in to avoid any
# possible reversion. Besides, so many things use Exporter it should
# be practically free to do this.
@ISA = qw{ Exporter };
# We always start with the superloader off
$SUPERLOAD = 0;
# When set, disables $obj->isa/can where $obj is blessed before its class is loaded
# Things will operate more quickly when set, but this breaks things if you're
# unserializing objects from Data::Dumper, etc., and relying on this module to
# load the related classes on demand.
$NOPREBLESS = 0;
# Disable stating for situations where modules are on remote disks
$NOSTAT = 0;
# AUTOLOAD hook counter
$HOOKS = 0;
# ERRATA
# Special classes are internal and should be left alone.
# Loaded modules are those already loaded by us.
# Bad classes are those that are incompatible with us.
%BAD = map { $_ => 1 } qw{
IO::File
};
%SPECIAL = map { $_ => 1 } qw{
CORE main UNIVERSAL
ARRAY HASH SCALAR REF GLOB
};
%LOADED = map { $_ => 1 } qw{
UNIVERSAL
Carp
Exporter
File::Spec
List::Util
Scalar::Util
Class::Autouse
};
# "Have we tried to autoload a class before?"
# Per-class loop protection and improved shortcutting.
# Defaults to specials+preloaded to prevent attempting them.
%TRIED_CLASS = ( %SPECIAL, %LOADED );
# "Have we tried to autoload a method before?"
# Per-method loop protection and improved shortcutting
%TRIED_METHOD = ();
# Storage for dynamic loaders (regular and sugar)
@LOADERS = ();
@SUGAR = ();
# We play with UNIVERSAL:: functions, so save backup copies
$ORIGINAL_CAN = \&UNIVERSAL::can;
$ORIGINAL_ISA = \&UNIVERSAL::isa;
}
#####################################################################
# Configuration and Setting up
# Developer mode flag.
# Cannot be turned off once turned on.
sub devel {
_debug(\@_, 1) if DEBUG;
# Enable if not already
return 1 if $DEVEL++;
# Load any unloaded modules.
# Most of the time there should be nothing here.
foreach my $class ( grep { $INC{$_} eq 'Class::Autouse' } keys %INC ) {
$class =~ s/\//::/;
$class =~ s/\.pm$//i;
Class::Autouse->load($class);
}
}
# Happy Fun Super Loader!
# The process here is to replace the &UNIVERSAL::AUTOLOAD sub
# ( which is just a dummy by default ) with a flexible class loader.
sub superloader {
_debug(\@_, 1) if DEBUG;
# Shortcut if needed
return 1 if $SUPERLOAD++;
# Enable the global hooks
_GLOBAL_HOOKS();
return 1;
}
sub sugar {
# Operate as a function or a method
shift if $_[0] eq 'Class::Autouse';
# Ignore calls with no arguments
return 1 unless @_;
_debug(\@_) if DEBUG;
foreach my $callback ( grep { $_ } @_ ) {
# Handle a callback or regex
unless ( ref $callback eq 'CODE' ) {
die(
__PACKAGE__
. ' takes a code reference for syntactic sugar handlers'
. ": unexpected value $callback has type "
. ref($callback)
( run in 2.988 seconds using v1.01-cache-2.11-cpan-2398b32b56e )