UTF-8 handling

How to handle when using UTF-8 with Mojolicious.

The point is Mitsu. One is to enable the utf8 pragma when dealing with UTF-8 in scripts. Of course, save the file in UTF-8.

use utf8;

(Currently Mojolicious automatically sets the utf8 pragma automatically, so you don't need to write it.)

The second is that the character string included in the parameters etc. has already been converted to the internal character string when the request reaches the controller. So, as a caveat, for example, when registering in the database, it is necessary to change from the internal string to the byte string.

The third is that when the template is drawn, it is converted from an internal string to a byte string. The template itself is converted from a byte string to an internal string when it is read, and automatically converted from an internal string to a byte string when it is output. Therefore, when passing parameters with the render method etc., it is necessary to pass the converted character string to the internal character string.

get'/' => sub {
  my $self = shift;

  # Converted to an internal string
  my $title = $self->param('title');
    
  #Convert internal string to byte string when saving to database

   #Convert byte string to internal string when retrieving from database

  # Pass an internal string
  $self->render(title => $title);
}

Associated Information