How to create a simple Web application with Web::App Template::Toolkit and Class::DBI
description
Transcript of How to create a simple Web application with Web::App Template::Toolkit and Class::DBI
![Page 1: How to create a simple Web application with Web::App Template::Toolkit and Class::DBI](https://reader035.fdocuments.net/reader035/viewer/2022062315/56814ebe550346895dbc6038/html5/thumbnails/1.jpg)
How to create a simple Web application with Web::App
Template::Toolkit and Class::DBI
Leonard MillerJune 17, 2008
![Page 2: How to create a simple Web application with Web::App Template::Toolkit and Class::DBI](https://reader035.fdocuments.net/reader035/viewer/2022062315/56814ebe550346895dbc6038/html5/thumbnails/2.jpg)
Who is this talk for?
![Page 3: How to create a simple Web application with Web::App Template::Toolkit and Class::DBI](https://reader035.fdocuments.net/reader035/viewer/2022062315/56814ebe550346895dbc6038/html5/thumbnails/3.jpg)
Who is this talk for?
• Need to write web applications
![Page 4: How to create a simple Web application with Web::App Template::Toolkit and Class::DBI](https://reader035.fdocuments.net/reader035/viewer/2022062315/56814ebe550346895dbc6038/html5/thumbnails/4.jpg)
Who is this talk for?
• Need to write web applications
• Don’t want/cannot have a heavy framework
![Page 5: How to create a simple Web application with Web::App Template::Toolkit and Class::DBI](https://reader035.fdocuments.net/reader035/viewer/2022062315/56814ebe550346895dbc6038/html5/thumbnails/5.jpg)
Who is this talk for?
• Need to write web applications
• Don’t want/cannot have a heavy framework
• Experienced Programmers.
![Page 6: How to create a simple Web application with Web::App Template::Toolkit and Class::DBI](https://reader035.fdocuments.net/reader035/viewer/2022062315/56814ebe550346895dbc6038/html5/thumbnails/6.jpg)
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.
![Page 7: How to create a simple Web application with Web::App Template::Toolkit and Class::DBI](https://reader035.fdocuments.net/reader035/viewer/2022062315/56814ebe550346895dbc6038/html5/thumbnails/7.jpg)
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.
![Page 8: How to create a simple Web application with Web::App Template::Toolkit and Class::DBI](https://reader035.fdocuments.net/reader035/viewer/2022062315/56814ebe550346895dbc6038/html5/thumbnails/8.jpg)
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
![Page 9: How to create a simple Web application with Web::App Template::Toolkit and Class::DBI](https://reader035.fdocuments.net/reader035/viewer/2022062315/56814ebe550346895dbc6038/html5/thumbnails/9.jpg)
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
![Page 10: How to create a simple Web application with Web::App Template::Toolkit and Class::DBI](https://reader035.fdocuments.net/reader035/viewer/2022062315/56814ebe550346895dbc6038/html5/thumbnails/10.jpg)
What is MVC
• MVC stands for Model-View-Controller
![Page 11: How to create a simple Web application with Web::App Template::Toolkit and Class::DBI](https://reader035.fdocuments.net/reader035/viewer/2022062315/56814ebe550346895dbc6038/html5/thumbnails/11.jpg)
What is MVC
• MVC breaks the work into three parts
![Page 12: How to create a simple Web application with Web::App Template::Toolkit and Class::DBI](https://reader035.fdocuments.net/reader035/viewer/2022062315/56814ebe550346895dbc6038/html5/thumbnails/12.jpg)
What is MVC
• MVC breaks the work into three parts1. Model - Short for database model.
Class::DBI does all the database work: inserts/queries.
![Page 13: How to create a simple Web application with Web::App Template::Toolkit and Class::DBI](https://reader035.fdocuments.net/reader035/viewer/2022062315/56814ebe550346895dbc6038/html5/thumbnails/13.jpg)
What is MVC
• MVC breaks the work into three parts1. Model - Short for database model.
Class::DBI does all the database work: inserts/queries.
2. View - Template::Toolkit does all the view/html work.
![Page 14: How to create a simple Web application with Web::App Template::Toolkit and Class::DBI](https://reader035.fdocuments.net/reader035/viewer/2022062315/56814ebe550346895dbc6038/html5/thumbnails/14.jpg)
What is MVC
• MVC breaks the work into three parts1. 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.
![Page 15: How to create a simple Web application with Web::App Template::Toolkit and Class::DBI](https://reader035.fdocuments.net/reader035/viewer/2022062315/56814ebe550346895dbc6038/html5/thumbnails/15.jpg)
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'}."<br>\n";}
![Page 16: How to create a simple Web application with Web::App Template::Toolkit and Class::DBI](https://reader035.fdocuments.net/reader035/viewer/2022062315/56814ebe550346895dbc6038/html5/thumbnails/16.jpg)
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();}
![Page 17: How to create a simple Web application with Web::App Template::Toolkit and Class::DBI](https://reader035.fdocuments.net/reader035/viewer/2022062315/56814ebe550346895dbc6038/html5/thumbnails/17.jpg)
What is MVC
• Any questions regarding what MVC is?
![Page 18: How to create a simple Web application with Web::App Template::Toolkit and Class::DBI](https://reader035.fdocuments.net/reader035/viewer/2022062315/56814ebe550346895dbc6038/html5/thumbnails/18.jpg)
CGI::Application• A sample program• Helloworld.cgi <- config info• HelloWorldCgiApp.pm <- controller• Html files: <- View
– header.html– body.html– footer.html
• MyDatabase.pm <- Model
![Page 19: How to create a simple Web application with Web::App Template::Toolkit and Class::DBI](https://reader035.fdocuments.net/reader035/viewer/2022062315/56814ebe550346895dbc6038/html5/thumbnails/19.jpg)
CGI::Applicationhelloworld.cgi:
use HelloWorldCgiApp;
my $helloworld = HelloWorldCgiApp->new();
$helloworld->run();
![Page 20: How to create a simple Web application with Web::App Template::Toolkit and Class::DBI](https://reader035.fdocuments.net/reader035/viewer/2022062315/56814ebe550346895dbc6038/html5/thumbnails/20.jpg)
CGI::Applicationhelloworld.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();
![Page 21: How to create a simple Web application with Web::App Template::Toolkit and Class::DBI](https://reader035.fdocuments.net/reader035/viewer/2022062315/56814ebe550346895dbc6038/html5/thumbnails/21.jpg)
CGI::ApplicationHelloWorldCgiApp (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');
}
![Page 22: How to create a simple Web application with Web::App Template::Toolkit and Class::DBI](https://reader035.fdocuments.net/reader035/viewer/2022062315/56814ebe550346895dbc6038/html5/thumbnails/22.jpg)
CGI::ApplicationHelloWorldCgiApp (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’);
![Page 23: How to create a simple Web application with Web::App Template::Toolkit and Class::DBI](https://reader035.fdocuments.net/reader035/viewer/2022062315/56814ebe550346895dbc6038/html5/thumbnails/23.jpg)
CGI::ApplicationHelloWorldCgiApp (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;
}
![Page 24: How to create a simple Web application with Web::App Template::Toolkit and Class::DBI](https://reader035.fdocuments.net/reader035/viewer/2022062315/56814ebe550346895dbc6038/html5/thumbnails/24.jpg)
CGI::ApplicationHelloWorldCgiApp (continued):
sub cgiapp_prerun
{
my ($self, $runmode) = @_;
my $q = $self->query;
}
![Page 25: How to create a simple Web application with Web::App Template::Toolkit and Class::DBI](https://reader035.fdocuments.net/reader035/viewer/2022062315/56814ebe550346895dbc6038/html5/thumbnails/25.jpg)
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
);
![Page 26: How to create a simple Web application with Web::App Template::Toolkit and Class::DBI](https://reader035.fdocuments.net/reader035/viewer/2022062315/56814ebe550346895dbc6038/html5/thumbnails/26.jpg)
Class::DBIMyDatabase.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 );
![Page 27: How to create a simple Web application with Web::App Template::Toolkit and Class::DBI](https://reader035.fdocuments.net/reader035/viewer/2022062315/56814ebe550346895dbc6038/html5/thumbnails/27.jpg)
Class::DBI• Using the Class::DBI ModuleHelloWorldCgiApp.pm:
use MyDatabase;
my @users = MyDatabase -> retrieve_all;
$tt->process('body.html',
{
users => [ @users ],
},
\$html);
![Page 28: How to create a simple Web application with Web::App Template::Toolkit and Class::DBI](https://reader035.fdocuments.net/reader035/viewer/2022062315/56814ebe550346895dbc6038/html5/thumbnails/28.jpg)
Class::DBI
• Using the Module
use MyDatabase;my @users = MyDatabase -> retrieve_all;my $user = MyDatabase -> retrieve( 1 );$tt->process('body.html', { users => [ @users ], user => $user, }, \$html);
![Page 29: How to create a simple Web application with Web::App Template::Toolkit and Class::DBI](https://reader035.fdocuments.net/reader035/viewer/2022062315/56814ebe550346895dbc6038/html5/thumbnails/29.jpg)
Class::DBI
• Using the Module – inserts:
use MyDatabase;
my $user = MyDatabase -> insert(
{
first_name => ‘Randall’,
last_name => ‘Schwartz’,
country_code => ‘US’,
}
);
![Page 30: How to create a simple Web application with Web::App Template::Toolkit and Class::DBI](https://reader035.fdocuments.net/reader035/viewer/2022062315/56814ebe550346895dbc6038/html5/thumbnails/30.jpg)
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
![Page 31: How to create a simple Web application with Web::App Template::Toolkit and Class::DBI](https://reader035.fdocuments.net/reader035/viewer/2022062315/56814ebe550346895dbc6038/html5/thumbnails/31.jpg)
Template::Toolkit
• Any html file is a TT file!
• Simplest usage for a scalar:<html>
<body>
[% var_name %]
</body>
</html>
![Page 32: How to create a simple Web application with Web::App Template::Toolkit and Class::DBI](https://reader035.fdocuments.net/reader035/viewer/2022062315/56814ebe550346895dbc6038/html5/thumbnails/32.jpg)
Template::Toolkit
• Usage for an Array where users is an array:
<body>
[% FOREACH item IN users %]
[% item %]<br />
[% END %]
</body>
![Page 33: How to create a simple Web application with Web::App Template::Toolkit and Class::DBI](https://reader035.fdocuments.net/reader035/viewer/2022062315/56814ebe550346895dbc6038/html5/thumbnails/33.jpg)
Template::Toolkit
• Usage for an Array where users is a hash:
<body>
[%item.user_id%] [%item.first_name%] [%item.last_name%] [%item.country_code%] <br />
</body>
![Page 34: How to create a simple Web application with Web::App Template::Toolkit and Class::DBI](https://reader035.fdocuments.net/reader035/viewer/2022062315/56814ebe550346895dbc6038/html5/thumbnails/34.jpg)
Template::Toolkit
• Usage for an Array where users is an array of hashes (like an array of rows from a database):
<body>
[% FOREACH item IN users %]
[%item.user_id%] [%item.first_name%]
[%item.last_name%] [%item.country_code%]<br />
[% END %]
</body>
![Page 35: How to create a simple Web application with Web::App Template::Toolkit and Class::DBI](https://reader035.fdocuments.net/reader035/viewer/2022062315/56814ebe550346895dbc6038/html5/thumbnails/35.jpg)
Template::Toolkit
• Usage for an Array where users is an array of hashes (like an array of rows from a database):
<body>
<form>
<input type=“hidden” name=“rm” value=“page_2”
[% FOREACH item IN users %]
...
[% END %]
<input type=“submit”>
</form>
</body>
![Page 36: How to create a simple Web application with Web::App Template::Toolkit and Class::DBI](https://reader035.fdocuments.net/reader035/viewer/2022062315/56814ebe550346895dbc6038/html5/thumbnails/36.jpg)
Questions?
![Page 37: How to create a simple Web application with Web::App Template::Toolkit and Class::DBI](https://reader035.fdocuments.net/reader035/viewer/2022062315/56814ebe550346895dbc6038/html5/thumbnails/37.jpg)
Thank you
Leonard MillerJune 17, 2008