How to create a simple Web application with CGI::Application Template::Toolkit and DBIx::Class Leonard Miller February 7, 2009.

Slides:



Advertisements
Similar presentations
(c) Creative Commons Attribution-ShareAlike License- Jason Purdy CGI::Application Raleigh.PM :: February 21 st, 2005 Jason Purdy
Advertisements

Purdue University Center for Education and Research in Information Assurance and Security Building a distributed intrusion detection system with Perl Diego.
Connecting to Databases. connecting to DB DB server typically a standalone application Server runs on localhost for smaller sites –i.e. Same machine as.
Connecting to Databases. relational databases tables and relations accessed using SQL database -specific functionality –transaction processing commit.
Programming MySQL, Perl COEN 351. Reading List Paul DuBois: MySQL and Perl for the Web, New Riders, 2002 Jacqueline D. Hamilton: CGI Programming 101,
PHP Hypertext Preprocessor Information Systems 337 Prof. Harry Plantinga.
Murali Mani SQL-PL Interface. Murali Mani Some Possible Options Web Interface Perl /CGI with Oracle/mySQL Install your own web server and use servlets.
PRACTICAL PHP AND MYSQL WALKTHROUGH USING SAMPLE CODES – MAX NG.
Implementing Session Support COEN 351. State Maintenance Client Side Mechanisms  Cookies Client needs to allow cookies Cookie handling done by browser.
NMED 3850 A Advanced Online Design February 25, 2010 V. Mahadevan.
J4www/jea Week 3 Version Slide edits: nas1 Format of lecture: Assignment context: CRUD - “update details” JSP models.
How to create a simple Web application with Web::App Template::Toolkit and Class::DBI Leonard Miller June 17, 2008.
Databases and Perl Using Perl to talk to databases.
M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #21 M.P. Johnson Stern School of Business, NYU Spring, 2005.
Python Web Applications A KISS Introduction. Web Applications with Python Fetching, parsing, text processing Database client – mySQL, etc., for building.
Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. Intermediate Perl Programming Class One Instructor:
 2004 Prentice Hall, Inc. All rights reserved. Chapter 25 – Perl and CGI (Common Gateway Interface) Outline 25.1 Introduction 25.2 Perl 25.3 String Processing.
