Posts Tagged ‘Perl’

Saturday 31. December, 2011

A new year is upon us, and I figured this would be good time to take a look at the improvements in the Mojolicious framework in the last year. There has been a dazzling number of releases, 122 in fact, including the current release, 2.42.

Some of the releases were mere bugfix releases, with nothing noteworthy to add, but I’ll try going through the major releases in the last year. When this year started, we already had a robust MVC Web framework, and through the year you will note that a lot of the work has gone into building a first class real time web framework. 2010 ended with 1.0, codename Snow Flake, released 26th of december 2010. 1.01 fixed a couple of bugs, and added TLS Authentication.

The next major release, code name “Smiling Cat Face With Heart-Shaped Eyes”, was released on 2011-02-14. This release deprecated wildcard route names in favor of automatically generated default names for all routes. There were several new experimental features, including a more Moose-like attribute generator, on_start attribute for Mojo::Client, support for setting the user agent, mode-specific built-in templates to easily add a production error template, and CSS selectors in the ‘get’ command. There were also a new image helper, and support for session cookies.

The next major change was in 1.13, which deprecated the ailing Mojo::Client for a much improved Mojo::UserAgent, which handled async requests in a much more coherent fashion.There were also features added like IPV6 support and the default Fail Raptor added in 1.17 and argument localization in 1.18 before 1.3 which was released the 5th of may1.4.

1.3 was codenamed “Tropical Drink”, and tried to deprecate 5.8 support, 5.8 no longer being supported by the Perl core developers, and having serious security issues. However, due to RedHat users with old Perls, we had to temporarily revert that decision, before finally removing 5.8 support in release 2.0; Mojo::Base was also updated to enable 5.10 features. This release also added an experimental “before_render” hook and exposed the hooks in Lite apps.

1.4, “Smiling Face With Sunglasses”, followed about a month later, 2nd of june, and contained a major update to the Mojo::DOM parser, adding an ‘append’ method, direct hash access for attributes, and child element accessor, as well as collections. The release also added an ‘eval’ command, improved long poll support and started serializing sessions as JSON, which is more efficient than Storable.

Several minor releases followed, including 1.42 allowing status as an argument to render and 1.44 adding Morbo, the fearsome restarting development server. (DOOOM) this release also added an application mount plugin. 1.47 included a new callback condition as well as a host condition for the routes. 1.53 added format support to Mojo::Log and 1.57 dramatically improved Hypnotoad hot restart, allowing you a hot restart just by running Hypnotoad again, as well as adding a —stop parameter.

1.65 changed the IOLoop to be event based, and added support for using the EV event loop, allowing for great performance and compability with AnyEvent modules. In 1.69, IOLoop::Trigger allowed grouping nonblocking requests easily, and Mojo::DOM added healing support, allowing it to parse most markup that renders in a browser.

To make Mojolicious more RESTish, 1.73 added the experimental respond_to renderer, as well as type detection. 1.78 added a cpanify command and plugin generator to allow easy plugin writing and sharing. 1.80 normalized the casing of plugins, and 1.82 added a grep method to collections, later joined by ‘first’, ‘reverse,’,shuffle’ and ‘sort’.

Web socket testing was added in 1.86, 1.87 added an app command and a ‘t’ helper, and 1.99 adding group support to Lite apps, as well as binary support for web sockets.

All this as well as several minor features, bugfixes and documentation updates adds up to Mojolicious 2.0, code named “Leaf Fluttering In Wind” which was released 17th of october. It also added slice support to collections and completed the transition from callbacks to events. Web socket support was updated to ietf-17.

2.01 followed two days later, adding upgrade and part events, and 2.03 adding new experimental ietf http status codes. 2.10 added a websocket send_frame method and frame event, 2.19 revised the hook system to use events, and 2.27 was a major streamlining of the core IOLoop, deprecating several methods.

2.29 added chained events from the plugin system, and a new around_dispatch hook. 2.35 added etag support to Mojo Headers, and 2.37 marked the change of Mojolicious to being maintained by a core team, adding me(Marcus Ramberg), Glen Hinkle and Abhijit Menon-Sen to the core. This release also worked around a serious memory leak in Perl itself.

2.39 was released just before the holidays, and updated the user agent to use an ‘error’ event, as well as exposing ‘local_address’ and adding a close method to IOLoop Streams. 2.40 was released on the 24th, adding JSON Pointer support, and finally 2.41 was released on the 28th stabilizing many of the experimental features mentioned above, and allowing session expiry to be turned off.

