Sunday, 14 September 2014

how to install perl framwork catalyst and how it work

today i will tell you how you install perl and catalyst in your linux system

first of all what is catalyst:
catalyst is a framework of perl its have some complication, in this article i will tell you how you install perl with catalyst

ok first of all install perl in your system so follow bellow command

$ sudo curl -L | bash

after perl install complete next install catalyst so follow bellow command

$ sudo cpanm Catalyst::Devel

now you have catalyst and perl so now you can test your framework so create folder perl for your project product and go to this folder and follow bellow command

$ test

here test is your project name

 you can see a test is created in your folder and also lot's of folder and file like

so now your project folder is created now run this command in terminal

$ perl -r

it will run catalyst server in your machine and next open web browser and type http://localhost:3000 then open a page like

this is a catalyst test page if show this then your server is work fine

in catalyst The default is to map URLs to controller names, and because of the way that Perl handles namespaces through package names, it is simple to create hierarchical structures in Catalyst. This means that you can create controllers with deeply nested actions in a clean and logical way. For example, the URL maps to the package test::Controller::Admin::Articles, and the create method.

so now i will create sample controller
now you have bellow kind of your folder structure

Changes               # Record of application changes
lib                   # Lib directory for your app's Perl modules
    test             # Application main code directory
        Controller    # Directory for Controller modules
        Model         # Directory for Models
        View          # Directory for Views          # Base application module
Makefile.PL           # Makefile to build application
test.conf            # Application configuration file
README                # README file
root                  # Equiv of htdocs, dir for templates, css, javascript
    static            # Directory for static files
        images        # Directory for image files used in welcome screen
script                # Directory for Perl scripts      # To run your app as a cgi (not recommended)   # To create models, views, controllers  # To run app as a fastcgi program   # The normal development server     # Test your app from the command line
t                     # Directory for tests
    01app.t           # Test scaffold      

The controller is a place to put global actions that usually execute on the root URL. Open the lib/test/Controller/ file in your editor. You will see the "index" subroutine, which is responsible for displaying the welcome screen that you just saw in your browser.

sub index :Path :Args(0) {
    my ( $self, $c ) = @_;
    # Hello World
    $c->response->body( $c->welcome_message );
Later on you'll want to change that to something more reasonable, such as a "404" message or a redirect, but for now just leave it alone.

so now if you edit  $c->welcome_message to "hello world"
then you will see in browser show "Hello World"

now we will test a template engine in catalyst follow bellow command

$ perl script/ view HTML TT

after this command run you will get error so you need view helper so follow bellow command

$ cpanm install "Catalyst::View::TT"

after install view::TT package now you run script

This creates the lib/test/View/ module, which is a subclass of Catalyst::View::TT.
  • The "view" keyword tells the create script that you are creating a view.
  • The first argument "HTML" tells the script to name the View module "", which is a commonly used name for TT views. You can name it anything you want, such as "". If you have more than one view, be sure to set the default_view in (See Catalyst::View::TT for more details on setting this).
  • The final "TT" tells Catalyst the type of the view, with "TT" indicating that you want to use a Template Toolkit view.
If you look at lib/test/View/ you will find that it only contains a config statement to set the TT extension to ".tt".
Now that the "View" exists, Catalyst will autodiscover it and be able to use it to display the view templates using the "process" method that it inherits from the Catalyst::View::TT class.

Create a root/ template file (put it in the root under the test directory that is the base of your application). Here is a simple sample:

    This is a TT view template, called '[% %]'.
Change the hello method in lib/Hello/Controller/ to the following:
sub test :Global {
    my ( $self, $c ) = @_;
    $c->stash(template => '');

now open browser and load http://localhost:3000/test page you can see show a message what you have write in view file


 Create a controller named "hello" by executing the create script:

$ script/ controller hello

This will create a lib/test/Controller/ file (and a test file). If you bring up in your editor, you can see that there's not much there to see.

In lib/test/Controller/, add the following method:

sub test :Local {
    my ( $self, $c ) = @_;

    $c->stash(username => 'Netai Nayek',
              template => '');

Create a new template file in that directory named root/ and include a line like:

<p>Hello, [% username %]!</p>

and open browser againload http://localhost:3000/test/hello/test page then you can see like this