Introduction to Perl Part III By: Cedric Notredame Adapted from (BT McInnes)
PHP1-1 PHP & SQL Xingquan (Hill) Zhu
Set 5: Perl and Database Connections
Databases with PHP A quick introduction. Y’all know SQL and Databases  You put data in  You get data out  You can do processing on it very easily 
Session 5: Working with MySQL iNET Academy Open Source Web Development.
INTERNET APPLICATION DEVELOPMENT For More visit:
Survey of Advanced Perl Topics Database access "OpSys"-like functions Signal Handling Inside-Out Objects.
CGI with Perl Darby Tien-Hao Chang (a.k.a. dirty) Department of Electrical Engineering, National Cheng Kung University.
Copyright © 2003 Pearson Education, Inc. Slide 8-1 The Web Wizard’s Guide to PHP by David Lash.
NMED 3850 A Advanced Online Design January 26, 2010 V. Mahadevan.
DBI tutorial February 5 th, What is DBI DBI is short for Database Interface, which is a Perl module DBI is short for Database Interface, which is.
Web Server Administration Chapter 7 Installing and Testing a Programming Environment.
PHP MySQL Introduction. MySQL is the most popular open-source database system. What is MySQL? MySQL is a database. The data in MySQL is stored in database.
Web Scripting [PHP] CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy.
Applied Research Laboratory UNCLASSIFIED IOUW 2000 Managing Oracle and Other Cool Things You Can Do with PERL John D. Groenveld Manufacturing Systems Division.
(Chapter 10 continued) Our examples feature MySQL as the database engine. It's open source and free. It's fully featured. And it's platform independent.
Final Project Implementation of Basic Authentication.
1 Basic Perl CGI Programming. 2 Issues How and when your program is invoked. Generating Response –HTTP Headers –HTML (or whatever document type you want)
Templates, Databases and Frameworks. Databases: DBI Common database interface for perl Provides a functional,
Databases On The Web with perl Archie Warnock
CISC 3140 (CIS 20.2) Design & Implementation of Software Application II Instructor : M. Meyer Address: Course Page:
Web Programming for DB Applications Yuen-Hsien Tseng 2006/04/18.
Just a Little PHP Programming PHP on the Server. Common Programming Language Features Comments Data Types Variable Declarations Expressions Flow of Control.
Introduction to Perl Yupu Liang cbio at MSKCC
Perl CGI What is "CGI"? Common Gateway Interface A means of running an executable program via the Web. Perl have a *very* nice interface to create CGI.
Introduction to Perl Part III By: Bridget Thomson McInnes 6 Feburary 2004.
Google App Engine Data Store ae-10-datastore
What’s a database? Data stored in a structured format that lends itself to easy manipulation and recall.
Java, JDBC, Perl, CGI Embedded SQL November 3 rd, 2000.
Web Scripting [PHP] CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy.
NMD202 Web Scripting Week5. What we will cover today PHP & MySQL Displaying Dynamic Pages Exercises Modifying Data PHP Exercises Assignment 1.
Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. Intermediate Perl Programming Class Four Instructor:
Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. Intermediate Perl Programming Class Three Instructor:
NMED 3850 A Advanced Online Design January 14, 2010 V. Mahadevan.
Just a Little PHP Programming PHP on the Server. Common Programming Language Features Comments Data Types Variable Declarations Expressions Flow of Control.
 2001 Prentice Hall, Inc. All rights reserved. Chapter 7 - Introduction to Common Gateway Interface (CGI) Outline 7.1Introduction 7.2A Simple HTTP Transaction.
Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. 1 Intermediate Perl Programming Class Two Instructor:
CHAPTER 10 PHP MySQL Database
Final Project Creating an interfaces to manipulate tables Professor. Jon Degallier Professor. Jon Degallier Student Name: Mahfuz Talukder Ohlone College.
CP476 Internet Computing Perl CGI and MySql 1 Relational Databases –A database is a collection of data organized to allow relatively easy access for retrievals,
CSC 405: Web Application Engineering II8.1 Web programming using PHP What have we learnt? What have we learnt? Underlying technologies of database supported.
Using ADO.Net to Build a Login System Dr. Ron Eaglin.
Databases and SQL CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
START Application Spencer Johnson Jonathan Barella Cohner Marker.
COMP234 - perl Perl DBI Topics Database vs access methods DBMS and DBMS API's Relational database SEQUEL Perl DBI SQL.
Web Systems & Technologies
Unix System Administration
PHP and MySQL.
Perl Database – Just Enough
Advanced Topics Web Programming.
Data Management Innovations 2017 High level overview of DB
Presentation transcript:

How to create a simple Web application with CGI::Application Template::Toolkit and DBIx::Class Leonard Miller February 7, 2009

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 Model - Short for database model. DBIx::Class 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.

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: 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 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 DB/Main.pm <- Model –DB/Main/Users.pm –DB/Main/Artists.pm –DB/Main/CDs.pm

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 => "Hello world!", }, ); $helloworld->run();

CGI::Application helloworld.cgi (with config info): use lib “~/testdir”; use HelloWorldCgiApp; my $helloworld = HelloWorldCgiApp->new ( PARAMS => { tt_config => { INCLUDE_PATH => ".", PRE_PROCESS => ‘test/header.html', POST_PROCESS => ‘test/footer.html', }, hw_string => "Hello test world!", }, ); $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 world!!!', }, \$html); return $html; }

