Stupid Perl Tricks

I’m certainly delving into some depths of Perl I’ve never touched before.

I need to lookup a record in one table using a subclass of DBIx::SearchBuilder::Record, and depending on what it says in the “table_name” column, look up a record in one of several other tables, each one of which has its own subclass of DBIx::SearchBuilder::Record. And after some delving on the internet, I was amazed to find out you could put the class name in a variable when you “new” it. Unfortunately you still need to “eval” the require statement.

my $mainRec =

# Ok, here comes the tricky part. Try to get figure out the class name
# from the table name
my $table_name = $mainRec->table_name;
$table_name =~ s/^dsu/DSU/i;
$table_name = ucfirst($table_name);
$table_name =~ s/_([a-z])/uc($1)/eg;
$table_name =~ s/s$//;

my $subRec;
no strict 'refs';
my $fullClass = "RTx::FooProject::Record::$table_name";
eval "require $fullClass";
$subRec = $fullClass->new($cfHandle);
$subRec->LoadByCol("main_id", $mainRec->id);

I literally threw my hands in the air in exultation when it worked. And then was rather painfully reminded of my recent shoulder surgery.