Getting started with Perl development

Working toward the standards

Qiang Li

Topics?

[any material that should appear in print but not on the slide]
Project Directory Layout

The Current Layout

The new layout

In General

The new layout

For Project

The new layout

For Project

The new layout

Advantages

The new layout

Apache configuration file

The new layout

Apache configuration file

oh, Camel LOVE :-)

oh, Camel LOVE :-)

CGI::Application Intro

CGI::Application Intro

CGI::Application Intro - What is C::A

  • Model - Your data layer
  • View - Your user interface
  • Controller - Dispatch actions based on URL/query, choose Model and View to use, and pass data between Model and View.
  • more details: separate data (model) and user interface (view) concerns, so that changes to the user interface do not affect the data handling, and that the data can be reorganized without changing the user interface. The model-view-controller solves this problem by decoupling data access and business logic from data presentation and user interaction, by introducing an intermediate component: the controller.

CGI::Application Intro - An example

CGI::Application Intro - An example

what is runmode, runmodes is the action

CGI::Application Intro - An example

The traditional way...
  • big if/then/else control branching
  • barely structured and easily broken by even small changes in functionality.

CGI::Application Intro - work flows

  1. cgiapp_init
    • load database setting, config files
  2. setup
    • you define runmodes and start runmode.
  3. cgiapp_prerun
    • ACL control, event logging
  4. your runmode executes
  5. run other work-flow methods. such as cgiapp_postrun(), teardown().
thanks for the OO design, predefined work flows are stubs for you to override

CGI::Application Intro - Common usage

a simple one
              report.pl  ( the front-end script )
                  |
                  |
                  v
	               report.pm 
        ( handle http requests, 
	dispatch actions to helper module )
                  | \
                  |  \
                  v   v 
         chk_acl.pm   make_report.pm   
    ( doesn't need to know about HTTP at all )

CGI::Application Intro - Common usage

a little complicated one

CGI::Application Intro - Common usage

Base Class

CGI::Application Intro - Common usage

Base Class - the diagram
   customer.pl            product.pl                       order.pl
      |                      |                                |
      |                      |                                |
      v                      v                                v
 -----------------------------------------------------------------------
|                Base.pm   ( check_acl,  event_logging )                |
 -----------------------------------------------------------------------
                             |
                             |   Inherit check_acl, 
                             |  event_logging from Base.pm
                             v							
--------------------------------------------------------------------------		
  customer.pm                product.pm                      order.pm    |   
--------------------------------------------------------------------------  
      |                          |                              |  
      |	                         |                              |                                
      v                          v                              v
customer_related.pm           product._related.pm            order_related.pm
create/delete/edit customer   create/delete/edit product     create;/delete/edit order

CGI::Application Intro - Common usage

Base Class
package MyApp::Base;
use base 'CGI::Application';

sub cgiapp_prerun { 
    my $self = shift;
    my $run_mode = shift;    
    # log action before it is executed
    $self->event_log("$user is executing $run_mode");
    
    # authorization - can user execute this runmode ?
    my $authorized  = 
          $self->check_permission_on_runmode( $user,  $run_mode );
    
    # user has no access, redirect to forbidden runmode
    $self->prerun_mode('forbidden') unless $authorized ;
}
# another example
our $TEMPLATE_OPTIONS = {
        INCLUDE_PATH => "$ENV{APP_ROOT}/templates",
    };
MyApp::Base->tt_config( TEMPLATE_OPTIONS => $TEMPLATE_OPTIONS );

CGI::Application Intro - useful methods

CGI::Application Intro - C::A plugins - CGI

CGI::Application Intro - C::A plugins - TT


    package MyApp::Product;
    sub list_products {
       my $self = shift;
       my $products = get_products();
       return $self->tt_process( "my.tmpl" , 
                       { products => $products } ); 
       # OR process template 
       # 'MyApp/Product/list_products.tmpl' 
       # return $self->tt_process( 
			{ products => $products } );
    }
    

CGI::Application Intro - C::A plugins - Stream


    use CGI::Application::Plugin::Stream (qw/stream_file/);
    sub excel_report {
        my $self = shift;
        $self->header_add( -type => 'application/x-sc',  
		 -attachment => "myfile.xls" );
        $self->stream_file("/path/to/myfile.xls");
    }
    

CGI::Application Intro - other C::A plugins

Some Common Modules

MyApp::EnvConfig

MyApp::Conf

MyApp::DB

YuSis::CAP::EventLogging

Random leftover stuff

How to start a new project?

Typical Application layout would be

Module Management

modules, plugins, and tools are great

modules, plugins, and tools are great

things are still on the plate

References

That's All, Thank You!