Skip to content

Commit

Permalink
Added Plack::Util::convert_to_app($app) using more generic coderef ov…
Browse files Browse the repository at this point in the history
…erloading

Now it warns the auto-conversion in development environment for all
objects that does &{} overloading, not just Plack::Component derived
classes. The check is done now in two places: Plack::Runner's build
method, in addition to Plack::Builder's builder { } block.
  • Loading branch information
miyagawa committed Feb 8, 2013
1 parent 4a7e5a4 commit 2efd238
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 14 deletions.
2 changes: 1 addition & 1 deletion lib/Plack/Builder.pm
Expand Up @@ -124,7 +124,7 @@ sub builder(&) {
}
}

$app = $app->to_app if $app and Scalar::Util::blessed($app) and $app->can('to_app');
$app = Plack::Util::convert_to_app($app);

$self->to_app($app);
}
Expand Down
13 changes: 1 addition & 12 deletions lib/Plack/Component.pm
Expand Up @@ -3,7 +3,7 @@ use strict;
use warnings;
use Carp ();
use Plack::Util;
use overload '&{}' => \&to_app_auto, fallback => 1;
use overload '&{}' => \&to_app, fallback => 1;

sub new {
my $proto = shift;
Expand All @@ -19,17 +19,6 @@ sub new {
$self;
}

sub to_app_auto {
my $self = shift;
if (($ENV{PLACK_ENV} || '') eq 'development') {
my $class = ref($self);
warn "WARNING: Automatically converting $class instance to a PSGI code reference. " .
"If you see this warning for each request, you probably need to explicitly call " .
"to_app() i.e. $class->new(...)->to_app in your PSGI file.\n";
}
$self->to_app(@_);
}

# NOTE:
# this is for back-compat only,
# future modules should use
Expand Down
2 changes: 1 addition & 1 deletion lib/Plack/Runner.pm
Expand Up @@ -21,7 +21,7 @@ sub new {
sub build(&;$) {
my $block = shift;
my $app = shift || sub { };
return sub { $block->($app->()) };
return sub { Plack::Util::convert_to_app($block->($app->())) };
}

sub parse_options {
Expand Down
13 changes: 13 additions & 0 deletions lib/Plack/Util.pm
Expand Up @@ -136,6 +136,19 @@ sub load_psgi {
return $app;
}

sub convert_to_app {
my $app = shift;

if (Scalar::Util::blessed($app) && overload::Method($app, '&{}')) {
if (($ENV{PLACK_ENV} || '') eq 'development') {
warn "WARNING: Converting $app to PSGI code reference using &{} overloading. You probably forgot to call ->to_app etc. in your .psgi file!\n";
}
$app = \&$app;
}

$app;
}

sub run_app($$) {
my($app, $env) = @_;

Expand Down

0 comments on commit 2efd238

Please sign in to comment.