我遇到了一个相当奇怪的问题。我只是尝试通过循环存储输入名称的数组以及与该名称关联的注释来显示用户输入的注释。
#Check through each name
for ( my $i = 0; $i < scalar @$namesRef; $i++ )
{
#Display the comment to the user
print @$namesRef[$i].": ".@$commentsRef[$i], p;
}
在显示评论的页面上,它不是像“John: comment”那样显示,而是像“Johncomment:”那样显示。此外,不包含“,p”,因此下一条注释不会换行。
我会放一张图片来更好地显示问题所在,但我还没有足够的代表:/
编辑:@符号在那里,因为它们是对此子例程之外的数组的引用。
编辑:初始数组在此子例程中声明。
子构建表单 { 我的 $form = 转变; #检查按下了哪个按钮。
my $daysOld = 0; #Holds how many days old the user is.
my $commentErrors = 0;
my @names = ();
my @comments =(); #Array to store the user's comments.
my @errorMessages =(); #Array to store the error messages for the current form.
下面是调用评论表单子(monad)例程的地方:
elsif ( $form == 3 )
{
&readComments(\@comments, \@names, \@errorMessages); #Read in the comments.
#Initial build - Can't have any errors.
&build3(\@comments,\@names, \@errorMessages, $commentErrors, param('name'), param('comment'));
}
elsif ( $form == 4 )
{
$commentErrors = &commentFormErrorCheck( param('name'), param('comment'), \@errorMessages ); #Check for an errors.
&build3(\@comments,\@names, \@errorMessages, $commentErrors, param('name'), param('comment'));
}
最佳答案
主要问题是在打印语句中使用@符号。
假设@names
和@comments
是并行数组,显示一个完整的简化示例来演示用法:
build3(\@comments, \@names);
sub build3 {
my $comments = shift;
my $names = shift;
for (my $i = 0; $i < @$names; $i++) {
print $names->[$i].": ".$comments->[$i], p;
}
}
也就是说,您可能需要查看 printf
以使该行更具可读性。
另外,不要忘记 HTML 转义。
编辑:
添加使用 HTML 转义和 printf()
的示例。
use CGI qw/escapeHTML p/;
printf("%s: %s%s\n", escapeHTML($names->[$i]), escapeHTML($comments->[$i]), p);
关于Perl CGI 打印格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20532585/