up
Perl Lecture Notes - Feb 22
A bit more DBI as well as various other modules to help cgi stuff
- Housekeeping
- Creating images on the fly
- a CPAN module I think is cool - GD.pm
- Interface to libgd graphics library,
which must also be installed
- pod docs
- A function plotting example :
run it
- source
(Creates a .png graphics file from a function like "sin(x)".)
- GD.pm creates and manipulates .jpeg, .png files, and
a couple other file formats. (It originally did
.gif files but now does not because of copyright issues.)
- Chapter 13 in CGI Programming discusses GD.pm, as
well as other dynamic image tricks and the more
powerful image transform libary ImageMagick and its perl
interface Image::Magick.
- Apache::DBI - persistent database connections
- Purpose: speed up dbase connections in CGI scripts
- Method: each apache httpd process manages a pool of connections
and reuses them.
- Installation: (1) install CPAN's Apache::DBI, (2)
install mod_perl into Apache (if you don't already have it)
(3) put "PerlModule Apache::DBI" httpd.conf
(All this is installed on bob already.)
- Changes to your .cgi scripts: none. (!) Effectively, this
module overrides $dbi->close, and squirrels away the connection
instead. Next time a process tries to DBI->connect with the
the same connection string, Apache::DBI hands over the already
established connection. Very smooth.
- Apache::DBI docs
- CGI::Speedy
- Purpose: speed up your perl cgi scripts
- Method: after script executes it doesn't quit but instead
sleeps in the background. Next invocation connects to
same process.
- Installation: download, build, install Speedy binary.
(This can be in your local directory). CGI scripts
then run the speedy* executable, not perl.
- Changes to your .cgi: replace #!/usr/bin/perl with
#!/home/user/bin/speedy or wherever you put the binary.
As with mod_perl, the cgi code must be clean enough
to run in a persistent environment, in which the same
script is called many times.
- Pros: doesn't change apache httpd or requre root privileges
- Cons: different speedy process hanging around for
each cgi script - can be a problem if you have many different
scripts.
- Other comparable technologies: mod_perl (integrated into Apache), FastCGI (launched from apache, but persistent outside httpd)
- Example: DawnDance
web site that I maintain ( uses mason, speedy, paypal )
- DBD::Pg - perl DBI driver for PostgreSQL on bob
- PostgreSQL |
DBI.pm |
DBD::Pg docs
- on bob: username=msie02 , dbname=msie02, passwd=olymp.02
- from command line:
shell> psql msie02 msie02 [ --file filename ]
example of file with SQL : sample.sql
- Notice particularly
how transactions and eval fit together, by
turning on "die by default if error" and
trapping thrown errors.
(Note: CSV does *not* support transactions. postgres does.)
- Example 1: add a few rows to the table -
exerciseSample.pl
- Example 2: display a table -
showSample.pl
(No changes to table, so commit/rollback is not needed.)
- S&P 500 database example, CVS / PostGres comparison,
drawing stock history
- One more example - of playing with a reasonably big data set
- Standard and Poor 500 stocks - daily records from last year
-
run it -
source
- technology : graphics on the fly, flat file (!) 5.5Meg data,
images saved after created, GD.pm, mason
- Would it be faster with DBD::Pg ?
- I've also started looking at the google programing contest
data set, and wondering about sql server perfomance with
something that size. We'll see what there's time to do.
- Resources
- Next time
- security
- email notification and email gateways
- Assignment:
- Read chapter 13 in CGI Programming, on dynamic graphics
- Browse the documentation for the modules discussed this week,
particularly DBD::Pg
- Coding: pick any one of today's technogoies to try out.
- Create a dynamic image
using GD.pm or any of the tricks in "CGI Programming", or
- Install and test CGI::Speedy, or
- Play around with DBD:Pg and DBI, or
- ...
Ta ta.