We now believe the real time part of Mojolicious to be as robust as the MVC part, and expect less frequent releases of the framework in 2012. If you want to learn more about the features in Mojolicious, check out our excellent guides.

Happy New Year, and thanks for 2011.

[All the information in this document was extracted from the Mojo Changelog.]

Sunday 4. December, 2011

In this visualization, I’ve tried to group the various Mojo classes by function and color code them. The classes are placed in layers, and related groups are close to each other. Note that I’ve dropped abstract base classes and Mojolicious related subclasses from this visualization, as well as some servers and all the commands.

This might be useful as a quick reference, if you want to print it, you can get the PDF here.

I’ve stuffed the omnigraffle source as well as a pdf and jpeg representation on github, in case someone wants to play with it:
https://github.com/marcusramberg/mojo-classes. It’s all licensed under CC BY-SA, so feel free to hack on it.

*updated* Was missing Test::Mojo, also made it a bit more symetrical while I was at it.

Wednesday 30. November, 2011

Mojolicious isn’t just useful for perl coders, it also includes a command line tool that can be quite handy for anybody who wants to get info from the web:

usage: /Users/marcus/perl5/perlbrew/perls/perl-5.14.2/bin/mojo get [OPTIONS] \
URL [SELECTOR] [COMMANDS]
  mojo get /
  mojo get mojolicio.us
  mojo get -v -r google.com
  mojo get --method POST --content 'trololo' mojolicio.us
  mojo get --header 'X-Bender: Bite my shiny metal ass!' mojolicio.us
  mojo get mojolicio.us 'head > title' text
  mojo get mojolicio.us .footer all
  mojo get mojolicio.us a attr href
  mojo get mojolicio.us '*' attr id
  mojo get mojolicio.us 'h1, h2, h3' 3 text

These options are available:
  --charset <charset>     Charset of HTML5/XML content, defaults to auto
                          detection or "UTF-8".
  --content <content>     Content to send with request.
  --header <name:value>   Additional HTTP header.
  --method <method>       HTTP method to use, defaults to "GET".
  --redirect              Follow up to 5 redirects.
  --verbose               Print verbose debug information to STDERR.

First, the name can be a bit awkward when you use it often. I tend to shorten it to ‘mg’:

    $ alias mg='mojo get'

mg is a command line utility similar to curl, but with some really neat tricks up it’s sleeve.

As you can see from the examples above, mg allows you to use familiar CSS selectors to process the response body. This turns out to be very useful. For instance, to get the links to the frontpaged apps on my site iusethis.com:

$ mg -r iusethis.com 'h2 a' attr href 

http://osx.iusethis.com/app/corebreach

http://osx.iusethis.com/app/terraray

http://osx.iusethis.com/app/preferencemanager

http://osx.iusethis.com/app/panoedit

http://osx.iusethis.com/app/findanyfile

http://osx.iusethis.com/app/webkit

http://osx.iusethis.com/app/nulanaslauncher

http://osx.iusethis.com/app/pagelayers

http://osx.iusethis.com/app/arrivalsampdepartures

http://osx.iusethis.com/app/iawriter

Check the top one for link tags:

$ mg -r http://osx.iusethis.com/app/corebreach link

<link href="http://osx.iusethis.com/appcast/corebreach" rel="alternate" title="Sparkle AppCast" type="application/rss+xml" />
<link href="http://osx.iusethis.com/comment/app.rss/corebreach" rel="alternate" title="Recent Comments" type="application/rss+xml" />
<link href="http://osx.iusethis.com/static/opensearch.xml" rel="search" title="iusethis" type="application/opensearchdescription+xml" />

Neat, an appcast, let’s take a look at the version history:

 $ mg http://osx.iusethis.com/appcast/corebreach title text
Appcast for CoreBreach
CoreBreach 1.1
CoreBreach 1.0.2
CoreBreach 1.0.1
CoreBreach 1.0

Mojo understands much more complex queries than these tho. Pretty much anything you can use in jQuery or CSS 3 works.
A good way to find these defintions is to open up your web page with the Chrome debugger:

Just right-click the element you want to know more about and choose ‘Inspect Element’ from the menu. This gives you a really simple view of the DOM, which shows you which element on the web page you are highlighting, as well as the parent nodes to your element for use in selectors.

As for the last argument, you can call anything that you can call on a Mojo DOM Node.
If you exclude it you just get the markup as you saw above.

Because the client uses the Mojo UserAgent, it already supports features like HTTPS and basic authentication credentials in the URL.
mojo get also supports features like doing posts and setting headers, as well as setting the method and the request body, but I think it’s this DOM queries that puts it apart from the other HTTP clients like curl, wget or lwp-download. Here’s a final trick:

