From: Christian Walde Date: Fri, 24 Oct 2014 17:35:13 +0000 (+0200) Subject: pass run_if_script args to new X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FWeb-Simple.git;a=commitdiff_plain;h=9609b43c79e5ad9bb40ff9f6c85b3462fa4097cd pass run_if_script args to new --- diff --git a/lib/Web/Simple/Application.pm b/lib/Web/Simple/Application.pm index 7ef273b..68f8b2c 100644 --- a/lib/Web/Simple/Application.pm +++ b/lib/Web/Simple/Application.pm @@ -50,16 +50,16 @@ sub _build_final_dispatcher { } sub run_if_script { - my ( $self ) = @_; + my ( $self, @args ) = @_; # ->to_psgi_app is true for require() but also works for plackup return $self->to_psgi_app if caller(1); - $self = ref($self) ? $self : $self->_build_for_run_if_script; - $self->run(@_); + $self = ref($self) ? $self : $self->_build_for_run_if_script(@args); + $self->run; } sub _build_for_run_if_script { - my ( $self ) = @_; - try { $self->new } + my ( $self, @args ) = @_; + try { $self->new(@args) } catch { die "Failed to create new '$self' object during run_if_script" . " (should your .pm file end with just '1;'?) : $_\n"; diff --git a/t/run_if_script.t b/t/run_if_script.t index dc66514..0839046 100644 --- a/t/run_if_script.t +++ b/t/run_if_script.t @@ -38,14 +38,14 @@ my $rt = RunTest->new( test => 3 ); is_deeply( [ $rt->run_if_script ], # - [ $rt, $rt, "run" ], + [ $rt, "run" ], "calling run_if_script on an object runs it directly" ); -is_deeply( - [ $rt->run_if_script( 4 ) ], # - [ $rt, $rt, 4, "run" ], - "passing arguments to run_if_script has them passed on to the run method" +is( + ( RunTest->run_if_script( test => 4 ) )[0]->test, # + 4, + "passing arguments to run_if_script has them passed on to the new method" ); like(