【发布时间】:2011-08-15 14:59:54
【问题描述】:
在我的 perl“脚本”中,我正在收集数据并构建哈希图。 hashmap键代表字段名称,值代表我要插入相应字段的值。
hashmap 被构建,然后传递给 saveRecord() 方法,该方法应该构建 SQL 查询并最终执行它。
这里的想法是更新数据库一次,而不是每个字段一次(有很多字段)。
问题:我无法将 hashmap 传递给 sub,然后将字段和值从 hashmap 中拉出。此时我的键和值是空白的。我怀疑数据在传递给潜艇的过程中丢失了。
脚本的输出表明没有键和值。
需要帮助以某种方式将数据传递给 sub,以便我将其拉回,如图所示 - 使用 join()。
谢谢!
代码sn-p:
for my $key (keys %oids) {
$thisField = $key;
$thisOID = $oids{$thisField};
# print "loop: thisoid=$thisOID field=$thisField\n";
# perform the SNMP query.
$result = getOID ($thisOID);
# extract the information from the result.
$thisResult = $result->{$thisOID};
# remove quotation marks from the data value, replace them with question marks.
$thisResult =~ s/\"|\'|/\?/g;
# TODO: instead of printing this information, pass it to a subroutine which writes it to the database (use an update statement).
# printf "The $thisField for host '%s' is '%s'.\n", $session->hostname(), $result->{$thisOID};
# add this key/value pair to the mydata hashmap.
$mydata{$thisField} = $thisResult;
# print "$thisField=$thisResult\n";
}
# write one record update for hashmap %mydata.
saveRecord (%mydata);
# write all fields to database at once...
sub saveRecord ($) {
my $refToFields=shift;
my @fieldlist = keys %$refToFields;
my @valuelist = values %$refToFields;
my $sql = sprintf ("INSERT INTO mytable (%s) VALUES (%s)",join(",",@fieldlist), join(",",@valuelist) );
# Get ID of record with this MAC, if available, so we can perform SQL update
my $recid = getidbymac ($MAC);
print "sql=$sql\n";
# TODO: use an insert or an update based on whether recid was available...
# TODO: ID available, update the record
# TODO: ID not available, insert record let ID be auto assigned.
}
【问题讨论】:
-
一般情况下,不要使用子程序原型。它们不像其他语言那样工作。大多数情况下,它们的存在是为了让模块作者可以复制内置函数的接口。有关更多信息,请参阅 perldoc perlsub。 perldoc.perl.org/perlsub.hmtl
标签: sql perl hash parameter-passing dbi