Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Mojo 0.7 released (Perl on Rails in 150 lines of code)

by sri (Vicar)
on Oct 20, 2008 at 18:44 UTC ( #718268=perlnews: print w/ replies, xml ) Need Help??

(from the blog)

This is the biggest and most exciting Mojo update so far, because it finally includes the first example web framework named Mojolicious.

Mojolicious has many similarities to Ruby on Rails and Merb, but stays true to it's Perl roots.
While it's still just a proof of concept, it shows very well whats so cool about Mojo and how to get started with building web frameworks on top of it.

Trying Mojolicious is very simple thanks to the helpers and code generator shipped with it.
$ cpanp install Mojo ... no prereqs needed, so installation will be very fast ... $ mojolicious generate app MyMojoliciousApp [mkdir] /Users/sri/my_mojolicious_app/bin [write] /Users/sri/my_mojolicious_app/bin/mojolicious [chmod] my_mojolicious_app/bin/mojolicious 744 [mkdir] /Users/sri/my_mojolicious_app/lib [write] /Users/sri/my_mojolicious_app/lib/MyMojoliciousApp.pm [mkdir] /Users/sri/my_mojolicious_app/lib/MyMojoliciousApp [write] /Users/sri/my_mojolicious_app/lib/MyMojoliciousApp/Example.p +m [mkdir] /Users/sri/my_mojolicious_app/t [write] /Users/sri/my_mojolicious_app/t/basic.t [mkdir] /Users/sri/my_mojolicious_app/public [write] /Users/sri/my_mojolicious_app/public/404.html [exist] /Users/sri/my_mojolicious_app/public [write] /Users/sri/my_mojolicious_app/public/index.html [mkdir] /Users/sri/my_mojolicious_app/templates/example [write] /Users/sri/my_mojolicious_app/templates/example/welcome.phtm +l $ cd my_mojolicious_app $ bin/mojolicious daemon Server available at http://127.0.0.1:3000.
By setting the MOJO_RELOAD environment variable you can just live edit every single file, web application development has never been this simple.
$ MOJO_RELOAD=1 bin/mojolicious daemon Server available at http://127.0.0.1:3000.
Unlinke other frameworks Mojolicious won't try to hide the dispatcher logic from you.
The application class gives you total control.
package MyMojoliciousApp; use strict; use warnings; use base 'Mojolicious'; # This method will run for each request sub dispatch { my ($self, $c) = @_; # Try to find a static file $self->static->dispatch($c); # Use routes if we don't have a response code yet $self->routes->dispatch($c) unless $c->res->code; # Nothing found unless ($c->res->code) { $self->static->serve($c, '/404.html'); $c->res->code(404); } } # This method will run once at server start sub startup { my $self = shift; # The routes my $r = $self->routes; # Default route $r->route('/:controller/:action/:id') ->to(controller => 'example', action => 'welcome', id => 1); } 1;
Controllers are plain old Perl classes without any magic.
package MyMojoliciousApp::Example; use strict; use warnings; use base 'Mojolicious::Controller'; # This is a templateless action sub test { my ($self, $c) = @_; # Response object my $res = $c->res; # Code $res->code(200); # Headers $res->headers->content_type('text/html'); # Content my $url = $c->url_for; $url->path->parse('/index.html'); $res->body(qq/<a href="$url">Forward to a static document.<\/a>/); } # This action will render a template sub welcome { my ($self, $c) = @_; # Render the template $c->render; } 1;
By default Mojo::Template (also known as "phtml") will be used to render templates.
But Mojolicious was designed to use multiple parallel template engines. (File extension decides which one to use)
% my $c = shift; <h2>Welcome to the Mojolicious Web Framework!</h2> This page was generated from a template at templates/example/test.phtm +l, <a href="<%= $c->url_for(action => 'test') %>">click here</a> to move forward to a templateless action.
Mojo itself also got some big updates, here's the complete changelog.
- Added the Mojolicious Web Framework example.
- Added upload and GET/POST parameter helpers to Mojo::Message.
- Hooks for upload progress and stuff added.
- Refactored transfer encoding code into Mojo::Filter and Mojo::Filter::Chunked.
- Added callbacks for start line and header generators.
- Added workaround for missing IO::Seekable support in older versions of File::Temp (Perl 5.8).
- script/mojo.pl got renamed to bin/mojo.
- Mojo::Cache got renamed to Mojo::File because there will be a cache module named MojoX::Cache, and that could cause confusion later on.
- Fixed many escaping related bugs around Mojo::URL.
- Fixed 100-Continue support in Mojo::Server::Daemon and Mojo::Client.
- Countless small bugs fixed and tests added.
Like what you see here? Now would be the perfect time to get involved in the project!
Just join our mailing list or the irc channel (#mojo on irc.perl.org) and share your ideas with us.

Comment on Mojo 0.7 released (Perl on Rails in 150 lines of code)
Select or Download Code
Re: Mojo 0.7 released (Perl on Rails in 150 lines of code)
by zby (Vicar) on Oct 21, 2008 at 07:08 UTC
    # Default route $r->route('/:controller/:action/:id') ->to(controller => 'example', action => 'welcome', id => 1);
    Do you need to add non default routes? Or is this $r->route('/:controller/:action/:id') part enough to declare the parsing of the uri and routing the the appriopriate controller and action? If my assumptions are right - then does ->to really mean ->default?
      Nope, you don't have to define other routes if you don't want to, ->to is in fact synonymous to ->default.
        OK - so one more question. In the code above - does ->to add that default route to $r (i.e. globally) - or does it only add it to uris that can be parsed as '/:controller/:action/:id' (i.e. to the just added route)?
Re: Mojo 0.7 released (Perl on Rails in 150 lines of code)
by holli (Monsignor) on Oct 21, 2008 at 12:50 UTC
    I have just tried to install it, but it failed these tests :( Is Windows supported at all?
    t/mojo/daemon..............ok 1/6Can't create listen socket: Bad file +descriptor at C:/TEMP/Mojo-0.7/bin/../lib/Mojo/Script/Daemon.pm line +29 # Server timed out t/mojo/daemon..............NOK 2/6# Failed test at C:\TEMP\Mojo-0.7\ +blib\lib/Test/Mojo/Server.pm line 76. t/mojo/daemon..............NOK 3/6# Failed test at t/mojo/daemon.t l +ine 31. # got: undef # expected: '200' t/mojo/daemon..............NOK 4/6# Failed test at t/mojo/daemon.t l +ine 32. # got: undef # expected: '1' t/mojo/daemon..............NOK 5/6# Failed test at t/mojo/daemon.t l +ine 33. # '' # doesn't match '(?-xism:Mojo is working)' # Server not running t/mojo/daemon..............NOK 6/6# Failed test at t/mojo/daemon.t l +ine 36. # Looks like you failed 5 tests of 6. t/mojo/daemon..............dubious Test returned status 5 (wstat 1280, 0x500) Failed 1/22 test scripts. 5/882 subtests failed.
    Platform is WinXP, Perl 5.8.8


    holli, /regexed monk/
      Is Windows supported at all?
      I've been using cygwin on Windows for a long time with great great success. Cygwin has really been a savior to me. However, these days, Virtualbox is starting to become necessary because I need heavy duty unix support. The idea of running Perl for anything in straight windows gives me the willies. I like Windows as a desktop. And I am slave to it because I am a Go player and all the software is windows-only. But I need my Unix!

        Did you even think about answering the guy's question or did you just want to talk about Cygwin?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlnews [id://718268]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (5)
As of 2014-12-21 00:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (100 votes), past polls