The Mojolicious Blog.

A semi-official blog dedicated to the Mojolicious web framework

Day 4: Don't Fear the Full App

Arucaria trees in Curitiba Brazil

One of the most common misconceptions about Mojolicious is that there is a large difference between the declarative "Lite" apps that we have seen so far and large-scale structured "Full" apps. Nothing could be further from the truth. Mojolicious::Lite is a very tiny wrapper around the so-called "Full" app architecture, giving it the approachable keyword syntax.

Because it is much nicer to have concise single-file examples for documentation most of Mojolicious' documentation uses Lite syntax most of the time. It is understandable that people worry about migrating (or as we call it "growing") even once their apps would benefit from Object-Oriented structure; after all the docs seem geared towards Lite apps. However, let those fears go, the transition is easy. And once you understand it, the documentatation examples are trivial to translate.

Plus, Mojolicious comes with two forms of help when transitioning. The first is the Growing Guide which covers everything this post will but from the perspective of porting an existing application (which I won't duplicate here). The second is the inflate command which can even start you on the process by moving your templates from the data section and into their own files.

That said, in order to futher demystify things, I'm going to cover some of the differences and pull back the curtain on the Lite syntax itself.

Continue reading Day 4: Don't Fear the Full App...

Day 3: Using Named Routes

"my name is" stickers

One of the things we know from years of programming is that you should never hard-code anything if you don't have to. And yet far too many web application hard-code their urls, especially internal ones. But what if you didn't have to?

Each Mojolicious route has its own name which can be used to generate urls. If you don't specify one, one is generated, but you shouldn't rely on that name, give it one that is meaningful and relevant to your purposes. In lite apps, the name is the last parameter, after any defaults or callbacks. (In a full app it is an attribute, but we'll talk about those in another post).

Then when you need a url, rather than hard-coding it, use url_for or related functionality to generate a url by name, you can even pass placeholder values if needed. Let's see how it works!

Continue reading Day 3: Using Named Routes...

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

Day 1: Getting Started

hot air ballons

Start at the Beginning

In this Advent Calendar series, some posts will be introductory, some will be advanced, some will be on new features. Who knows what could be next? But for now let's ensure a level playing field by working out how to get started.

Continue reading Day 1: Getting Started...

Mojolicious Advent 2017

I'm happy to announce that I'll be running a Mojolicious Advent Calendar this year!

For those of you who are unaware, an advent calendar is a way to count down to Christmas through December. In many cases, the act of marking each day also provides a treat, usually chocolate. All told, 24 days are marked, one for each day from Dec 1 to Dec 24, Christmas Eve.

I'm not sure exactly how this moved into coding circles, but there is now a tradition of doing a similar countdown for programmers. The treats are little bits of wisdom on some programming topic.

Perl has http://www.perladvent.org/ which has run for quite a long time. Other calendars have popped up for other languages and I'm told it is even migrating to non-coding maker-type spaces!

This year I've decided that I'd like there to be one for the Mojolicious Web Framework. My desire is partly to put out some common starting tips and tricks and partly due to some recent additions that could really do with some extra publicity (they're cool, just wait!).

At least at the moment it is just me so, in the hopes of actually completing it, some posts might be brief. Some calendars try to use holiday themes in the posts, which I may do it the muse is upon me, but I make no promises.

I hope you'll enjoy no matter what religious or non-religious winter holidays you celebrate!