The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Catalyst::Action::FromPSGI - Use a Plack app as a Catalyst action

VERSION

version 0.001001

SYNOPSIS

First, you have a plack app you wrote and want to use:

 package MyApp::WS::App;

 use Web::Simple;

 has name => (
    is => 'ro',
    required => 1,
 );

 sub dispatch_request {
    sub (/hi) {
       [ 200,
          [ 'Content-type' => 'text/plain' ],
          [ 'Hello ' . $_[0]->name ]
       ]
    },
 }

 1;

Now you want to reuse this app in a Catalyst action:

 package MyApp::Controller::HelloName;

 use base 'Catalyst::Controller';

 sub say_hi :Path('/say_hi_to') ActionClass('FromPlack') {
   my ($self, $c, $name, @args) = @_;

   MyApp::WS::App->new(name => $name)->to_psgi_app
 }

 1;

The above would yield 'Hello fREW' for the request to /say_hi_to/fREW/hi.

Of course the above example is contrived, but keep in mind this will work for any of the myriad Plack apps out there.

DESCRIPTION

Catalyst::Action::FromPlack gives you a handy way to mount Plack apps under Catalyst actions.

Note that because Catalyst is in control of the dispatch cycle any limitations you place on it will be placed on the Plack app as well. So for example:

 sub foo : Path('/foo') Args(1) ActionClass('FromPlack') { ... }

will never run the Plack app if the url is /foo/bar/baz because the Catalyst dispatcher won't even match for more than one argument. For this reason I recommend leaving Args unspecified for FromPlack actions.

I actually made this because I'm interested in using Web::Machine instead of Catalyst::Action::REST and possibly even replacing my chaining code with Web::Simple based dispatching.

THANKS

Matt S. Trout - for pioneering the actual guts of this code. Stevan Little - for porting Web::Machine, my motivation for making this.

AUTHOR

Arthur Axel "fREW" Schmidt <frioux+cpan@gmail.com>

COPYRIGHT AND LICENSE

This software is copyright (c) 2012 by Arthur Axel "fREW" Schmidt.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.