make Class->as_psgi_app only call ->new once
[catagits/Web-Simple.git] / lib / Web / Simple / Application.pm
index 9511e34..d52dc78 100644 (file)
@@ -260,8 +260,8 @@ sub _run_fcgi {
 }
 
 sub as_psgi_app {
-  my $self = shift;
-  ref($self) ? sub { $self->_dispatch(@_) } : sub { $self->new->_dispatch(@_) }
+  my $self = ref($_[0]) ? $_[0] : $_[0]->new;
+  sub { $self->_dispatch(@_) };
 }
 
 sub run {
@@ -271,7 +271,11 @@ sub run {
   } elsif ($ENV{GATEWAY_INTERFACE}) {
     return $self->_run_cgi;
   }
-  my $path = shift(@ARGV) or die "No path passed - use $0 / for root";
+  unless (@ARGV && $ARGV[0] =~ m{^/}) {
+    return $self->_run_cli;
+  }
+
+  my $path = shift @ARGV;
 
   require HTTP::Request::Common;
   require Plack::Test;
@@ -283,4 +287,16 @@ sub run {
   print $response->as_string;
 }
 
+sub _run_cli {
+  my $self = shift;
+  die $self->_cli_usage;
+}
+
+sub _cli_usage {
+  "To run this script in CGI test mode, pass a URL path beginning with /:\n".
+  "\n".
+  "  $0 /some/path\n".
+  "  $0 /\n"
+}
+
 1;