Mojo Wonk Blog.

A semi-offical blog dedicated to the Mojolicious web framework

Day 10: Give the Customer What They Want

Wood bar counter with chairs

Writing an API can be as easy as taking the results of some database query and presenting it to the user. A more advanced one can often present the data in one of multiple formats. The user can then specify which format they want.

JSON is currently the most popular format for new APIs. XML is another common one and was certainly king before JSON hit the scene. An API might choose to make an HTML format of the data available, whether in some representation of the data or to render documentation about the API itself. Of course there are many others.

Mojolicious believes in Content Negotiation, as it is called, and supports it throughout the stack. Mojolicious makes it easy and convenient for users to pick a format they would like. Similarly it makes rendering the different formats easy on the site author, as you would expect.

Continue reading Day 10: Give the Customer What They Want...

Day 7: Using Template Variants For a Beta Landing Page

Single yellow tulip in a field of red tulips

CPAN Testers is a pretty big project with a long, storied history. At its heart is a data warehouse holding all the test reports made by people installing CPAN modules. Around that exists an ecosystem of tools and visualizations that use this data to provide useful insight into the status of CPAN distributions.

For the CPAN Testers webapp project, I needed a way to show off some pre-release tools with some context about what they are and how they might be made ready for release. I needed a "beta" website with a front page that introduced the beta projects. But, I also needed the same Mojolicious application to serve (in the future) as a production website. The front page of the production website would be completely different from the front page of the beta testing website.

To achieve this, I used Mojolicious's template variants feature.

Continue reading Day 7: Using Template Variants For a Beta Landing Page...

Day 2: The Stash

leather bag

In Mojolicious, when processing a request and preparing a response one of the most important concepts is "the stash". Since it is a non-blocking framework, your code can't use global variables to store any state during processing. If you did and some other code were to run, it could very easily get cross-talk between requests.

The stash is the place you can store information while you process it. It is just a simple hash reference that is attached to the controller object that is processing the request. It lives and dies with that one transaction.

While you can and should use it as a scratchpad, it really is much more. The stash controls almost every aspect of the response that you generate. Let's look a little closer to see how it works

Continue reading Day 2: The Stash...