What is Catalyst?
Reference The author said, this is based on MayPole, increasing the features of various Ruby-ON-RAILS, STRUTS, Spring, Tomcat and other frameworks or applications in innovation. It seems exciting.
Download module
Catalyst has a range of modules that have been released on the CPAN. The most visible way is to install the bundle-catalyst-0.01 module.
Perl-McPan -e "Install bundle :: Catalyst"
Write a controller first
Since it is a MVC framework, let's first see the controller:
# lib / myapp.pm
Package myapp;
Use strict;
USE Catalyst '-debug';
__Package __-> config
Name => 'my Application',
root => '/ home / joeuser / myapp / root'
);
__Package __-> action (_default => sub {
MY ($ Self, $ C) = @_;
$ C-> Response-> Output ('Catalyst Rockz!');
});
1;
This module file named myapp.pm is our first controller. Name and root are the two option parameters required for Catalyst itself. Name represents the name of your application; root indicates the root directory deployed by your application, so other related resources are in it, including template files, etc.
The code is very simple, just use USE Catalyst in your module; however, the back parameter -debug is a debug switch.
The first thing is to tell the MVC kernel module, which application I am now, where is the root of all resources.
Next, the response processing code corresponding to the user request is defined. The user is requested by the URL, where the behavior is called Action, which is likely to draw on the Struts framework from Java. For example, you say http: // localhost / article / show / 2 means that you want to display a number 2, then show here is what we said. That if it is http: // localhost / article? In the system of static pages, both the WEB server returns /article/index.html file. INDEX.HTML is here a default resource. In Catalyst, we use the _default expression of the same concept. So in the code above, we defined a Action named _default. When the Action is not explicitly pointed out in the requested URL, Catalyst calls the Action method to return the corresponding processing content. Someone may ask more questions: Is there a controller in the URL? Very correct, that is, this. In fact, this URL structure is like: who wants anything (controller), but there are some conditions (behind the parameters, or part of the PATH, or the key value behind the question mark parameter).
In Catalyst, the Action Name The following line (_) is a private function, and it is impossible to request a response via URL. But there is an exception: _default. In the above example, _default gets a $ C object, which is our Controller object, so it shows a sentence on the page, as long as the Output method of the Response object is called by the controller.
Test
Catalyst itself has built a web server. Of course, you can use MOD_PERL on the production server.
Perl -i / home / joeuser / myapp / lib -mcatalyst :: test = myApp-E1 3000
Or you can also test the application directly from the command line:
Perl -i / home / joeuser / myapp / lib -mcatalyst :: test = myapp -e1 http: // localhost /
Other components
In the simplest application, the above controller module is already enough. However, the actual situation is much more complicated, so we use the MVC framework. A MVC framework naturally has three parts: Model, view, Controller, Catalyst automatically looks for corresponding resources from myApp / Model, MyApp / View, and MyApp / Controller directories.
So let's create these directories.
Under normal circumstances, you can start with the inherited Catalyst module,
# lib / myApp / view / tt.pm
Package myapp :: view :: tt;
Use strict;
Use base 'Catalyst :: View :: TT';
1;
This is your own View module, directly inherited Catalyst :: View :: TT. Now you can use in Action:
$ C-> Forward ('myapp :: view :: tt');
We add this VIEW named TT to the process chain:
# lib / myapp.pm
Package myapp;
Use strict;
USE Catalyst '-debug';
__Package __-> config
Name => 'my Application',
root => '/ home / joeuser / myapp / root'
);
__Package __-> action
_DEFAULT => Sub {
MY ($ Self, $ C) = @_;
$ C-> Stash -> {Template} = 'index.tt';
}
_end => sub {
MY ($ Self, $ C) = @_;
$ C-> Forward ('myapp :: view :: tt') Unless $ C-> Response-> Output;
}
);
1;
_end is another built-in action, which is called after all other Action calls are completed, so it is generally used to give relevant View, where we use TT to complete the content rendering of the template file. And _end relative to _begin, is called before each other Action call.
In the above example, _default is running, the controller is submitted to myapp :: view :: Tt, which uses the controller's Forward method. Our own MyApp :: View :: TT inherits from Catalyst :: View :: Tt, so you don't have to do anything, it will use $ c-> stash -> {template} to process the template file, and then put The result is output by $ C-> Response-> Output. In the Forward method here, we give parameters just a name of a class, and the Forward method performs the Process () class method in this class.
Stash is our highest level of Hash for storing and delivering data in all requests.
Don't forget to create a TT template file:
[% # root / index.tt #%]
Hello Catalyst!
Also create a CDBI-based Model is also very easy, but first create a database:
- myapp.sql
Create Table foo
ID Integer Primary Key,
Data Text
);
Create Table Bar
ID Integer Primary Key,
Foo Integer References foo,
Data Text
);
INSERT INTO FOO (DATA) VALUES ('Test!');
% SQLite /TMP/myApp.db For the sake of simplicity, we use the SQLite database, of course, you can also use other database products. Come now create this model: # lib / myapp / model / cdbi.pm Package myapp :: model :: cdbi; Use strict; Use base 'Catalyst :: Model :: CDBI'; __Package __-> config DSN => 'DBI: SQLite: /TMP/myapp.db', RELATIONSHIPS => 1 ); 1; This is all, gives the parameters (DSN) connecting the database, and specifies the automatic association, all database tables are automatically mapped, you don't have to care about the details. You can pass the data of the template file with $ c-> stash: # lib / myapp.pm Package myapp; Use strict; USE Catalyst '-debug'; __Package __-> config Name => 'my Application', root => '/ home / joeuser / myapp / root' ); __Package __-> action _end => sub { MY ($ Self, $ C) = @_; $ C-> Stash -> {Template} || = 'index.tt'; $ C-> Forward ('myapp :: view :: tt') Unless $ C-> Response-> Output; } 'View' => Sub { MY ($ Self, $ C, $ ID) = @_; $ c-> stash -> {item} = myapp :: model :: cdbi: foo-> retrieve ($ ID); ); 1; [% # root / index.tt #%] The ID is [% item.data%] Here we create a foo table, so automatic myapp :: model :: cdbi :: foo, used to express the database table, which is inherited by Class :: DBI, so use the retrieve method to retrieve Specify the record of the ID, returns the CDBI object, assign the value to the HASH variable. It has been said that the content of the Stash variable will be passed to the TT, so that Item.Data represents the Data method for outputting the Item this CDBI object, which is the content of the DATA field. So far, the easiest Catalyst application architecture has been reflected. However, specific practical applications have a lot of meticulous work to do. The purpose of Catalyst is to simplify your web application development, so you only need to pay attention to the specific business without having to care about other trivial details. postscript The content here, mostly refer to INTRO documents from Catalyst 2.99_12. As the project is further developed, I will continue to write. Or combine your own actual application to analyze and apply this frame. It is said that Perl is not good at Web development, yes, this is because Perl has no ripe, commercial web development framework. The reason is very simple, no one invests to do this. The author of Catalyst tries, learn from some of the smart code implementation, as well as the features of other MVC framework products to provide Perl programmers to develop frameworks for Perl.