CGI::Application HelloWorldCgiApp (continued): sub cgiapp_prerun { my ($self, $runmode) my $q = $self->query; #things you need to run every time #input validation etc. #logging }

DBIx::Class 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 ); | user_id | first_name | last_name | country_code | | 1 | joe | user | US | | 2 | Steve | Jobs | US | | 3 | Bill | Gates | US | | 4 | Larry | Wall | US |

DBIx::Class DB/Main.pm package DB::Main; use base qw/DBIx::Class::Schema/; __PACKAGE__->load_classes(); 1;

DBIx::Class DB/Main/User.pm package DB::Main::User; use base qw/DBIx::Class/; __PACKAGE__-> load_components(qw/PK::Auto Core/); __PACKAGE__->table('users'); __PACKAGE__->add_columns(qw/ user_id first_name last_name country_code /); __PACKAGE__-> set_primary_key('user_id');

DBIx::Class Using the DBIx::Class Module HelloWorldCgiApp.pm: use DB::Main; my $schema = DB::Main-> connect('dbi:mysql:db','user', 'password'); = $schema->resultset('User')->all; my $users_rs = $schema->resultset('User'); my $user = $users_rs ->next; $tt->process('body.html', { users => ], user => $user, }, \$html);

DBIx::Class Using the Module – inserts: my $new_user = $schema-> resultset('User')->new({ last_name => $last_name, first_name => $first_name, }); $new_user->insert;

DBIx::Class Things that you still need to do: Verify your data -- this is good practice under any circumstances Open your database connection Error handling -- did the DB connection open? Did the sql succeed? Unlike some larger frameworks, CGI::Application will not do the error checking for you. It’s a lite framework, so you still need to do some work

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 where item is a hash: [% item.user_id %] [% item.first_name %] [% item.last_name %] [% item.country %]

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 %] [% 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 %]

