160 likes | 255 Views
Templates, Databases and Frameworks. Databases: DBI http://dbi.perl.org/. Common database interface for perl Provides a functional, consistent interface Supports at least 72 different RDBMS including ODBC
E N D
Databases: DBI http://dbi.perl.org/ • Common database interface for perl • Provides a functional, consistent interface • Supports at least 72 different RDBMS including ODBC • Database plugins are used to integrate into DBI – hiding db specific code from developers • Easy to change application database
Databases: DBIx::Classhttp://search.cpan.org/dist/DBIx-lass/lib/DBIx/Class/Manual.pod • Example of a popular Object Relational Mapper • Builds on DBI’s functionality • Allows you to hide SQL statements in perl code • Encapsulates differences in SQL syntax between different RDBMs
DBI Example use DBI; my $dsn = "dbi:mysql:database=testme"; my $dbh = DBI->connect($dsn,$username,$password); my $statement = $dbh->prepare( "select * from sometable where somefield=?“ ); $statement->execute("something") or die $statement->errstr; while (my $row = $statement->fetch) { my ($field1,$field2,$field3) = @$row; # do something with $field1 etc } $statement->finish;
DBIx Example # Example (from DBIx::Class documentation) for “artist” table package MyDB::Schema::Result::Artist; use base qw/DBIx::Class::Core/; __PACKAGE__->table('artist'); __PACKAGE__->add_columns(qw/ artistid name /); __PACKAGE__->set_primary_key('artistid'); __PACKAGE__->has_many(cds => 'MyDB::Schema::Result::CD'); 1;
Templates • Not being able to embed perl in a web page • Annoyance of perl for web programming • Solution: templating modules • Some Examples: TT (Template Toolkit) http://search.cpan.org/~ingy/Template-Toolkit-Simple-0.08/lib/Template/Toolkit/Simple.pm HTML::Template http://search.cpan.org/~samtregar/HTML-Template-2.9/Template.pm
Template Toolkit Example [%# loop example -%] <table> [% FOREACH book IN books -%] <tr> <td>[% book.title %]</td> <td>[% book.rating %]</td> [%# TT can handle complex data types including object method calls -%] <td>([% book.author_count %]) [% book.author_list %]</td> </tr> [% END -%] </table>
Web Frameworks • The annoyance of using templates is that even a moderately complex application can become very messy very quickly. • Frameworks “fix” this by hiding complexity. • Two examples: CGI::Application http://search.cpan.org/~markstos/CGI-Application-4.31/lib/CGI/Application.pm Catalyst http://search.cpan.org/~hkclark/Catalyst-Manual-5.8004/lib/Catalyst/Manual.pm
CGI::Application Catalyst Newer web framework Full blown application server 1700+ modules. Limited support for apache. Comes with a built in web server for testing. More sophisticated but seemed to be clunkier to use in practice. Requires a great deal of installation. Had to reinstall many modules as Ubuntu was slightly out of date. • Older toolkit can be embedded into a cgi script, mod_perl module or fastcgi script. • Less well documented but easier to learn and use. • Has plugins for most of the major tasks you'd want to perform. • DBI and CGI are well supported • Probably too unstructured for programmers who want a more formal MVC interface from the start.
CGI::Applicationhttp://www.cgi-app.org/ • Make the backbone: a module that extends CGI::Application package Books; # perl supports multiple inheritance use base qw{CGI::Application CGI::Application::Plugin::DBH CGI::Application::Plugin::TT}; use CGI::Application; # base application module use CGI::Application::Plugin::DBH; # database connectivity plugin use CGI::Application::Plugin::TT; # templating plugin …
CGI::Applicationhttp://www.cgi-app.org/ • Implement required methods: package Books; # continued sub setup { my $self = shift; $self->run_modes( list => 'showlist', ... other actions ... ); $self->start_mode(list'); ... other configuration stuff (doesn't produce html) ... } sub teardown { my $self = shift; ... cleanup stuff ... } # for our "list" action sub showlist{ ... do something ... ... this function will produce html (never prints directly) ... return $html; }
CGI::Applicationhttp://www.cgi-app.org/ • Make script to run application: Typical url: http://.../books.cgi?rm=list #!/usr/bin/perl # in books.cgi use Books; use strict; my $bl = Books->new(); $bl->run(); • Scripts can be customized through the new method.
Catalysthttp://www.catalystframework.org/ • Make the backbone: An application frame – with custom helper scripts bash> catalyst.pl MyApp bash> cdMyApp bash> perl Makefile.PL -- This will build tools you can use to make controllers, views and models bash> script/MyApp_create.pl controller Site bash> script/MyApp_create.pl view TTTT bash> script/MyApp_create.pl model DB DBIC::Schema Catalyst::Schema ... dbi:SQLite:books.db ...
Catalysthttp://www.catalystframework.org/ • Customize the application: Example: adding an action to a controller Url http://.../error/1103 is mapped to this method Will display a TT template with the errno variable set # in lib/MyApp/Root.pm (the default controller) add an error message sub errordisplay :Chained('/') :PathPart('error') :Args(1) { my ($self, $c, $error) = @_; $c->stash(template => 'books/error.tt‘, errno => $error); }
Conclusion Would I use these? • I would not use Catalyst • Poor integration with apache • Can only run one instance at a time • Way too complex: what happens if something doesn’t work? • I might use CGI::Application • Flexible and simple enough • Neither are as fast as a well written php application • Would need some compelling reason / lack of functionality
Example sites • Catalyst example site (based on the tutorial) • http://cmpt470.csil.sfu.ca:8008/books logins: test01 / mypass (admin), test02 / mypass (ordinary user) • http://code.google.com/p/cmpt470group8/source/browse/#svn/trunk/techeval/catalyst • CGI::Application example site (partial clone of Catalyst site) • http://cmpt470.csil.sfu.ca:8008/cgi-application/books.cgi (no login required) • http://code.google.com/p/cmpt470group8/source/browse/#svn/trunk/techeval/cgi-application (Database is rebuilt every day …)