Helping ordinary people create extraordinary websites!
GET OUR NEWSLETTER
Your Email:
 

Cultured Perl: Embedding Perl in database tables

By Teodor Zlatanov
2005-04-27


Setting up Tables

Before delving into details, we should review the architecture and tables, such as the template table for Perl code. For example, object deletion will use the "DELETE" template. The following listing demonstrates the way you can define that table with MySQL.

Listing 1. The code templates table definition (MySQL)

DROP TABLE IF EXISTS codetemplates;

CREATE TABLE codetemplates (
name varchar(200) NOT NULL,
template longtext NOT NULL,
PRIMARY KEY (name)
) TYPE=MyISAM;
LOCK TABLES codetemplates WRITE;
INSERT INTO codetemplates VALUES ('DELETE', '$target->delete()');
INSERT INTO codetemplates VALUES ('MODIFY', '$target->VERB(DATUM)');
UNLOCK TABLES;
This is just a basic two-column table with the template name as the primary key, so no two templates can have the same name.

Programming is a lot like speaking because you need verbs, nouns, adjectives, and so on. In fact, Larry Wall, Perl's creator, is a linguist whose experience in that field has obviously influenced Perl's evolution. In terms of embedded Perl code, the things you always need are a "noun" (the thing affected) and a "verb" (the action taken). The "modifier" (adverb or adjective in a sentence) is not required, usually.

The "noun" is the target of the code. I'll call it $target and it will be required. The code that prepares for the action will have $target ready.

The "verb" is the action taken by the code. I'll run that action by evaluating the code; if the code should fail, then I will catch that error. Perl provides the eval() function for that purpose. The string VERB will be a placeholder for the modifier action.

The "modifier" is the tricky one. When it's not needed, as in the case of simple object deletion, you don't have to worry. When it is needed, we'll provide a placeholder string. The string will be DATUM, and you will see how to use it in the section on embedded modifications.

Tutorial Pages:
» Put Perl into your RDBMS Design to Reach Database Nirvana
» Class::DBI Capabilities
» Setting up Tables
» Embedded Deletion
» Embedded Modifications
» Compatibility with Other Languages and Alternative Approaches
» Conclusion
» Resources


First published by IBM DeveloperWorks


 | Bookmark
Related Tutorials:
» Random subroutines in Perl
» Log Script Use
» Creating Perl Modules for Web Sites
» Bit Vector, Using Perl Vec
» Build a Perl/CGI Voting System
» Perl Range Operator

Advertise with Us!


Tutorials Scripts Web Hosting Developer Manuals
Resources