1 / 37

How to create a simple Web application with Web::App Template::Toolkit and Class::DBI

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?. 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.

finnea
Download Presentation

How to create a simple Web application with Web::App Template::Toolkit and Class::DBI

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. How to create a simple Web application with Web::App Template::Toolkit and Class::DBI Leonard Miller June 17, 2008

  2. Who is this talk for?

  3. Who is this talk for? • Need to write web applications

  4. Who is this talk for? • Need to write web applications • Don’t want/cannot have a heavy framework

  5. Who is this talk for? • Need to write web applications • Don’t want/cannot have a heavy framework • Experienced Programmers.

  6. 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.

  7. 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.

  8. 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

  9. 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

  10. What is MVC • MVC stands for Model-View-Controller

  11. What is MVC • MVC breaks the work into three parts

  12. What is MVC • MVC breaks the work into three parts • Model - Short for database model. Class::DBI does all the database work: inserts/queries.

  13. What is MVC • MVC breaks the work into three parts • Model - Short for database model. Class::DBI does all the database work: inserts/queries. • View - Template::Toolkit does all the view/html work.

  14. What is MVC • MVC breaks the work into three parts • Model - Short for database model. Class::DBI does all the database work: inserts/queries. • View - Template::Toolkit does all the view/html work. • Controller - CGI::Application holds all the logic to glue the Model and the View together.

  15. 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"; }

  16. 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(); }

  17. What is MVC • Any questions regarding what MVC is?

  18. CGI::Application • A sample program • Helloworld.cgi <- config info • HelloWorldCgiApp.pm <- controller • Html files: <- View • header.html • body.html • footer.html • MyDatabase.pm <- Model

  19. CGI::Application helloworld.cgi: use HelloWorldCgiApp; my $helloworld = HelloWorldCgiApp->new(); $helloworld->run();

  20. 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();

  21. 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'); }

  22. CGI::Application $q -> param (‘rm’); 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'); }

  23. 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; }

  24. CGI::Application HelloWorldCgiApp (continued): sub cgiapp_prerun { my ($self, $runmode) = @_; my $q = $self->query; }

  25. 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 );

  26. 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 );

  27. Class::DBI • Using the Class::DBI Module HelloWorldCgiApp.pm: use MyDatabase; my @users = MyDatabase -> retrieve_all; $tt->process('body.html', { users => [ @users ], }, \$html);

  28. 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);

  29. Class::DBI • Using the Module – inserts: use MyDatabase; my $user = MyDatabase -> insert( { first_name => ‘Randall’, last_name => ‘Schwartz’, country_code => ‘US’, } );

  30. 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

  31. Template::Toolkit • Any html file is a TT file! • Simplest usage for a scalar: <html> <body> [% var_name %] </body> </html>

  32. Template::Toolkit • Usage for an Array where users is an array: <body> [% FOREACH item IN users %] [% item %]<br /> [% END %] </body>

  33. 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>

  34. 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>

  35. 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>

  36. Questions?

  37. Thank you Leonard Miller June 17, 2008

More Related