【发布时间】:2019-02-20 15:41:35
【问题描述】:
我不知道为什么我的脚本在我的脚本中声明数组时可以工作,但是当我尝试从数据库中获取数组数据时却不工作。
Perl
use Template;
my $template = Template->new;
if ( $info ) {
my $select = $DBH->prepare("SELECT FOO, BAR, MOO FROM tble WHERE CONCAT(FOO, ', ', BAR, ', ', MOO) LIKE ?");
$select->execute('%' . $info . '%');
$names = $select->fetchall_arrayref();
foreach $names ( @$names ) {
( $variable1, $variable2, $variable3 ) = @$names;
}
}
my $templ = <<START_HTML;
<!DOCTYPE html">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1
+" />
<title>Untitled Document</title>
</head>
<body>
[% FOREACH name IN list %]
<li>
<div class='inforno'>
<img src='inforno'>
</div>
<div class='inforno'>
<a href='#' class='inforno'>[% name %]</a>
</div>
<span class='inforno'>
<a href='#' class='inforno'>Edit user</a>
</span>
</li>
[% END %]
</body>
</html>
START_HTML
$template->process(\$templ, { list => \@$names })
or die $template->error;
输出
ARRAY(0x2030674)
ARRAY(0x2030634)
ARRAY(0x2030618)
当我运行时,我得到了那个错误。我想从数据库中获取数组数据。
【问题讨论】:
-
为什么不使用这些变量就分配
( $variable1, $variable2, $variable3 ) = @$names?