The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Kelp::Util - Kelp general utility functions

SYNOPSIS

    use Kelp::Util;

    # MyApp::A::b
    say Kelp::Util::camelize('a#b', 'MyApp');

    # Controller
    say Kelp::Util::extract_class('Controller::Action');

    # Action
    say Kelp::Util::extract_function('Controller::Action');

DESCRIPTION

These are some helpful functions not seen in Plack::Util.

FUNCTIONS

No functions are exported and have to be used with full package name prefix.

camelize

This function accepts a string and a base class. Does three things:

  • transforms snake_case into CamelCase for class names (with lowercasing)

  • replaces hashes # with Perl package separators ::

  • constructs the class name in similar fasion as "load_class" in Plack::Util

The returned string will have leading + removed and will be prepended with the second argument if there was no +. An optional third argument can also be passed to treat the entire string as a class name.

Will not do the camelizing if there is no # sign in the string, even if the third argument is present.

extract_class

Extracts the class name from a Controller::action string. Returns undef if no class in the string or the class is main.

extract_function

Extracts the function name from a string. If there is no class name, returns the entire string. Returns undef for empty strings.

effective_charset

Takes a charset name and returns it back if it is supported by Encode. If there is no charset or it isn't supported, undef will be returned.

adapt_psgi

Transforms a given Plack/PSGI application (in form of a runner subroutine) to a Kelp route handler. The route handler will take the last argument matched from a pattern and adjust the proper environmental paths of the PSGI standard. This will make the application mostly behave as if it was mounted directly where the route points minus the last placeholder. For example, route /app will adjust the script name to '/app' and path info will always be empty, while route /app/>rest will have the same script name and path info set to whatever was after /app in the URL (trailing slashes included).

NOTE: having more than one placeholder in the pattern is mostly wasteful, as their matched values will not be handled in any way (other than allowing a varying request path).

load_package

Takes a name of a package and loads it efficiently.

merge

    my $merged = Kelp::Util::merge($val1, $val2, $allow_blessed);

Merges two structures. Used by config module to merge configuration files. Optionally, a third argument can be passed to allow merging values of blessed references as well.