2014-04-14 109 views
1

我不确定标题是否正确。我正在试图做的,这是(我已经得到了从DBI数据库句柄):Perl字符串作为函数参考

my $idcenter = 4; 
my $getCenter = $dbh->prepare(
<<SQLEND 
select * from center 
where uidcenter = ? 
SQLEND 
); 

my @tables = ("Center"); 
foreach $table (@tables) { 
    my $func = "get$table"; 
    $func->bind_param(1, $idcenter); 
    etc. 
} 

那么,如何你建造一个变量点(是这样吗?)的功能?有没有办法?

回答

3

您使用散列

my %get_table = (
    center => $getCenter, 
); 

$get_table{"center"}->bind_param(1, $idcenter); 
+0

哦,谢谢!之前我使用过一个哈希表,但我没有把美元符号放在getCenter前面。 – horndinkle

+3

确保你有['use strict;'](http://perldoc.perl.org/strict.html)和['use warnings;'](http://perldoc.perl.org/warnings.html)在每个perl脚本的顶部。如果你这样做了,并且你忘记了这个美元符号,perl会给你以下有用的错误:''严格的潜艇'在使用时不允许使用'Bareword'getCenter' – Miller