NAME
DDG::Meta::RequestHandler - Functions for a request handler
VERSION
version 1018
DESCRIPTION
This meta class can install the required handle_request_matches function required by the DDG::IsGoodie and the DDG::IsSpice role.
It installs the keyword "handle", which installs this function on its call.
EXPORTS FUNCTIONS
handle
This function takes the keyword for the to handle attribute of the request as first parameter and a coderef of the actual handler.
An example can look like:
handle remainder => sub { lc($_) };
You must define triggers before you are allowed to define a handler, cause specific features are only available for specific DDG::Block implementations.
The choosen keyword defines which data can be found in $_ or @_ depending on the kind of data. If the requested attribute is an array then you get query_raw on $_ and the array of the attribute on @_.
If you dont give any keyword and just give a coderef, then query_raw is taken as keyword for the attribute.
DDG::Block::Regexp based plugins can use matches to get the matches of the regexp as parameter on @_.
DDG::Block::Words can use remainder and remainder_lc which gives back the parts of the query which are not hit by the trigger of the plugin. It is the most used handler.
The following keywords can be used by all plugins, cause they are based on the DDG::Request itself:
- query_raw - query_nowhitespace - query_nowhitespace_nodash - query - query_lc - query_clean - wordcount - words (array) - query_parts (array) - query_raw_parts (array)
To access the DDG::Request itself, you can directly access the variable $req which is set to the current DDG::Request for the call to the coderef of the handler.
You also get $loc and $lang which are always DDG::Location and DDG::Language objects, even if the DDG::Request had none. Given this case, then all functions give back empty values of the objects. This way you can directly work with those variable without getting error messages for accessing functions which are not there. To find out if there is a location or language at all you can use $has_loc and $has_lang.
METHODS
apply_keywords
This function installs the "handle" keyword. It requires for this the target package name, a result handler for handling the results of the plugins and optional a role which is applied after installing the keyword. This function is used in DDG::Meta as part of all the meta for DDG::Goodie and DDG::Spice.
request_symbols
This function uses a given Package::Stash and DDG::Request to implement the $loc, $has_loc, $lang and $has_lang variables on the package of the Package::Stash. It will automatically called by the installed handler, you never need to call it.
reset_request_symbols
This function uses a given Package::Stash and unsets $loc, $has_loc, $lang and $has_lang again. It will automatically called by the installed handler, you never need to call it.
AUTHOR
DuckDuckGo <open@duckduckgo.com>
COPYRIGHT AND LICENSE
This software is Copyright (c) 2013 by DuckDuckGo, Inc. https://duckduckgo.com/.
This is free software, licensed under:
The Apache License, Version 2.0, January 2004