Helping ordinary people create extraordinary websites!
HOME TUTORIALS SCRIPTS WEB HOSTING BLOG FORUM
Get Our Newsletter
Your Email:

Cultured Perl: Embedding Perl in database tables

By Teodor Zlatanov
2005-04-27


Embedded Modifications

Modification is more fun. The sharp-eyed ones have already noticed the code in Listing 1 that shows the template for modification: $target->VERB(DATUM). Other than the singular of "data," what does this template show?

Recall that earlier, I defined the necessary information for an operation as the noun, verb, and optional modifier. If the operation is "MODIFY," then the modifier is only optional for retrieving the value.

Let's do an example. The object (target noun or subject) is $target, which is of class Employee. That class has, from the "employees" table somewhere in the database, the field "name" so the raw code to set the name would be as follows.

Listing 4. Just set the employee name
my $target = Employee->retrieve(500);

$target->name("Jonesy");
Of course, if I did something so simple, I'd lose my professional pride and never work as a programmer again. I must obfuscate this example.

Listing 5. Once more, with feeling

my $target = Employee->retrieve(500);

my $data = "Jonesy";
my $verb = "name";
my $codetemplate = CodeTemplate->retrieve('MODIFY');
my $template = $codetemplate->template();

if ($template)
{
$template =~ s/VERB/$verb/g;

# this is how you can get the OLD value of the field
$retriever = $template;
$retriever =~ s/DATUM//g;

$old_value = eval $retriever;

$template =~ s/DATUM/\$data/g;
}

my $result = eval $template;
if ($@)
{
print "The evaluation failed\n";
}
elsif (defined $result)
{
print "This operation resulted in [$result]\n";
}
else
{
print "This operation's result was undefined\n";
}
Kidding aside, this example is complicated for a reason. Now you can set any field at all while keeping its previous value by setting a modifier and a verb.

Do you notice something funny? The code here looks a lot like Listing 3, which handles deletions. You can easily combine Listings 3 and 5 to make a generic code template handler. The deletion template would not suffer from any of the substitutions, but you do have to be careful about retrieving the old value of the field because for a deletion, that would just delete the object. So make the old value retrieval only happen for a "MODIFY" template.

Other templates you could add are, of course, ADD and SEARCH. They work the same way: give the template handler a template, a verb, and a modifier, and it will happily run the resulting code.

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