Web::Machine - A Perl port of Webmachine
version 0.11
use strict; use warnings; use Web::Machine; { package HelloWorld::Resource; use strict; use warnings; use parent 'Web::Machine::Resource'; sub content_types_provided { [{ 'text/html' => 'to_html' }] } sub to_html { q{<html> <head> <title>Hello World Resource</title> </head> <body> <h1>Hello World</h1> </body> </html>} } } Web::Machine->new( resource => 'HelloWorld::Resource' )->to_app;
Web::Machine provides a RESTful web framework modeled as a state machine. You define one or more resource classes. Each resource represents a single RESTful URI end point, such as a user, an email, etc. The resource class can also be the target for POST requests to create a new user, email, etc.
Web::Machine
POST
Each resource is a state machine, and each request for a resource is handled by running the request through that state machine.
Web::Machine is built on top of Plack, but it handles the full request and response cycle.
See Web::Machine::Manual for more details on using Web::Machine in general, and how Web::Machine and Plack interact.
This is a port of Webmachine, actually it is much closer to the Ruby version, with a little bit of the JavaScript version and even some of the Python version thrown in for good measure.
This module is extremely young and it is a port of an pretty young (June 2011) module in another language (ruby), which itself is a port of a still kind of young module (March 2009) in yet another language (Erlang). But that all said, it really seems like a sane idea and so I stole it and ported it to Perl.
NOTE: This module is a Plack::Component subclass and so follows the interface set forward by that module.
new( resource => $resource_classname, ?resource_args => $arg_list, ?tracing => 1|0, ?streaming => 1|0 )
The constructor expects to get a $resource_classname, which it will use to create an instance of the resource class. If that class requires any additional arguments, they can be specified with the resource_args parameter. It can also take an optional tracing parameter which it will pass onto the Web::Machine::FSM, and an optional streaming parameter, which if true will run the request in a PSGI streaming response, which can be useful if you need to run your content generation asynchronously.
$resource_classname
resource_args
tracing
streaming
inflate_request( $env )
This takes a raw PSGI $env and inflates it into a Plack::Request instance. By default this also uses HTTP::Headers::ActionPack to inflate the headers of the request to be complex objects.
$env
create_fsm
This will create the Web::Machine::FSM object to run. It will get passed the value of the tracing constructor parameter.
create_resource( $request )
This will create the Web::Machine::Resource instance using the class specified in the resource constructor parameter. It will pass in the $request object and call new_response on the $request object to get a Plack::Response instance.
resource
$request
new_response
finalize_response( $response )
Given a $response which is a Plack::Response object, this will finalize it and return a raw PSGI response.
$response
call( $env )
This is the call method overridden from the Plack::Component superclass.
call
If you set the WM_DEBUG environment variable to 1 we will print out information about the path taken through the state machine to STDERR.
WM_DEBUG
1
Stevan Little <stevan.little@iinteractive.com>
Andrew Nelson <anelson@cpan.org>
Dave Rolsky <autarch@urth.org>
Fayland Lam <fayland@gmail.com>
Gregory Oschwald <goschwald@maxmind.com>
Jesse Luehrs <doy@tozt.net>
John SJ Anderson <genehack@genehack.org>
Olaf Alders <olaf@wundersolutions.com>
This software is copyright (c) 2013 by Infinity Interactive, Inc..
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install Web::Machine, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Web::Machine
CPAN shell
perl -MCPAN -e shell install Web::Machine
For more information on module installation, please visit the detailed CPAN module installation guide.