Presentation is loading. Please wait.

Presentation is loading. Please wait.

Templates, Databases and Frameworks. Databases: DBI Common database interface for perl Provides a functional,

Similar presentations


Presentation on theme: "Templates, Databases and Frameworks. Databases: DBI Common database interface for perl Provides a functional,"— Presentation transcript:

1 Templates, Databases and Frameworks

2 Databases: DBI http://dbi.perl.org/ 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

3 Databases: DBIx::Class http://search.cpan.org/dist/DBIx-lass/lib/DBIx/Class/Manual.pod http://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

4 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;

5 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;

6 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

7 Template Toolkit Example [%# loop example -%] [% FOREACH book IN books -%] [% book.title %] [% book.rating %] [%# TT can handle complex data types including object method calls -%] ([% book.author_count %]) [% book.author_list %] [% END -%]

8 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

9 CGI::Application 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. 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.

10 CGI::Application http://www.cgi-app.org/ http://www.cgi-app.org/ 1.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 …

11 CGI::Application http://www.cgi-app.org/ http://www.cgi-app.org/ 2.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; }

12 CGI::Application http://www.cgi-app.org/ http://www.cgi-app.org/ 3.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.

13 Catalyst http://www.catalystframework.org/ http://www.catalystframework.org/ 1.Make the backbone: An application frame – with custom helper scripts bash> catalyst.pl MyApp bash> cd MyApp 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 TT TT bash> script/MyApp_create.pl model DB DBIC::Schema Catalyst::Schema... dbi:SQLite:books.db...

14 Catalyst http://www.catalystframework.org/ http://www.catalystframework.org/ 2.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); }

15 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

16 Example sites Catalyst example site (based on the tutorial) – http://cmpt470.csil.sfu.ca:8008/books 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 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://cmpt470.csil.sfu.ca:8008/cgi-application/books.cgi – http://code.google.com/p/cmpt470group8/source/browse/#svn/trunk/techeval/cgi-application http://code.google.com/p/cmpt470group8/source/browse/#svn/trunk/techeval/cgi-application (Database is rebuilt every day …)


Download ppt "Templates, Databases and Frameworks. Databases: DBI Common database interface for perl Provides a functional,"

Similar presentations


Ads by Google