[CDBI] Updating data automatically after a search (trigger)

Perrin Harkins perrin at elem.com
Wed Feb 1 15:08:09 GMT 2006


Kjell Arne wrote:
> Example with a session object:
> my $session = My::Session->search(pin => $pin) if $pin;

That gives you an iterator, not a session object.  And if pin is the 
primary key, it should use retrieve instead.

It also triggers a bug in perl caused by "my $foo = 1 if $bar" 
constructs.  Never assign to a lexical with a trailing if statement.

> I basically want something like this to happen in session.pm:
> sub search {
>     my ($self, $DATA) = shift;
> 
>     $self->SUPER::search($DATA);
> 
>     // This will fail as $self is not blessed
>     $self->last_active(time);
>     $self->update();
>     // **
> }

Make a method for it.

package My::Session;

sub find_by_pin {
     my ($class, $pin) = @_;
     return unless $pin;
     my ($session) = $class->search(pin => $pin);
     $session->last_active(time);
     $session->update();
     return $session;
}

- Perrin




More information about the ClassDBI mailing list