There’s more available CGI::Application::Plugin::AbstractCallback CGI::Application::Plugin::ActionDispatch CGI::Application::Plugin::ActionDispatch::Attributes CGI::Application::Plugin::AnyCGI CGI::Application::Plugin::AnyTemplate CGI::Application::Plugin::AnyTemplate::Base CGI::Application::Plugin::AnyTemplate::ComponentHandler CGI::Application::Plugin::AnyTemplate::Driver::HTMLTemplate CGI::Application::Plugin::AnyTemplate::Driver::HTMLTemplateExpr CGI::Application::Plugin::AnyTemplate::Driver::HTMLTemplatePluggable CGI::Application::Plugin::AnyTemplate::Driver::Petal CGI::Application::Plugin::AnyTemplate::Driver::TemplateToolkit CGI::Application::Plugin::Apache CGI::Application::Plugin::Apache2::Request CGI::Application::Plugin::Apache::Request CGI::Application::Plugin::Authentication CGI::Application::Plugin::Authentication::Driver CGI::Application::Plugin::Authentication::Driver::Authen::Simple CGI::Application::Plugin::Authentication::Driver::CDBI CGI::Application::Plugin::Authentication::Driver::DBI CGI::Application::Plugin::Authentication::Driver::DBIC CGI::Application::Plugin::Authentication::Driver::Dummy CGI::Application::Plugin::Authentication::Driver::Filter::crypt CGI::Application::Plugin::Authentication::Driver::Filter::lc CGI::Application::Plugin::Authentication::Driver::Filter::md5 CGI::Application::Plugin::Authentication::Driver::Filter::sha1 CGI::Application::Plugin::Authentication::Driver::Filter::strip CGI::Application::Plugin::Authentication::Driver::Filter::uc CGI::Application::Plugin::Authentication::Driver::Generic CGI::Application::Plugin::Authentication::Driver::HTPasswd CGI::Application::Plugin::Authentication::Store CGI::Application::Plugin::Authentication::Store::Cookie CGI::Application::Plugin::Authentication::Store::Session CGI::Application::Plugin::Authorization CGI::Application::Plugin::Authorization::Driver CGI::Application::Plugin::Authorization::Driver::DBI CGI::Application::Plugin::Authorization::Driver::Dummy CGI::Application::Plugin::Authorization::Driver::Generic CGI::Application::Plugin::Authorization::Driver::HTGroup CGI::Application::Plugin::Authorization::Driver::SimpleGroup CGI::Application::Plugin::AutoRunmode CGI::Application::Plugin::AutoRunmode::FileDelegate CGI::Application::Plugin::BREAD CGI::Application::Plugin::BrowserDetect CGI::Application::Plugin::CAPTCHA CGI::Application::Plugin::CHI CGI::Application::Plugin::Cache::Adaptive CGI::Application::Plugin::CaptureIO CGI::Application::Plugin::CompressGzip CGI::Application::Plugin::Config::Any CGI::Application::Plugin::Config::Context CGI::Application::Plugin::Config::General CGI::Application::Plugin::Config::IniFiles CGI::Application::Plugin::Config::Simple CGI::Application::Plugin::Config::YAML CGI::Application::Plugin::ConfigAuto CGI::Application::Plugin::DBH CGI::Application::Plugin::DBIProfile CGI::Application::Plugin::DBIProfile::Data CGI::Application::Plugin::DBIProfile::Graph::GDGraphInline CGI::Application::Plugin::DBIProfile::Graph::HTML CGI::Application::Plugin::DBIProfile::Graph::HTML::Horizontal CGI::Application::Plugin::DBIProfile::Graph::HTMLBarGraph CGI::Application::Plugin::DBIProfile::Graph::SVGTT CGI::Application::Plugin::DebugMessage CGI::Application::Plugin::DebugScreen CGI::Application::Plugin::DevPopup CGI::Application::Plugin::DevPopup::HTTPHeaders CGI::Application::Plugin::DevPopup::Log CGI::Application::Plugin::DevPopup::Timing CGI::Application::Plugin:: CGI::Application::Plugin::Eparam CGI::Application::Plugin::ErrorPage CGI::Application::Plugin::Feedback CGI::Application::Plugin::FillInForm CGI::Application::Plugin::Flash CGI::Application::Plugin::FormState CGI::Application::Plugin::FormValidator::Simple CGI::Application::Plugin::Forward CGI::Application::Plugin::HTCompiled CGI::Application::Plugin::HTDot CGI::Application::Plugin::HTMLPrototype CGI::Application::Plugin::HelpMan CGI::Application::Plugin::HtmlTidy CGI::Application::Plugin::I18N CGI::Application::Plugin::JSON CGI::Application::Plugin::LinkIntegrity CGI::Application::Plugin::LogDispatch CGI::Application::Plugin::Mason CGI::Application::Plugin::Menu CGI::Application::Plugin::MessageStack CGI::Application::Plugin::MetadataDB CGI::Application::Plugin::Output::XSV CGI::Application::Plugin::PageBuilder CGI::Application::Plugin::ParsePath CGI::Application::Plugin::Phrasebook CGI::Application::Plugin::ProtectCSRF CGI::Application::Plugin::RateLimit CGI::Application::Plugin::Redirect CGI::Application::Plugin::RequireSSL CGI::Application::Plugin::Routes CGI::Application::Plugin::RunmodeDeclare CGI::Application::Plugin::Session CGI::Application::Plugin::Stash CGI::Application::Plugin::Stream CGI::Application::Plugin::TT CGI::Application::Plugin::TT::LastModified CGI::Application::Plugin::TemplateRunner CGI::Application::Plugin::Thumbnail CGI::Application::Plugin::TmplInnerOuter CGI::Application::Plugin::ValidateRM CGI::Application::Plugin::View::HTML::Template CGI::Application::Plugin::ViewCode CGI::Application::Plugin::YAML

Questions?

Thank you Leonard Miller February 7th Frozen Perl 2009