Transform password into digest inside a DBIx::Class model

Posted by on May 28, 2008
I'm working on a project using Catalyst , and I initially coded the password hashing (compare the passwords then build the digest, used during the signup and change password processes) directly into my controller (could be placed in a Utils package) and passed the digest to the create/update like so:
my $pass = $c->req->param("password");
my $digest = Digest->new('MD5')->add($pass)->digest;
Reading the Catalyst mailing list I learned about store_column(), part of DBIx::Class::Row . Upon storage of a row (an update or create), it allows you to override the default "store this value" and manipulate the value into whatever you'd like. Using the code below I was able to move this logic out of the controller/Utils package and into the model where it belongs.
sub store_column {
    my( $self, $col, $val ) = @_;
    $val = Digest->new('MD5')->add($val)->digest
              if $col eq 'password';
    return $self->next::method( $col, $val );
This technique can also be used to perform calculations .