Perl CGI 打印格式

标签 perl cgi

我遇到了一个相当奇怪的问题。我只是尝试通过循环存储输入名称的数组以及与该名称关联的注释来显示用户输入的注释。

#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/

相关文章:

javascript - Perl CGI 和 JavaScript : Dynamic Dependant Dropdown

javascript - 表数据上的 G-mail 样式表单提交

Python CGI - 根据用户时区更改数据

perl - 如何从 Perl 中的数组中获取加权随机选择?

perl - 我如何在没有 root 和互联网访问权限的情况下使用 cpanminus

perl - 这两个关于注释和原型(prototype)的警告在 Perl 中意味着什么?

php - yii2: Unable to determine the entry script file path - 将PHP服务器环境从cgi改为cli

mysql - 我如何在我的 CSV 中将 ,, 正则表达式表示为 ,\N, 以便 mysqlimport 理解它们?

perl - 如何打印每行带有分隔符的 N 个数组元素?

c++ - 忽略 C++ 中自签名证书的 "Certificate Verification"