in reply to How to cache a regular expression in a DBIx::Class object

Try something like

sub match_re { my $self = shift; # retrieve from cache if it's there: my $re = $self->{match_re}; return $re if defined $re; # otherwise compute it and store it: $re = $self->match_string; $re = qr/\Q$re\E/; return $self->{match_re} = $re; }

in the result class.

Replies are listed 'Best First'.
Re^2: How to cache a regular expression in a DBIx::Class object
by chrestomanci (Priest) on Apr 08, 2011 at 21:04 UTC

    Thank you for replying. Unfortunately I have tried that and it did not work.

    When processing log output, I set a breakpoint in the perl debugger on the second line of the input file. (using $DB::single=1 if 0  != $INPUT_LINE_NUMBER)

    When I hit that breakpoint I stepped into the $watch_string->match_re() method and found that the regular expression that should have been cached was not there. It looks like DBIx::Class was re-creating objects every time.

    There are about 10 rows in the tblWatchString table, so I would not have thought that objects would be cleared as part of a memory saving or garbage collection system.

      It looks like DBIx::Class was re-creating objects every time.

      Oh, I missed that part. There's a much easier for that - move the retrieval of the DBIx::Class objects out of the loop:

      my @watchstrings = $schema->resultset('WatchString')->all while( my $line <INPUT> ) { for my $w (@watchstrings) { ... } }