Tag Perl

Perl needs modern garbage collection

Chromatic just wrote a list of important features that Perl 5 needs. While I agree with most of the listed ones (and in fact contributed to the new p5-mop proposal during the recent Hackathon), I found one feature curiously missing.

I mean, JWZ pointed this out in 1998; Perl garbage collection is a bad joke. That was 14 years ago, and the situation is the same. It is trivial to create a circular reference and trip it up. Especially when you are doing evented code where callbacks are common, it is quite easy to close over some variable and create a leak. Languages like C#, Ruby and Java has this sorted already with variants of mark and sweep-based systems, Python has built in cycle detectors in their ref count implementation, and even Objective-C manages to detect cycles fairly well with their new compile-time automated reference counting. 
Don’t worry so much about adding sugar to the syntax. This is a feature Perl really needs. 

Moving to Moose Hackathon 2012

This week, my excellent Perl Mongers group Oslo.pm, led by the excellent Salve,  has arranged an event we called the Moving to Moose Hackathon 2012. The purpose of this event was to promote the adoption of a MOP (Meta Object Protocol) in Perl development. To this effect, we had two groups, the Perl RDF community, looking to adopt moose in their modules, and Stevan Little and his merry gang of p5-mop developers, looking to create a proposal for a MOP in the Perl core itself, to replace the venerable blessed hash object model.

 
Unfortunately due to the arrival of baby Milla in June, I was unable to attend the entire event, which started on saturday. Thus I missed the walk to Pulpit Rock (did I mention the event was smack in the middle of the Norwegian fjords?) Luckily I’ve been there before (see pic on the right :-). I arrived early Tuesday morning by the night train. On the way there, I re-read the proposal for the p5-mop
 
Pulpit Rock
 
Since I’m not a mad hatter scientist like Yuval and Jesse, and don’t know the Moose MOP that well, I concentrated my efforts on tooling and real world triage of the prototype. For me, the most exciting idea of the proposal is adding first class ‘method’, ‘has’ and ‘class’ keywords, so that you can declare Perl classes like this:
 
    class Cat (extends => Animals) {
      has $claws;
      method meow { … }
    }
 
Since I’m an avid TextMate fan (specially since the Open Source release this fall), I started by extending the TM2 syntax to support these new keywords. This means getting proper highlighting, as well as easy navigation in classes and methods. You can find my fork of the official Perl syntax highlighter here.
 
I then started work on porting a few Mojo base classes to the new syntax. Being one of the Mojolicious core developers, I know these classes rather well, and they have the advantage of being light on dependencies and optimized for easy porting to Perl6, as well as having comprehensive test suites. This made them an ideal target for trying out features of the prototype. 
 
Of course I met some hardship underways. The biggest issue for me during the port was the lack of BUILDARGS, a class method to massage the constructor parameters into a hash, but all in all the experience was quite educational. I also found some small points of improvement in the specifications during my attempts, and submitted a simple doc patch. 
 
Together with Jan Henning, I also looked into the feasibilty of supporting overloading in the new p5-mop classes, and Jan Henning ended up writing a new meta class to support this. I believe this should be refactored into a meta role when the mop has been updated with support for that.
 
In the period that I was there, the main p5-mop developers struggled quite a bit with an issue in the dispatching that was giving bizarre errors and was really hard to track down. Unfortunately, this slowed down development, as we were unable to start using the new two-phase bootstrap code which  would allow us much easier development of the meta classes themself. 
 
However, I was really excited about the discussions about the new ‘meta’ keyword on the last night of the hackathon to allow meta defintions (and thus class methods, and roles) in the class defintions themselves.  I quite look forward to the updated prototype, and hope to resume my port as soon as BUILDARGS support has landed.
 
I think the crucial question now is if we can manage to maintain velocity in the development of the p5-mop prototype after this hackathon. I truly hope we can start seeing real world usage of it soon. This is the best way to figure out gotchas and problems with the specification before it lands in the core.

Debugging LWP with mitmproxy

Are you using an LWP app, and trying to debug it? My personal preferences is using mitmproxy.

To get LWP to use mitmproxy, set the following ENV variables:

PERL_LWP_ENV_PROXY=1 HTTPS_PROXY=’http://localhost:8080/’ ./script/myapp

Note, this also works for apps using Mojo::UserAgent, except you don’t need the first ENV variable.

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

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.

$ 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!

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.

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.

Mooseish DBIC Schemas

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.

Mojolicious GSOC blog

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.

Copyright © marcus ramberg
nordaaker

Built on Notes Blog Core
Powered by WordPress