ForewordThere are very few Habrés, or rather no articles at all on such a wonderful MVC framework like Catalyst. And so I decided to devote my first article to him.
Here I described the basic principles for working with Catalyst on the example of a simple web application, the so-called quick start for mastering this framework.
IntroductionCatalyst is a powerful MVC framework for developing Perl web applications.
A few words about MVC (Model / View / Controller):
- Model (Model) - contains only data and methods for working with them.
- View (View) - is responsible for exactly how this data will look like.
- Controller (Controller) - manages the communication between the system and the user, and also controls the necessary interactions when the application is running.
Catalyst - contains an extensive number of CPAN modules that facilitate the development of a Perl web application.
What is the simplicity when using this framework?
')
The process of creating a new project is performed using the command:
catalyst.pl Test::App
(in the file system in the directory with the project name "::" it will be replaced with "-".
This script is part of the auxiliary system, which has the name Catalyst :: Helper, which generates the necessary scripts, folders, tests, files, etc. for the future application.
The structure of the created project:
- / lib - the heart of the project, contains all perl code designed for the web application to work, consists of subdirectories created automatically:
- Test / App / Model - for modules implementing the model;
- Test / App / View - ... view;
- Test / App / Controller - ... controller;
- Test / App / App.pm - module for setting up a web application.
- / root - here are stored templates for View, necessary css, js, images, in general, all static data;
- / script - contains scripts that were automatically created by Helper. They are required to run:
- test server;
- testing a specific URL;
- create MVC components;
- running scripts like CGI / FastCGI;
- etc.
- / t - contains tests
- Changes - change history of your project.
- Makefile.PL - service information for installing necessary modules when deploying an application.
- test_app.conf is a configuration file, service variables are specified (it has the highest priority at startup than settings inside the application).
- README - information on the launch of the application, its installation, configuration, etc.
The required models / views / controllers are added using the appropriate command:
script/test_app_create.pl {model|view|controller} { }
Contents of the TestApp.pm file
package Test::App; use Moose; use namespace::autoclean; use Catalyst::Runtime 5.80; use Catalyst qw/ -Debug
As we can see, Catalyst uses the Moose module for the object display of the CPAN elements used, which greatly simplifies working with objects in the Perl language.
The Root.pm file which is located in the / lib / Test / App / Controller / folder looks like this:
Now, after getting acquainted with this framework, we will try to create a simple web application.
1. Create a view using the
script/test_app_create.pl view Web TT
command (TT - Template Toolkit, templates, other templates can also be used, for example, Mason)
2. Create the index.tt file in the root folder /
Paste this code there:
[% IF result %]<p>[% result %]</p> [% END %] <form name="hello" method=POST action="hello"> <input type="text" name="hi" value=""> <input type="submit" value="Do something!"> </form>
3. In the Root.pm file we delete the following line:
$c->response->body( $c->welcome_message ); #
$c->response->body( $c->welcome_message ); #
4. add hello method there:
sub hello :Local { my ($self, $c) = @ _; my $hi = $c->req->body_params->{hi}; $c->stash( result => $c->model('Hello')->hello($hi), template => 'index.tt', ); }
5. Create model
script/test_app_create.pl Model Hello
script/test_app_create.pl Model Hello
6. In the created model we write the method:
sub hello { my ($self, $hi) = @ _; return "Hello $hi!\n"; }
7. Run the
script/test_app_server
test server
8. In the browser, enter
localhost:3000/
localhost:3000/
(default port)
We admire the result.
PS Here, in principle, the basic ideas when working with Catalyst, and I would like more articles on Habré from people who know this framework. Documentation is certainly good, but real experience is always more interesting.
PPS While writing this article and here pearl was not without magic. The code uses the array "@_", which is translated in tags when specifying the language of pearls in a habrauser with the name "_", did not think of anything beautiful how to put spaces between @ and _. Be careful…