The smallest Mojolicious application
Now that Mojolicious is installed, let's create a small web application. First of all, create a file called webapp.pl . Try writing the following code in this file.
use Mojolicious::Lite; get'/' => sub { my $self = shift; $self->render(text =>'Hello World'); };; app->start;
The familiar Hello World application.
Run application
Let's run this web application first. Use the morbo command to run the application.
morbo webapp.pl
Then, you should see the following.
[Tue Mar 25 14:08:11 2014] [info] Listening at "http: // *: 3000". Server available at http://127.0.0.1:3000.
This means that the server is available at "http://127.0.0.1:3000". Let's access the following URL immediately.
http://127.0.0.1:3000
Did you see "Hello World"? If it is displayed, it is successful. You are now a web application engineer. Because I made a web application.
Press "Ctrl + c" to stop.
Problems with Windows
In case of Windows, it seems that the interrupt of "Ctrl + c" is hard to be recognized. It will stop if you press "Ctrl + c" many times. This is a bit inconvenient, so it's a good idea to use the start command to launch it in a separate window. If you open another window, you can stop morbo with the "x" button.
start morbo webapp.pl
Explanation of morbo command
The morbo command was installed when you installed Mojolicious. The morbo command loads the web application and starts the development server. By default, it listens on port 3000. And if there is access to port 3000 on the local host (127.0.0.1), it will return a response.
morbo is a convenient server for development, which automatically detects when you change a file. This means that you don't have to restart morbo, just change the file. Because of this, the development efficiency is very good.
morbo is a development server, but you can start a production server with the hypnotoad command. I would like to explain the hypnotoad command when explaining how to start an application in a production environment.
Application description
Now I would like to explain the meaning of the application.
Loading #Mojolicious::Lite use Mojolicious::Lite; # Routing settings get'/' => sub { my $self = shift; #Drawing the contents $self->render(text =>'Hello World'); };; #Starting the Mojolicious application app->start;
Loading Mojolicious::Lite
Mojolicious includes a module called Mojolicious::Lite . With Mojolicious::Lite, you can create a web application with a single file.
Loading #Mojolicious::Lite use Mojolicious::Lite;
use is for loading modules in Perl. You can load the Mojolicious::Lite module by writing "use Mojolicious::Lite".
If you load Mojolicious::Lite, you will be able to use some functions such as get function and app function. It also has the effect of automatically enabling strict and warnings. When using Mojolicious::Lite, it is not necessary to write "use strict" and "use warnings".
Routing description
To write a web application, first write routing .
# Routing settings get'/' => sub { my $self = shift; ... };;
You can write the routing using the get function. The first argument of the get function is the URL pattern. For example, write / or / info . The last argument describes the process to be executed. Write this as a subroutine reference.
get (URL pattern, reference to subroutine that describes the process to be executed)
If this description is made and the URL pattern is matched, the process described in the subroutine reference will be executed.
In Perl, you can omit the parentheses in the function. Also, the comma "," can be described using "=>". So the above has the same meaning as below.
get URL pattern => Subroutine reference that describes the process to be executed
At first glance, it may seem strange, but the entity is just a function. Like the sample code, it is often written in abbreviated notation, so let's remember it.
Receiving controller object
Let's take a look at the content of the next process to be executed. First, let's get the controller object as the first argument of the subroutine. A controller object is an object that is created for each HTTP request, and you can use this object to perform various processes. The entity is a Mojolicious::Controller object.
get'/' => sub { #Receiving a controller object my $self = shift; $self->render(text =>'Hello World'); };;
I think those who first saw what shift is. In Perl you can use the shift function to get the first argument of a subroutine. So what is assigned to $self is the first argument of the subroutine. A controller object is passed as this first argument.
In the explanation that follows, this subroutine part is sometimes called a controller, so please keep it as a tail. Also, keep in mind that controller objects can be represented as $c .
$c->render(...);
Drawing text
Next, let's look at the drawing part of the text.
get'/' => sub { my $self = shift; #Drawing text $self->render(text =>'Hello World'); };;
To render the text, use the render method and specify text in the first argument and the content you want to render in the second argument. The render method is a method of the Mojolicious::Controller class and is used when rendering the contents.
This description will display "Hello World" on your browser.
Start application
The last thing to remember is the start of the application.
#Starting the Mojolicious application app->start;
By making this description, the server will start and accept HTTP requests.
This concludes the discussion of the smallest web application. Next time, let's grow this little web application.