Browse files

Added Plack::Util::convert_to_app($app) using more generic coderef ov…

…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...
1 parent 4a7e5a4 commit 2efd23859638ef7a1efe549a553a1984ff50ff3a @miyagawa miyagawa committed Feb 8, 2013
Showing with 16 additions and 14 deletions.
  1. +1 −1 lib/Plack/Builder.pm
  2. +1 −12 lib/Plack/Component.pm
  3. +1 −1 lib/Plack/Runner.pm
  4. +13 −0 lib/Plack/Util.pm
View
2 lib/Plack/Builder.pm
@@ -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);
}
View
13 lib/Plack/Component.pm
@@ -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;
@@ -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
View
2 lib/Plack/Runner.pm
@@ -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 {
View
13 lib/Plack/Util.pm
@@ -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) = @_;

0 comments on commit 2efd238

Please sign in to comment.