Moving Perl Mojolicious Routes to Their Own Module

A mentee asked me over the weekend if there was a way within a Mojolicious web application to store the routes separately from the main application class. Here's one way. These instructions assume you're using Perl 5.34 and Mojolicious 9.19 (the latest as of this writing) via the terminal command line on a Linux, Unix, or macOS system; make the appropriate changes if this doesn't apply to you.

First, if you haven't already, create your Mojolicious app at your shell prompt:

Localizing Dates in a Perl Web App With JavaScript

Last week's article received a comment on a private Facebook group that amounted to "just use JavaScript's built-in formatting." So what would that look like?

Perl
 




x
38


 
1
#!/usr/bin/env perl
2

           
3
use Mojolicious::Lite -signatures;
4
use DateTime;
5

           
6
get '/' =>
7
    sub ($c) { $c->render( template => 'index', date => DateTime->today ) };
8

           
9
helper localize_date => sub ( $c, $date = DateTime->today, $style = 'full' ) {
10
    my $date_params = join ',' => $date->year, $date->month_0, $date->day;
11
    return
12
        qq<new Date($date_params).toLocaleString( [], {dateStyle: "$style"})>;
13
};
14

           
15
app->start;
16
__DATA__
17

           
18
@@ index.html.ep
19
% layout 'default';
20
% title 'Today';
21

           
22
<ul>
23
    <li><script>
24
        document.write(<%== localize_date $date %>)
25
    </script></li>
26
    % for my $style ( qw(long medium short) ) {
27
    <li><script>
28
        document.write(<%== localize_date $date, $style %>)
29
    </script></li>
30
    % }
31
</ul>
32

           
33
@@ layouts/default.html.ep
34
<!DOCTYPE html>
35
<html>
36
    <head><title><%= title %></title></head>
37
    <body><%= content %></body>
38
</html>


It's structured much like the Perl-only solution, with a default "/" route and a localize_date Mojolicious helper to do the formatting. I opted to output a piece of JavaScript from the helper on lines 11 through 14 since it could be repeated several times in a document. You could instead declare a function in the default layout's HTML <head> on line 38 that would receive a date and a formatting style, outputting the resulting formatted date.

Localizing Dates in A Perl Web Application

When we're writing software for a global audience, it's nice if we can provide it according to their native languages and conventions. Translating all of the text can be a huge undertaking, but we can start small by making sure that when we show the day and date it appears as the user expects. For example, to me it's Tuesday, April 20, 2021; to my friend Paul in the UK, it's Tuesday, 20 April 2021 (note the difference in order); and to my other friend Gabór in Israel, it's יום שלישי, 20 באפריל 2021 (note the different direction of the text, different language, and character set).

Thankfully, we have a number of tools to assist us:

  • The DateTime::Locale library, which enables our Perl software to represent dates and times globally and contains a catalog of locales. It works with the DateTime library for storing our dates as objects that can be easily manipulated and formatted.
  • The HTTP Accept-Language header, which lets a web browser communicate to the server what natural languages and locale variants the user understands.
  • The HTTP::AcceptLanguage module, which helps us parse the Accept-Language header and select a compatible locale.

Our sample code uses the Mojolicious framework and is very simple; almost half of it is just HTML web page templates. You could easily adapt it to other frameworks or templating systems.