Saturday 31. December, 2011

A Mojolicious 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.]

Saturday 10. December, 2011

Beautiful Open Source

Earlier today, HP announced that they are open sourcing webOS, the mobile OS that they bought from Palm, inspired by BeOS, which Palm acquired a few years before that. Of course, these news comes as a consequence of HP canning their hardware platform, ending their tablet ambitions in a spectacular fire sale where they lost money on every device sold.

I’ve been a fan of the simplicity and elegance of webOS for a while. I even used BeOS back in it’s day. I didn’t really like the HP devices tho, and for me this is the optimal outcome. Provided that HP choses a liberal license, this means that webOS might actually get some adoption.

I even belive webOS would make a foundation for a nice Linux based desktop OS (Something Linux desperately needs), if provisions were made for a usable keyboard and touchpad multitouch gestures. Some of the leading internet pundits are less optimistic about this move than I am tho. John Gruber of Daring Fireball writes:

I hope I’m wrong, but I think this is just the difference between putting your dog down and letting it free on a distant mountain road.

Matt Gemmel takes it even further:

Want to know what happens when you make an operating system open? The same thing as when you leave your car open: sooner or later, it ends up smelling like a urinal.

Which I find a rather ridicolous notion. Certainly, a lot of Open Source apps were made by neckbeards with little or no design skills. However, WebOS is already very well designed, and even contains a nice style guide for apps. Just look how nice this Open Source twitter client for WebOS looks (phnx):

The distinguished interface style is already there, with gui widgets for app developers to use, which makes it more likely that usable and user friendly apps may be developed by an open source community. From running iusethis.com, I’ve noticed that when there’s already a defined style like on the mac, there’s a better chance of getting well designed open source apps.

Just look at QuickSilver, Cyberduck, Transmission and even Adium, widely used open source mac apps that are far ahead of their equivalent Linux-based apps. Being open source doesn’t have to mean design by comittee. It’s not like closed source is a guarantee of good design either.

What WebOS does need is improved performance, stability, better apis, and wider availability. The first things are absolutely things that we beardy Unix types are quite good at. Obviously Open Sourcing a codebase also means that professionals from several companies contribute, just look at the kind of boost this has given to Webkit. While on the subject of WebKit, the fact that WebOS apps are made using open web technologies is a very good fit for the open source community as well. Some of us that have been developing for the IOS platform are secretly yearning for a equally good open platform. Android is certainly not it.

Hopefully Open Sourcing with a liberal license will help with the last point too. I specially see Chinese cell phone producers as likely to pick up this opportunity, but possibly some of the existing android players as well. I’m even hoping there might be a distribution available for the iPad, and as I mentioned earlier, I am hoping to see webOS on prominent Linux distributions in the near future..

It’s not like this is a big closed source platform that is totally alien to the Open Source community. webOS already runs on linux, and uses a lot of the same open source technology we know and love (just check the open source information in the OS to verify this).

Hopefully HP will allow the current team to steward the OS forward. In my ideal world they would move the project to github and keep the contribution flow through pull requests where the core team is able to maintain a certain level of control.

If this happens, I see a bright future ahead for WebOS.

Sunday 4. December, 2011

Visualization of the Mojo class tree.

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

$ mojo get is X-ray for the web

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

Easily integrating your Mojolicious app with Facebook.

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.

Saturday 1. January, 2011

iPad as a writer’ s tool

I believe I have found my ideal writing tool. For the last couple of months, I have been drafting text using iA Writer on the iPad. While the softward keyboard is cumbersome to use, I find the focus the combination gives very inspiring. Nothing but me and the text, no IMs or emails to disturb me. I have turned off notifications on the iPad, so not even a ongoing game of Words With Friends can spoil my productivity.

Today I was out shopping on Oxford street, and on a whim I picked up the iPad keyboard dock. This thing is awesome! It has the required special keys to interact with the iPad’s functionality, but otherwise it feels just like Apple’s other excellent keyboards. It holds the iPad vertically in just the right angle, and plugs into a power source, as well as external speakers if I need music while I work. In short, I am in love.

I expect this to be my primary writing tool for the near future. If I’m going to nitpick, the only thing I am hoping for is higher DPI on the next generation of iPads so that iA Writer can fit a bit more text per line. One final trick; The keyboard lets you switch between languages with cmd-space. Exactly the same combination I use to switch between norwegian and english keyboard layouts on the mac. Pretty awesome.

Tags: ,
Thursday 16. December, 2010

My first Mojolicious Plugin – Proxy

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.

Tuesday 28. September, 2010

Mozilla Javascript documentation

JavaScript JS Documentation: JS String match, JavaScript String match, JS String .match, JavaScript String .match

Friday 6. August, 2010

3 reasons for the lack of updates here lately

Dark Nebula Episode 2

dark_nebula_ep2.jpg

Gravity Hook

gravity_hook.jpg

Van Helsing

van_helsing.jpg

Tuesday 22. June, 2010

i use this, now 50% more social.

We’ve just pushed a major update to iusethis, adding several social features and updating the commenting system with support for replies. To improve performance, we’ve also added a new caching system, to allow us to cache more data for logged-in users.

Find your friends

Looking up your contacts in GMail is a feature that we’ve wanted for a long time, but we did not want to compromise users by asking for their passwords. Now that Google supports OAuth we were able to implement it the way we wanted. In order to use it, go to your profile and click the ‘more’ link on the ‘friends’ sidebar:

i_use_this_mac_os_x_software__applications_used_by_marcus-20100622-095254.png

Once There, click on this sidebar:

i_use_this_mac_os_x_software__friends_of_marcus-20100622-094917.png

The system asks you to grant iusethis permission to access your address book data, choose the google account you use:

Google_Accounts-20100622-102953.png

Then we will present you with a list of contacts who have accounts on iusethis. Just press the ‘follow’ link to add them.

Messaging

You can now contact users on iusethis directly. To do this, go to the user’s profile, and click the contact link:

i_use_this_mac_os_x_software__Recent_comments_by_rob-20100622-103404.png

Then just write your message and click ‘Send’. Just like everywhere else on iusethis, you can use textile formatting in your message. The message will be delivered to the user as a normal email, with your registered email address as the ‘From’ address, so that he can easily respond to your message.

i_use_this_mac_os_x_software__software_for_the_osx_population-20100622-103530.png

If you do not want to accept messages from users, you can disable it from preference.

 

i_use_this_mac_os_x_software__software_for_the_osx_population-20100622-114940.png

We also added app owners to the app page, in case you want to contact them:

YoruFukurou_-_i_use_this_on_mac_os_x-20100622-143625.png

Replies

@username replies has been a defacto convention on iusethis for a long time, and we’ve already been doing things like linking to users automatically, but now we actually support links back to the comment you’re responding to. This also means that each comment has it’s own permanent page:

i_use_this_mac_os_x_software__comment_left_by_marcus_on_Steam_at_2010-06-16T08_43_38-20100622-130535.png

For consistency we are now using relative timestamps for dates everywhere, and the dates act as permalinks. By default, we will assume you are responding to the latest comment by this user on the app you’re commenting on, but if you want to reply to a specific comment, you can use the ‘reply to comment’ link on the comment permalink page. We also took the opportunity to streamline the user comments page to be more like the main recent comments list:

i_use_this_mac_os_x_software__Recent_comments_by_brh-20100622-142656.png

Hope these changes are useful to you. If you have feedback, please respond to out twitter account, @iut.