Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

Phred (5358)

Phred
  fredNO@SPAMtaperfriendlymusic.org
http://www.redhotpenguin.com/

Fred is a Perl and PostgreSQL geek. He has made some very small contributions to a few cpan modules and mod_perl.

Journal of Phred (5358)

Friday August 05, 2005
04:43 PM

Procedural Language Perl (PLPerl)

[ #26129 ]

In my latest adventures in code I've been using PLPerlNG, which is a Perl procedural language for the PostgreSQL database. In the past when I've needed to munge data before creating a new object with Class::DBI, I override the create method or create a before_create trigger.

I wanted to try something different this time, so after some frustration with trying to get this particular trigger working in the plpgsql language, I decided to write it in PLPerl. For this implementation, I have an image table which I map locations of images to, and if the application does not provide a name for the image I use the filename sans extension for the name. Here's the code:

CREATE OR REPLACE FUNCTION image_insert() RETURNS trigger as $$
    if ( ! defined $_TD->{new}{name} || $_TD->{new}{name} eq '') {

        # Assign the name of the file to name of the image if no name specified
        ($_TD->{new}{name}) = $_TD->{new}{uri} =~ m{([^/]*)\.\w+$};
        return "MODIFY"; # Modify tuple and proceed INSERT/UPDATE command
    } else {

        return; # Proceed INSERT/UPDATE command
    }
$$
language plperl;

CREATE TRIGGER image_insert
    BEFORE INSERT ON image
    FOR EACH ROW
    EXECUTE PROCEDURE image_insert();

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.