Gist of the Day: Mojo Merging Hashes

Mojolicious is a fun module/framework/thing for me. How do I love Mojolicious? Let me count the ways (sorry Shakespeare):

  1. Mojo has ridiculously few dependencies, especially when compared to other web frameworks
  2. Mojo brings web servers (morbo and hypnotoad) with it
  3. Mojo does MVC, or doesn’t do MVC, whatever I want to do
  4. Mojo has templates, and they’re outstanding
  5. Mojo does routes very simply, which is badass
  6. I can write an entire web application in only one file (though it could get big and may not be advisable) using Mojolicious::Lite
  7. Mojo has helpers which are actually helpful
  8. Mojo is crazy fast, so fast that if there is a performance problem I’m pretty sure it’s me
  9. The more I use Mojo, the more I find cool stuff that it can do

The Pre-Code Talk

So what I’m doing today is pretty simple. I’m going to make a web form which takes two inputs: hash1 and hash2. The user inputs two hashes in the format:

key1:value1,key2:value2,key3:value3
key1:value1,key2:value2a,key3:value3a,key4:value4

This should then output one hash with four keys (key1, key2, key3, and key4), with two of the keys having scalar values and the other two being array references.
It’s a pretty simple exercise, but I’m going to show you how to use Mojo a little bit more. Features of Mojo we’ll use are:

 The Code


So here we have all of the basics of a web application:

  • Form input
  • Form field validation
  • Output of processed form input
  • Templates

Again, note that the templates are below the __DATA__ token, and then see how we have the app showing error output per-field.

The Conclusion

I know this isn’t a terribly complex web app, but I think it’s a useful demonstration of Mojolicious::Lite for simple web applications.

One thought on “Gist of the Day: Mojo Merging Hashes

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.