/usr/share/perl5/Method/Autoload.pm is in libmethod-autoload-perl 0.02-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 | package Method::Autoload;
use strict;
use warnings;
use UNIVERSAL::require;
our $VERSION='0.02';
our $AUTOLOAD;
=head1 NAME
Method::Autoload - Autoloads methods from a list of packages into the current package
=head1 SYNOPSIS
package MyPackage;
use base qw{Method::Autoload}
=head1 DESCRIPTION
The Method::Autoload base class package is used to autoload methods from a list of packages where you may not know what methods are available until run time. A good use of this package is programming support for user contributed packages or user contributed plugins.
=head1 USAGE
use MyPackage;
my $object=MyPackage->new(%hash); #provides new and initialize methods
$object->pushPackages("My::Bar"); #appends to "packages" array
$object->unshiftPackages("My::Foo"); #prepends to "packages" array
use MyPackage;
my $object=MyPackage->new(packages=>["My::Foo", "My::Bar"]);
$object->foo; #from My::Foo
$object->bar; #from My::Bar
=head1 CONSTRUCTOR
=head2 new
my $object=MyPackage->new(%hash);
my $object=MyPackage->new(package=>["My::Package1", "My::Package2"]);
=cut
sub new {
my $this=shift;
my $class=ref($this) || $this;
my $self={};
bless $self, $class;
$self->initialize(@_);
return $self;
}
=head2 initialize
=cut
sub initialize {
my $self=shift;
%$self=@_;
}
=head1 METHODS PUBLIC
=head2 packages
Returns the current list of packages in the "packages" array.
my @package=$object->packages; #()
my $package=$object->packages; #[]
=cut
sub packages {
my $self=shift;
$self->{"packages"}=[] unless ref($self->{"packages"}) eq "ARRAY";
return wantarray ? @{$self->{"packages"}} : $self->{"packages"};
}
=head2 pushPackages
Pushes packages on to the "packages" array.
$object->pushPackages("My::Bar");
$object->pushPackages(@packages);
=cut
sub pushPackages {
my $self=shift;
push @{$self->packages}, @_ if @_;
return $self->packages;
}
=head2 unshiftPackages
Unshifts packages on to the "packages" array. Use this if you want to override a "default" package. Please use with care.
$object->unshiftPackages("My::Foo");
$object->unshiftPackages(@packages);
=cut
sub unshiftPackages {
my $self=shift;
unshift @{$self->packages}, @_ if @_;
return $self->packages;
}
=head2 autoloaded
Returns a hash of autoloaded methods and the classes that they came from.
my %hash=$object->autoloaded; #()
my $hash=$object->autoloaded; #{}
=cut
sub autoloaded {
my $self=shift;
$self->{"autoloaded"}={} unless ref($self->{"autoloaded"}) eq "HASH";
return wantarray ? @{$self->{"autoloaded"}} : $self->{"autoloaded"};
}
=head1 METHODS PRIVATE
=head2 DESTROY ("Global" method)
We define DESTROY in this package so that it does not call AUTOLOAD but you may overload this method in your package, if you need it.
=cut
sub DESTROY {return "0E0"};
=head2 AUTOLOAD ("Global" method)
AUTOLOAD is a "global" method. Please review the limitations on inheriting this method.
=cut
sub AUTOLOAD {
my $self=shift;
my $method=$AUTOLOAD;
$method=~s/.*://;
#warn sprintf("Autoloading Method: %s\n", $method);
foreach my $class ($self->packages) {
if ($class->can($method)) {
#warn(sprintf(qq{Package "%s" is loaded and method "%s" is supported\n}, $class, $method));
$self->autoload($class, $method);
last; #for performance and in case another package defines method.
} else {
#warn sprintf("Loading Package: %s\n", $class);
$class->use;
if ($@) {
#warn(sprintf(qq{Warning: Failed to use package "%s". Is it installed?\n}, $class));
} else {
if ($class->can($method)) {
$self->autoload($class, $method);
last; #for performance and in case another package defines method.
}
}
}
}
die(sprintf(qq{Error: Could not autoload method "%s" from packages %s.\n},
$method, join(", ", map {qq{"$_"}} $self->packages)))
unless $self->can($method);
return $self->$method(@_);
}
=head2 autoload
my $subref=$object->autoload($class, $method);
=cut
sub autoload {
my $syntax=q{Error: autoload syntax $obj->autoload($class, $method)};
my $self=shift or die($syntax);
my $class=shift or die($syntax);
my $method=shift or die($syntax);
my $sub=join("::", $class, $method);
#warn sprintf(qq{Importing method "%s" from class "%s"\n}, $method, $class);
$self->autoloaded->{$method}=$class;
no strict qw{refs};
return *$method=\&{$sub};
}
=head1 BUGS
DavisNetworks.com provides support services for all Perl applications including this package.
=head1 SUPPORT
=head1 AUTHOR
Michael R. Davis
CPAN ID: MRDVT
STOP, LLC
domain=>michaelrdavis,tld=>com,account=>perl
http://www.stopllc.com/
=head1 COPYRIGHT
This program is free software licensed under the...
The BSD License
The full text of the license can be found in the LICENSE file included with this module.
=head1 SEE ALSO
L<Class::Std> AUTOMETHOD method,
=cut
1;
|