Give warnings in to_app() auto promotion #369
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Here's one of the under-documented features in Plack: If a .psgi file returns an object that has
to_app
method, plackup will automatically call the method, to make it a PSGI code reference. It was merely a shortcut to make Plack::App and Component subclasses to work even when you forget->to_app
.However - When you combine that with Plack::Builder with
builder { ...; $app }
, or use the command lineplackup -e 'Plack::App::File->new'
with the default development environment, the application object is now wrapped by the builder and default middleware - which means there's no chance of calling->to_app
in the loaders.PSGI handlers will now have
$app
as an object to call as a code ref, and at the runtime, Plack::App::Component automatically promotes itself to the coderef viato_app
, with the sub overloading, for each request.This is bad in terms of performance and all that, so here're two attempts in this pull request to make it less worse:
a) Gives warning when such automatic conversion happens, when PLACK_ENV is set to development, so that developers will notice that something is wrong, and that they have to call
->to_app
to get the right PSGI application.b) Move the automatic promotion code to Builder. It doesn't feel like a strictly correct place to do that, but this would catch 99% of the use case where this whole things becomes an issue. Also it is necessary for a backward compatibility reason, to prevent the actual use of
plackup -e 'Plack::App::File->new'
from breaking with the upgrade.