NAME

KelpX::Hooks - Override any method in your Kelp application

SYNOPSIS

# in your Kelp application
use KelpX::Hooks;

# and then...
hook "template" => sub {
	return "No templates for you!";
};

DESCRIPTION

This module allows you to override methods in your Kelp application class. The provided "hook" method can be compared to Moose's around, and it mimics its interface. The difference is in how and when the replacement of the actual method occurs.

The problem here is that Kelp's modules are modifying the symbol table for the module at the runtime, which makes common attempts to change their methods` behavior futile. You can't override them, you can't change them with method modifiers, you can only replace them with different methods.

This module fights the symbol table magic with more symbol table magic. It will replace any method with your anonymous subroutine after the application is built and all the modules have been loaded.

EXPORT

hook

hook "sub_name" => sub {
	my ($original_sub, $self, @arguments) = @_;

	# your code, preferably do this at some point:
	return $self->$original_sub(@arguments);
};

Allows you to provide your own subroutine in place of the one specified. The first argument is the subroutine that's being replaced. It won't be run at all unless you call it explicitly.

Please note that Kelp::Less is not supported.

CAVEATS

This module works by replacing the build method in symbol tables. Because of this, you cannot hook the build method itself or any method which is run before it. The module will verbosely refuse to hook new or build.

Hooks will first try to apply before the build is run. Any modules declared in configuration will have their upgraded functions available inside build. Hooks which fail to apply before build will try again after it finished, and raise an exception if they fail again.

SEE ALSO

Kelp, Moose::Manual::MethodModifiers

AUTHOR

Bartosz Jarzyna, <bbrtj.pro@gmail.com>

COPYRIGHT AND LICENSE

Copyright (C) 2020 - 2024 by Bartosz Jarzyna

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.