Richard Bucker

mojolicious – first app (part 2)

Posted at — Sep 20, 2011

I was going to call it a night but I just wanted to make a little more progress on this first app. I’d like to think I’m tired and cranky because I’m not seeing it. Mojolicious generated the following as my first app:#!/usr/bin/env perluse Mojolicious::Lite;# Documentation browser under “/perldoc” (this plugin requires Perl 5.10)plugin ‘PODRenderer’;get ‘/welcome’ => sub { my $self = shift; $self->render(‘index’);};app->start;DATA@@ index.html.ep% layout ‘default’;% title ‘Welcome’;Welcome to Mojolicious!@@ layouts/default.html.ep<!doctype html><html> <head><title><%= title %></title></head> <body><%= content %></body></html>That’s everything. It’s their version of hello world. The app started right away, however, it was my fault.  I pointed my browser to http://127.0.0.1:3000// and the page that popped up was some sort of 404 error page. It was nice enough… but much larger than my screen so I had to scroll to see everything.Which is when I realized that I was not seeing my project. After a quick code review I changed my browser to load http://127.0.0.1:3000//welcome and my welcome page arrived. However since there was no default page (aka index) or default redirect. I started looking.Skimming the documentation I found something that looked like:get ‘/’ => sub { . . .};This had promise as I added a redirect.get ‘/’ => sub { my $self = shift; $self->redirect_to('/welcome');};My only problem is/was that it would not recognize or redirect http://127.0.0.1:3000/. I had to read a lot more doc and make a few assumptions as to what was going on. The mojo docs indicate that the pattern could have been a regex, however, their pattern matching is more efficient just because it is and based on the way most people use it. And in their pattern matcher ‘/’ indicates that they are greedy matching placeholders and not a pattern per say. (I think that the * indicates that there has to be at least one byte/char)The strange thing is that ‘/’ and ‘/’ are not the same string and both will not match the empty path. Only ‘/’ will.So in my app I have a ‘/welcome’ in order to say hello. A ‘/*’ to get all of the non-empty paths so that I can redirect them to ‘/welcome’ and I have a ‘/’ in order to catch the empty path. What a pain in the ass! At least I only have a few pages.ELAPSED TIME: 30min