ropey has asked for the wisdom of the Perl Monks concerning the following question:

Hi Guys, There is probably a simple solution... but got a little stuck... So I am using Class::DBI I pass in the information I want to save in the database which in this case is also information which I want encrypted. I have a encryption function and I simply want to encrypt the data BEFORE it gets inserted into the DB. I thought I could simply add a 'before_set_$column' trigger and alter the value of one field and let it be updated... but once I update the data using the accessors... it calls the function again recursively... which isnt really helpful... what is the correct way to do it ? I have so far (and doesnt work)
package Austin::DB::CreditCard; use strict; use warnings; use Austin::DB; use base qw/Austin::DB/; use Austin::SUP::Helpers qw/encrypt/; use Data::Dumper; Austin::DB::CreditCard->table('cc_payment'); Austin::DB::CreditCard->columns(All => qw/trip_id cc_type cc_name cc_n +umber expiry issue_no sec_code/); Austin::DB::CreditCard->autoupdate(0); __PACKAGE__->add_trigger( before_set_cc_number => \&do_encryption); sub do_encryption { my $self = shift; my $encrypted = encrypt('local', $self->cc_number, getlogin()); $self->cc_number($encrypted); } 1;
And then simply insert...
Austin::DB::CreditCard->insert(\%info);

Please no comments on storing cc info in a DB... at this point we do not have a choice and thats beyond my control

Replies are listed 'Best First'.
Re: Class::DBI and Triggers
by rhesa (Vicar) on Nov 14, 2006 at 12:57 UTC
    Inside your trigger, do not call the accessor directly. Instead, call $self->set('cc_number' => $encrypted). That should avoid triggering the trigger again.