Set MOJO_USERAGENT_DEBUG=1 in your environment to get full traces of your HTTP requests.

Like it? Installation is really simple and fast. Go get it!

Saturday 8. January, 2011

Facebook has recently released a new Graph API. It’s a simple RESTish API, and uses the newly released OAuth2 spec for authorization. This makes it a great match for Mojolicious, and using my new OAuth2 plugin (Also coming to a CPAN mirror near you), it’s absolutely trivial.

Here is a simple Lite example. Just register an app with facebook to get the key and secret.

   use Mojolicious::Lite;
   # configure the plugin
   plugin 'oauth2', facebook => {
       key => 'my-key',
       secret => 'my-secret' };

   get 'hello' => sub {
      My $self=shift;
      #redirects the gets the token asynchronous
   $self->get_token('facebook', callback => sub {
      my $token=shift;
      my $me=$self->client->get(
        'https://graph.facebook.com/me?access_token='.$token)->res->json;
          $self->render( text =>
            "Hello ".$me->{name} );
    });

As you can see, the plugin handles the request flow automatically. We then use the built in JSON parsing in the Mojolicious Client to turn the API data into structures we can work with in our application.

Updating facebook data is just as trivial, you just use post requests with the data you need to update. See http://graph.facebook.com/ for more info on that.

If you plan to run your app through a standalone daemon such as Hypnotoad (I really think you should :) It could be worth it to rewrite the last section of your app to take advantage of async operations like this:

  $self->client->async->get(        'https://graph.facebook.com/me?access_token='.$token, sub {
  my $me=shift->res->json;
  $self->render( text=>"Hello ".$me->{name} );
})->start;

The async plugin itself also supports an optional async=>1 setting to enable async fetching of the token.

Thursday 16. December, 2010

A few days ago I uploaded my first Mojolicious plugin to CPAN, Mojolicious::Plugin::Proxy. My use case was that I am writing a simple single-file html5/javascript app, which needed to access a remote JSON feed. When running on a device with file:/// URLs, cross-site scripting is not an issue, but when running on a web-server, my appis unable to reach that remote JSON feed, so I wrote this plugin to allow me to simply proxy JSON requests from the local server to the remote one. This is how it works

    plugin 'proxy';
    get 'ws' => sub { shift->proxy_to('http://remote_url', with_query_params=>1);

The fact that Mojolicious includes a full async http client made this plugin trivial to implement. The unit tests is almost more code than the module itself.

Thursday 3. June, 2010

Longing for a more Mooseish DBIx::Class? Eventually there will be MooseX::DBIC, but if you need something while you’re waiting, you could check out MooseX::DBIC::AddColumn which lets you do Moose-like column definitions in your Result classes.

Wednesday 2. June, 2010

I’m mentoring a Google Summer of Code project to improve the Mojolicious test suite this year, and my student has just set up a blog to journal his progress. Check it out, you might even learn something.

Tuesday 1. June, 2010

12:27 <ribasushi> a call to all git-heads

12:28 <ribasushi> recently haarg did a seemingly perfect conversion of dbic from svn to git (a non-trivial feat)

12:28 <ribasushi> I am calling for people who actually know git to randomly poke at the new history and notufy me of whatever problems they see

12:28 <ribasushi> http://github.com/haarg/DBIx-Class

12:29 <ribasushi> (also you can ascertain the quality of the conversion, and potentially draft haarg to do a catalyst conversion as well – it took him a couple of freaking hours to do dbic)

12:32 <marcus> awesome

12:32 <marcus> haarg++

12:33 <marcus> it looks freaking good

12:33 <ribasushi> marcus: please spread the word to get more eyeballs on this, if nothing pops up in the next day and a half we’ll be  switching dbic over

Consider this me having spread the word to you ;-)

Sunday 28. March, 2010

Thinking about submitting a talk for NPW 2010 in Reykjavik? The deadline for the Call for Paper is tomorrow, 29th of march, so you’d better hurry up and get that submission in there. I’ve submitted a talk, and am really looking forward to visit iceland. I’ve never been, but from the photos I’ve seen it seems like a beautyful place.

Yet another Perl MVC micro-framework with a name that will be confused with my wiki software. From the Author’s description:

(PoC)yet another sinatra-ish framework built on CPAN module.

Rather than ‘shaggadelic’ or ‘dance’, this one starts the app with ‘zigorou’. I can’t see it catching on.