Sunday 14 September 2014

how to install perl framwork catalyst and how it work

Standard
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 http://xrl.us/installperlnix | 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



$ catalyst.pl 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 test_server.pl -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 http://test.com/admin/articles/create 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
    test.pm          # 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
    favicon.ico
    static            # Directory for static files
        images        # Directory for image files used in welcome screen
script                # Directory for Perl scripts
    test_cgi.pl      # To run your app as a cgi (not recommended)
    test_create.pl   # To create models, views, controllers
    test_fastcgi.pl  # To run app as a fastcgi program
    test_server.pl   # The normal development server
    test_test.pl     # Test your app from the command line
t                     # Directory for tests
    01app.t           # Test scaffold      
    02pod.t          
    03podcoverage.t

The Root.pm controller is a place to put global actions that usually execute on the root URL. Open the lib/test/Controller/Root.pm 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/test_create.pl 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 test_create.pl script

This creates the lib/test/View/HTML.pm 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 "HTML.pm", which is a commonly used name for TT views. You can name it anything you want, such as "MyView.pm". If you have more than one view, be sure to set the default_view in Hello.pm (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/HTML.pm you will find that it only contains a config statement to set the TT extension to ".tt".
Now that the HTML.pm "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/test.tt template file (put it in the root under the test directory that is the base of your application). Here is a simple sample:

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

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 SIMPLE CONTROLLER AND AN ACTION

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

$ script/test_create.pl controller hello

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

In lib/test/Controller/hello.pm, add the following method:

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

    $c->stash(username => 'Netai Nayek',
              template => 'hello.tt');
}


Create a new template file in that directory named root/hello.tt 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