Web::Machine(3pm)
π Web::Machine β a Perl port of Webmachine π
okay bestie, so this is Web::Machine, and it's a User Contributed Perl doc, sitting pretty in section 3pm of your manual pages. version? 0.17. and honestly? she ate. let's get into it π«Ά
β¨ SYNOPSIS β¨
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;
like. that's the whole hello world. she's giving minimal, she's giving RESTful, she's giving iconic π
πΈ DESCRIPTION πΈ
Web::Machine is a RESTful web framework, and the whole vibe is that it's modeled as a state machine. you define one or more resource classes, and each resource = a single RESTful URI end point. a user, an email, whatever. and that same resource class can also be the target for POST requests when you wanna create a new user, email, etc. multitasking queen π
so each resource is a state machine, and every request for a resource gets handled by running that request through the state machine. that's the whole thing. that's the plot π
Web::Machine is built on top of Plack, but β and this is the important part β it handles the full request and response cycle itself. no passing the aux cord around.
if you want more details on using Web::Machine in general, and on how Web::Machine and Plack interact, go read Web::Machine::Manual. it's the extended lore π¦
this is a port of Webmachine <https://github.com/basho/webmachine> β but ngl, it's actually much closer to the Ruby version <https://github.com/seancribbs/webmachine-ruby>, with a lil bit of the JavaScript version <https://github.com/tautologistics/nodemachine> and even some of the Python version <https://github.com/benoitc/pywebmachine> thrown in for good measure. a little bit of everyone. a collaboration π€
you can learn a bit about Web::Machine's history from the slides for the 2012 YAPC::NA talk <https://speakerdeck.com/stevan_little/rest-from-the-trenches>. history lesson, but make it fun.
and if you wanna learn more about Webmachine itself, the SEE ALSO section at the bottom has the links. keep scrolling bestie β¬οΈ
π METHODS π
NOTE: this module is a Plack::Component subclass, so it follows the interface set forward by that module. respect the family lineage π
new( resource => $resource_classname, ?resource_args => $arg_list, ?tracing => 1|0, ?streaming => 1|0, ?request_class => $request_class )β the constructor. she expects a$resource_classname, which she'll use to load and create an instance of the resource class. if that class needs any extra args, you can pass them with theresource_argsparameter, and the contents ofresource_argsget made available to theinit()method ofWeb::Machine::Resource. β¨
the new method can also take an optional tracing parameter, which gets passed on to Web::Machine::FSM, and an optional streaming parameter, which β if true β will run the request in a PSGI <http://plackperl.org/> streaming response. super useful if you need to run your content generation asynchronously. no blocking, no waiting, no drama π
the optional request_class parameter accepts the name of a module that'll be used as the request object. that module must be a class that inherits from Plack::Request. use this if you have a subclass of Plack::Request you'd like to use in your Web::Machine::Resource. custom request, custom you π
inflate_request( $env )β takes a raw PSGI$envand inflates it into a Plack::Request instance. by default this also uses HTTP::Headers::ActionPack to inflate the headers of the request into complex objects. glow up for your headers πcreate_fsmβ creates the Web::Machine::FSM object to run. it gets passed the value of thetracingconstructor parameter. π₯create_resource( $request )β creates the Web::Machine::Resource instance using the class specified in theresourceconstructor parameter. it passes in the$requestobject and callsnew_responseon the$requestobject to get a Plack::Response instance. π«Άfinalize_response( $response )β give her a$responsethat's a Plack::Response object and she'll finalize it and return a raw PSGI response. done and dusted βcall( $env )β this is thecallmethod, overridden from thePlack::Componentsuperclass. main character behavior honestly π
π DEBUGGING π
if you set the WM_DEBUG environment variable to 1, we'll print out information about the path taken through the state machine to STDERR. like a little breadcrumb trail of what your request did. it's giving detective π΅οΈ
if you set WM_DEBUG to diag, then the debugging information gets printed using Test::More's diag() sub instead. fancy β¨
π SEE ALSO π
the links, as promised, bestie:
- The diagram β <https://github.com/Webmachine/webmachine/wiki/Diagram>
- Original Erlang β <https://github.com/basho/webmachine>
- Ruby port β <https://github.com/seancribbs/webmachine-ruby>
- Node JS port β <https://github.com/tautologistics/nodemachine>
- Python port β <https://github.com/benoitc/pywebmachine>
- 2012 YAPC::NA slides β <https://speakerdeck.com/stevan_little/rest-from-the-trenches>
- an elaborate machine is indispensable: a blog post by Justin Sheehy β <http://blog.therestfulway.com/2008/09/webmachine-is-resource-server-for-web.html>
- Resources, For Real This Time (with Webmachine): a video by Sean Cribbs β <http://www.youtube.com/watch?v=odRrLK87s_Y>
π SUPPORT π
found a bug? first of all, relatable. second of all, bugs may be submitted through <https://github.com/houseabsolute/webmachine-perl/issues>. go off, report it π
π AUTHORS π
- Stevan Little <stevan@cpan.org>
- Dave Rolsky <autarch@urth.org>
π§ CONTRIBUTORS π§
the whole group chat, in alphabetical-ish order:
- Andreas Marienborg <andreas.marienborg@gmail.com>
- Andrew Nelson <anelson@cpan.org>
- Arthur Axel 'fREW' Schmidt <frioux@gmail.com>
- Carlos Fernando Avila Gratz <cafe@q1software.com>
- Fayland Lam <fayland@gmail.com>
- George Hartzell <hartzell@alerce.com>
- Gregory Oschwald <goschwald@maxmind.com>
- Jesse Luehrs <doy@tozt.net>
- John SJ Anderson <genehack@genehack.org>
- Mike Raynham <enquiries@mikeraynham.co.uk>
- Nathan Cutler <ncutler@suse.cz>
- Olaf Alders <olaf@wundersolutions.com>
- Stevan Little <stevan.little@gmail.com>
- Thomas Sibley <tsibley@cpan.org>
π COPYRIGHT AND LICENCE π
this software is copyright (c) 2016 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. free as in freedom, free as in slay π¦
and that's the page, bestie!! this doc came to you courtesy of perl v5.34.0, dated 2022-07-03, from Web::Machine(3pm) β mwah, go build something RESTful πβ¨π
π¦ where this page comes from
- debian bookworm: libweb-machine-perl 0.17-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libweb-machine-perl 0.17-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms