How to create a simple Web application with Web::App Template::Toolkit and Class::DBI Leonard Miller June 17, 2008
Who is this talk for?
Need to write web applications
Who is this talk for? Need to write web applications Don’t want/cannot have a heavy framework
Who is this talk for? Need to write web applications Don’t want/cannot have a heavy framework Experienced Programmers.
Who is this talk for? Need to write web applications Don’t want/cannot have a heavy framework Experienced Programmers. Know about CPAN, how to learn from CPAN’s documentation.
Who is this talk for? Need to write web applications Don’t want/cannot have a heavy framework Experienced Programmers. Know about CPAN, how to learn from CPAN’s documentation. Newer programmers that could use a new/better way to organize their code.
Why these three Modules? Separate out the code to MVC Pieces Each can be used/tested alone The modules themselves are easy to use and understand
Why not Catalyst? mod_perl/fastcgi: You don’t always have the access on the machine to get Catalyst to work. CGI::Application is a ‘lite’ framework, and as such is much smaller. Not as big and scary. Trivial to install in a local ~/lib dir
What is MVC MVC stands for Model-View-Controller
What is MVC MVC breaks the work into three parts
What is MVC MVC breaks the work into three parts 1.Model - Short for database model. Class::DBI does all the database work: inserts/queries.
What is MVC MVC breaks the work into three parts 1.Model - Short for database model. Class::DBI does all the database work: inserts/queries. 2.View - Template::Toolkit does all the view/html work.
What is MVC MVC breaks the work into three parts 1.Model - Short for database model. Class::DBI does all the database work: inserts/queries. 2.View - Template::Toolkit does all the view/html work. 3.Controller - CGI::Application holds all the logic to glue the Model and the View together.
What is MVC Who has seen code like this: use DBI; my $sql = "select * from users"; my $dbh = DBI->connect( $ds, $un, $pw ); my $sth = $dbh->prepare($sql); $sth->execute(); print "Content-type: text/html\r\n\r\n"; while (my $h = $sth->fetchrow_hashref()) { print ”Name is:".$h->{'first_name'}." \n"; }
What is MVC Who has seen code like this: my $q = new CGI; if ($q-> param('first_name' eq ''){ print input_form(); } else{ my $sql = "insert into users..."; my $sth = $dbh->prepare($sql); $sth->execute(); print submission_form(); }
What is MVC Any questions regarding what MVC is?
CGI::Application A sample program Helloworld.cgi <- config info HelloWorldCgiApp.pm <- controller Html files: <- View –header.html –body.html –footer.html MyDatabase.pm <- Model
CGI::Application helloworld.cgi: use HelloWorldCgiApp; my $helloworld = HelloWorldCgiApp->new(); $helloworld->run();
CGI::Application helloworld.cgi (with config info): use HelloWorldCgiApp; my $helloworld = HelloWorldCgiApp->new ( PARAMS => { tt_config => { INCLUDE_PATH => ".", PRE_PROCESS => 'header.html', POST_PROCESS => 'footer.html', }, hw_string => "hi there big earth!", }, ); $helloworld->run();
CGI::Application HelloWorldCgiApp (continued): package HelloWorldCgiApp; use base 'CGI::Application'; use Template; sub setup { my $self = shift; $self->run_modes( 'mode1' => 'start’, 'mode2' => 'sec_page' ); $self->start_mode('mode1'); }
CGI::Application HelloWorldCgiApp (continued): package HelloWorldCgiApp; use base 'CGI::Application'; use Template; sub setup { my $self = shift; $self->run_modes( 'mode1' => 'start’, 'mode2' => 'sec_page' ); $self->start_mode('mode1'); } $q -> param (‘rm’);
CGI::Application HelloWorldCgiApp (continued): sub start { my $self = shift; my $tt_config = $self->param(‘tt_config’) my $tt = Template->new( $tt_config ); $tt->process('body.html', { hwstr => 'hi big planet!!!', }, \$html); return $html; }
CGI::Application HelloWorldCgiApp (continued): sub cgiapp_prerun { my ($self, $runmode) my $q = $self->query; }
Class::DBI The mysql table: CREATE TABLE users ( user_id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, first_name varchar(75) NOT NULL, last_name varchar(75) NOT NULL, country_code CHAR(2) NULL );
Class::DBI MyDatabase.pm: package MyDatabase; use base 'Class::DBI'; MyDatabase->connection('dbi:mysql:db',‘user’,’pw'); MyDatabase -> table('users'); MyDatabase -> columns( All => qw/user_id first_name last_name country_code / ); MyDatabase -> columns( Primary => user_id );
Class::DBI Using the Class::DBI Module HelloWorldCgiApp.pm: use MyDatabase; = MyDatabase -> retrieve_all; $tt->process('body.html', { users => ], }, \$html);
Class::DBI Using the Module use MyDatabase; = MyDatabase -> retrieve_all; my $user = MyDatabase -> retrieve( 1 ); $tt->process('body.html', { users => ], user => $user, }, \$html);
Class::DBI Using the Module – inserts: use MyDatabase; my $user = MyDatabase -> insert( { first_name => ‘Randall’, last_name => ‘Schwartz’, country_code => ‘US’, } );
Template::Toolkit Why use Template::Toolkit? –Common ‘look and feel’ templates. Easy to change for those people who are better at design than we are. –What type of data is It good for? arrays hashes scalars
Template::Toolkit Any html file is a TT file! Simplest usage for a scalar: [% var_name %]
Template::Toolkit Usage for an Array where users is an array: [% FOREACH item IN users %] [% item %] [% END %]
Template::Toolkit Usage for an Array where users is a hash: [%item.user_id%] [%item.first_name%] [%item.last_name%] [%item.country_code%]
Template::Toolkit Usage for an Array where users is an array of hashes (like an array of rows from a database): [% FOREACH item IN users %] [%item.user_id%] [%item.first_name%] [%item.last_name%] [%item.country_code%] [% END %]
Template::Toolkit Usage for an Array where users is an array of hashes (like an array of rows from a database): <input type=“hidden” name=“rm” value=“page_2” [% FOREACH item IN users %]... [% END %]
Questions?
Thank you Leonard Miller June 17, 2008