Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

RFC: Not Even A Framework

by Dallaylaen (Chaplain)
on Oct 02, 2016 at 14:31 UTC ( [id://1173121]=perlmeditation: print w/replies, xml ) Need Help??

Hello dear fellow monks,

It's Sunday, and I would like to confess to an awful sin of writing my own web framework from scratch. Yes, it's 2016, there are Dancer, Catalyst, Mojo, Kepl, and a dozen less known examples. I mostly did it for my own education, as well as to look at possible ways of handling the nuisances that have been plaguing me throughout my two last jobs. Yet I see some not so bad ideas in there and would like to share and possibly get some critique.

It's called Neaf [ni:f], which stands for Not Even A Framework.

Much like Dancer, it splits an application into a set of handler subroutines associated with URI paths. Unlike Dancer, however, it doesn't export anything (except one tiny auxiliary sub) into the application namespace. Instead, a know-it-all Request object is fed to a handler when serving request, like in OO CGI.pm or Kelp.

The response is expected in the form of unblessed hash reference which is in turn fed to the view object for rendering (Template Toolkit and JSON/JSONP currently supported, also Data::Dumper for debugging). Also the return value may contain some dash-prefixed switches altering the behavior of Neaf itself - awful looking, yet visible and simple way of doing it without going for a more complex structure.

Unlike anything I've seen so far, and much like Perl's own -T switch*, it offers no (easy) way to get user inputs without validation, either through regexp, or through a form validator. (Regexp-based one is in stock, LIVR also supported).

My not-so-impressive feature list so far:

  • GET, POST, and HEAD requests, query parameters (multivalues not done yet), uploads, cookies, and redirects;
  • Template::Toolkit, JSON::XS view out of the box;
  • Can serve raw generated content (e.g. images) and static files (like css/js);
  • CLI-based debugging (can simulate posts, uploads, cookies etc);
  • Can serve delayed or unspecified length replies, or do custom actions after the request is finished;
  • cookie-based sessions supported (no storage drivers available out of the box yet, though);
  • Form validation and resubmission;
  • Half-decent example directory and 79% overall test coverage.

The following is a valid application and can run as a CGI script, a PSGI app, or under Apache as a mod_perl handler**:

use strict; use warnings; use MVC::Neaf; MVC::Neaf->route( "/" => sub { my $req = shift; return { -template => \'Hello, [% name %]!', -type => 'text/plain', name => $req->param( name => qr/\w+/, "Stranger" ), }, }); MVC::Neaf->run;

So here it is: https://github.com/dallaylaen/perl-mvc-neaf. Thanks for reading this, and hope you enjoy the rest of the weekend.

*That said, I have not seen it often in production, certainly not in big apps with convoluted logic.

**Setting up a handler requires a little extra work though - see apache.cgi file in the distribution.

UPDATE Uploaded to CPAN.

Replies are listed 'Best First'.
Re: RFC: Not Even A Framework
by stevieb (Canon) on Oct 02, 2016 at 23:37 UTC

    I see you've already got a CPAN ID, why not put it up there?

    Makes it easier for some to play around with...

      Well, I'm planning to do that, but still unsure if CPAN needs another framework. Maybe I should have gone to PrePAN first...

      Update: x-posted to prepan.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://1173121]
Approved by atcroft
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2024-04-16 15:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found