1
我有20個表格,我試圖向我的「模型」添加一個搜索功能,該模型返回用戶選擇的表格的行的值「看法」部分。Perl:製作表格名稱作爲輸入的一般功能
sub search {
my ($table, $page_no, $search_value, $where, $order) =
@args{qw/table page_no search_value where order/};
my $option = {};
if (defined $order || defined $self->order) {
$option->{order_by} = $order // $self->order;
}
my @rows = $self->ds->select($where, $option);
return (\@rows);
}
不幸的是,我的 「DS」 的方法(這依賴於SQL ::摘要做查詢)太一般了:
sub ds { DataSource->new(); }
我有使用DataSource.pm(我一般DBI特定模塊模塊)作爲父項並指定ds。如果,例如,我想修改表A,我需要調用:
sub ds { DataSource->TableA->new(); }
我的問題是,在這種情況下,我不知道先驗什麼表的用戶將需要修改,因此我不知道$table
需要什麼值。我想寫一些需要$table
作爲輸入,並給出相應的ds。如果$table
是TableA
,ds將需要變爲,如果$table
是TableB
,那麼ds將需要變爲sub ds { DataSource->TableB->new(); }
等等。我不知道,我怎麼寫這個而不依賴於很多條件?
不幸的是我無法修改ds。我很樂意依靠騰這個......
將方法名稱存儲在一個變量幫助你嗎? 'DataSource - > $ tablename-> new;'是有效的Perl。 – choroba