docs::a...dLookup(3)
๐ NAME ๐
Okay bestie, welcome to the user-contributed Perl manual page <pink>docs::a...dLookup(3)</pink> โ and the main character here is ModPerl::MethodLookup: the lookup bestie for mod_perl modules, objects, and methods. It's giving โfind my stuffโ energy, but for Perl. ๐
โจ Synopsis โจ
Here's the whole vibe check in code, and yes โ copy it exactly, no cap:
use ModPerl::MethodLookup;
# return all module names containing XS method 'print'
my ($hint, @modules) =
ModPerl::MethodLookup::lookup_method('print');
# return only module names containing method 'print' which
# expects the first argument to be of type 'Apache2::Filter'
# (here $filter is an Apache2::Filter object)
my ($hint, @modules) =
ModPerl::MethodLookup::lookup_method('print', $filter);
# or
my ($hint, @modules) =
ModPerl::MethodLookup::lookup_method('print', 'Apache2::Filter');
# what XS methods defined by module 'Apache2::Filter'
my ($hint, @methods) =
ModPerl::MethodLookup::lookup_module('Apache2::Filter');
# what XS methods can be invoked on the object $r (or a ref)
my ($hint, @methods) =
ModPerl::MethodLookup::lookup_object($r);
# or
my ($hint, @methods) =
ModPerl::MethodLookup::lookup_object('Apache2::RequestRec');
# preload all mp2 modules in startup.pl
ModPerl::MethodLookup::preload_all_modules();
# command line shortcuts
% perl -MModPerl::MethodLookup -e print_module \
Apache2::RequestRec Apache2::Filter
% perl -MModPerl::MethodLookup -e print_object Apache2
% perl -MModPerl::MethodLookup -e print_method \
get_server_built request
% perl -MModPerl::MethodLookup -e print_method read
% perl -MModPerl::MethodLookup -e print_method read APR::Bucket
๐ธ Description ๐ธ
So, <hotpink>mod_perl 2.0</hotpink> has a lot of methods, and they live in various modules. The catch? You gotta load each module before you can use the methods you want. Kind of a whole โyou can't sit with us until you're loadedโ situation. ๐ญ
ModPerl::MethodLookup gives you the Perl API for finding module names that contain the methods in question, plus other helper functions so you can figure out what methods a module defines, what methods can be called on a given object, etc. It's literally the group chat that tells you who knows what. ๐ซถ
๐ง API ๐ง
๐ฆ lookup_method() ๐ฆ
Find modules (packages) containing a certain method. Obsessed with the precision. โจ
($hint, @modules) = lookup_method($method_name);
($hint, @modules) = lookup_method($method_name, $object);
($hint, @modules) = lookup_method($method_name, $class));
arg1: $method_name ( string )โ the method name to look up, periodt.opt arg2: $object or $classโ a blessed object or the name of the class it's blessed into. If there is more than one match, this extra information is used to return only modules containing methods operating on the objects of the same kind.- If a sub-classed object is passed, it'll be handled correctly by checking its super-class(es). This usage is useful when
AUTOLOADis used to find a not-yet-loaded module which includes the called method. ret1: $hintโ a string containing a human-readable lookup result, suggesting which modules should be loaded, ready for copy-n-paste, or explaining the failure if the lookup didn't succeed.ret2: @modulesโ an array of modules which have matched the query, i.e. the names of the modules which contain the requested method.since: 2.0.00โ that's the version, bestie. ๐
Examples โจ
Return all module names containing XS method print:
my ($hint, @modules) =
ModPerl::MethodLookup::lookup_method('print');
Return only module names containing method print which expects the first argument to be of type Apache2::Filter:
my $filter = bless {}, 'Apache2::Filter';
my ($hint, @modules) =
ModPerl::MethodLookup::lookup_method('print', $filter);
or:
my ($hint, @modules) =
ModPerl::MethodLookup::lookup_method('print', 'Apache2::Filter');
๐ lookup_module() ๐
Find methods contained in a certain module (package). It's giving โwhat's in your bag, bestie?โ energy. ๐
($hint, @methods) = lookup_module($module_name);
arg1: $module_name ( string )โ the module name.ret1: $hintโ a string containing a human-readable lookup result, suggesting which methods the module$module_nameimplements, or explaining the failure if the lookup failed.ret2: @methodsโ an array of methods which have matched the query, i.e. the names of the methods defined in the requested module.since: 2.0.00โ iconic since that version. ๐
Example โจ
What XS methods defined by module Apache2::Filter:
my ($hint, @methods) =
ModPerl::MethodLookup::lookup_module('Apache2::Filter');
๐ช lookup_object() ๐ช
($hint, @methods) = lookup_object($object);
($hint, @methods) = lookup_object($class);
arg1: $object or $classโ an object or a name of a class an object is blessed into.- If a sub-classed object is passed, it'll be handled correctly by including methods provided by its super-class(es).
ret1: $hintโ a string containing a human-readable lookup result, suggesting which methods the given object can invoke (including module names that need to be loaded to use those methods), or explaining the failure if the lookup failed.ret2: @methodsโ an array of methods which have matched the query, i.e. the names of the methods that can be invoked on the given object (or its class name).since: 2.0.00โ yes bestie, that version. ๐
META: As of this writing, this function may miss some of the functions/methods that can be invoked on the given object. Currently we can't programmatically deduct the objects they are invoked on, because these methods are written in pure XS and manipulate the arguments stack themselves. Currently these are mainly XS functions, not methods, which of course aren't invoked on objects. There are also logging function wrappers (Apache2::Log).
Examples โจ
What XS methods can be invoked on the object $r:
my ($hint, @methods) =
ModPerl::MethodLookup::lookup_object($r);
or $r's class โ Apache2::RequestRec:
my ($hint, @methods) =
ModPerl::MethodLookup::lookup_object('Apache2::RequestRec');
๐ preload_all_modules() ๐
The function preload_all_modules() preloads all mod_perl 2.0 modules which implement their API in XS. This is similar to the mod_perl 1.0 behavior which has most of its methods loaded at the startup. It's basically the โload everything nowโ button. ๐ฎโ๐จ
CPAN modules developers should make sure their distribution loads each of the used mod_perl 2.0 modules explicitly, and not use this function, as it takes the fine control away from the users. One should avoid doing this on the production server (unless all modules are used indeed) in order to save memory.
since: 2.0.00โ the version, bestie. ๐
๐จ๏ธ print_method() ๐จ๏ธ
print_method() is a convenience wrapper for lookup_method(), mainly designed to be used from the command line. For example, to print all the modules which define method read, execute:
% perl -MModPerl::MethodLookup -e print_method read
Since this will return more than one module, we can narrow the query to only those methods which expect the first argument to be blessed into class APR::Bucket:
% perl -MModPerl::MethodLookup -e print_method read APR::Bucket
You can pass more than one method and it'll perform a lookup on each of the methods. For example, to lookup methods get_server_built and request you can do:
% perl -MModPerl::MethodLookup -e print_method \
get_server_built request
The function print_method() is exported by default. Slay. ๐
since: 2.0.00โ bestie, the version. ๐
๐งพ print_module() ๐งพ
print_module() is a convenience wrapper for lookup_module(), mainly designed to be used from the command line. For example, to print all the methods defined in the module Apache2::RequestRec, followed by methods defined in the module Apache2::Filter, you can run:
% perl -MModPerl::MethodLookup -e print_module \
Apache2::RequestRec Apache2::Filter
The function print_module() is exported by default. No cap. ๐ซถ
since: 2.0.00โ that's the version. ๐ธ
๐ print_object() ๐
print_object() is a convenience wrapper for lookup_object(), mainly designed to be used from the command line. For example, to print all the methods that can be invoked on an object blessed into a class Apache2::RequestRec, run:
% perl -MModPerl::MethodLookup -e print_object \
Apache2::RequestRec
Similar to print_object(), more than one class can be passed to this function. The way that works is lowkey handy. โจ
The function print_object() is exported by default. ๐
since: 2.0.00โ the version, bestie. ๐
๐ APPLICATIONS ๐
okay bestie, this is the practical part of the page. Two big application-y things live here: AUTOLOAD magic and command-line lookups. Both are giving โI know a guy who knows a moduleโ energy. ๐
โจ AUTOLOAD โจ
When Perl fails to locate a method it checks whether the package the object belongs to has an AUTOLOAD function defined and if so, calls it with the same arguments as the missing method while setting a global variable $AUTOLOAD (in that package) to the name of the originally called method.
We can use this facility to lookup the modules to be loaded when such a failure occurs. Though since we have many packages to take care of we will use a special UNIVERSAL::AUTOLOAD function which Perl calls if it canโt find the AUTOLOAD function in the given package.
In that function you can query ModPerl::MethodLookup, require() the module that includes the called method and call that method again using the goto() trick:
use ModPerl::MethodLookup;
sub UNIVERSAL::AUTOLOAD {
my ($hint, @modules) =
ModPerl::MethodLookup::lookup_method($UNIVERSAL::AUTOLOAD, @_);
if (@modules) {
eval "require $_" for @modules;
goto &$UNIVERSAL::AUTOLOAD;
}
else {
die $hint;
}
}
But hold up: the docs literally say we donโt endorse this approach. ๐ญ The better move is to always abort execution, print the $hint, and fix the code to load the missing module. Also, installing UNIVERSAL::AUTOLOAD may cause a lot of problems, since once itโs installed Perl will call it every time some method is missing (e.g. undefined DESTROY methods).
The following approach seems to somewhat work for me. It installs UNIVERSAL::AUTOLOAD only when the child process starts.
httpd.conf:
-----------
PerlChildInitHandler ModPerl::MethodLookupAuto
startup.pl:
-----------
{
package ModPerl::MethodLookupAuto;
use ModPerl::MethodLookup;
use Carp;
sub handler {
*UNIVERSAL::AUTOLOAD = sub {
my $method = $AUTOLOAD;
return if $method =~ /DESTROY/; # exclude DESTROY resolving
my ($hint, @modules) =
ModPerl::MethodLookup::lookup_method($method, @_);
$hint ||= "Can't find method $AUTOLOAD";
croak $hint;
};
return 0;
}
}
This example doesnโt load the modules for you. Itโll print to STDERR what module should be loaded, when a method from the not-yet-loaded module is called.
A similar technique is used by Apache2::porting.
META: there is a better version of AUTOLOAD discussed on the dev list. Replace the current one with it. (search the archive for EazyLife)
๐ป Command Line Lookups ๐ป
When a method is used and mod_perl has reported a failure to find it, itโs often useful to use the command line query to figure out which module needs to be loaded. For example if when executing:
$r->construct_url();
mod_perl complains:
Can't locate object method "construct_url" via package
"Apache2::RequestRec" at ...
you can ask ModPerl::MethodLookup for help:
% perl -MModPerl::MethodLookup -e print_method construct_url
To use method 'construct_url' add:
use Apache2::URI ();
and after copy-n-pasting the use statement in our code, the problem goes away.
One can create a handy alias for this technique. For example, C-style shell users can do:
% alias lookup "perl -MModPerl::MethodLookup -e print_method"
For Bash-style shell users:
% alias lookup="perl -MModPerl::MethodLookup -e print_method"
Now the lookup is even easier:
% lookup construct_url
to use method 'construct_url' add:
use Apache2::URI;
Similar aliases can be provided for print_object() and print_module().
๐ Todo ๐
These methods arenโt yet picked by this module (the extract from the map file):
modperl_filter_attributes | MODIFY_CODE_ATTRIBUTES
modperl_spawn_proc_prog | spawn_proc_prog
apr_ipsubnet_create | new
Please report to the mod_perl development mailing list if you find any other missing methods. But remember that as of this moment the module reports only XS functions. In the future we may add support for pure perl functions/methods as well.
๐ SEE ALSO ๐
- the mod_perl 1.0 backward compatibility document
- porting Perl modules
- porting XS modules
Apache2::porting
๐ COPYRIGHT ๐
mod_perl 2.0 and its core modules are copyrighted under The Apache Software License, Version 2.0.
๐ AUTHORS ๐
The mod_perl development team and numerous contributors.
okay thatโs the whole page bestie ๐ this lil doc-drop is from perl v5.42.3, dated 2023-10-21, and itโs the docs::a...dLookup(3) page. mwah ๐๐ซถ
๐ฆ where this page comes from
- nixos 26.05: mod_perl 2.0.13
- nixpkgs unstable: mod_perl 2.0.13
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms