有點。您可以使用do
嵌入for
:
print $survey->tr(
$survey->td(
do {
my @forms;
for my $i (0..$size-1) {
push @forms, $survey->form(
{ name=>'survey', method => 'POST', action => 'survey.cgi' },
$survey->hidden(name=>"q$_", value => "currentQ[q$_]"),
$survey->submit(name=>'direction', value =>'Previous'),
);
}
@forms
},
$survey->form(
{ name=>'survey', method=>'POST', action=>'survey.pl.cgi' },
$survey->submit(name=>'direction', value =>'Next'),
),
),
);
它可能更容易預先計算的內部零件,雖然。
my @forms;
for my $i (0..$size-1) {
push @forms, $survey->form(
{ name=>'survey', method => 'POST', action => 'survey.cgi' },
$survey->hidden(name=>"q$i", value => "currentQ[q$i]"),
$survey->submit(name=>'direction', value =>'Previous'),
);
}
push @forms, $survey->form(
{ name=>'survey', method=>'POST', action=>'survey.pl.cgi' },
$survey->submit(name=>'direction', value =>'Next'),
);
print $survey->tr($survey->td(@forms));
如果你確實想擁有for
在中間,你可以使用startXXX
和endXXX
。
print $survey->starttr();
print $survey->starttd();
for my $i (0..$size-1) {
print $survey->form(
{ name=>'survey', method => 'POST', action => 'survey.cgi' },
$survey->hidden(name=>"q$i", value => "currentQ[q$i]"),
$survey->submit(name=>'direction', value =>'Previous'),
);
}
print $survey->form(
{ name=>'survey', method=>'POST', action=>'survey.pl.cgi' },
$survey->submit(name=>'direction', value =>'Next'),
);
print $survey->endtd();
print $survey->endtr();
最後,map
整齊地結合do
for
。
print $survey->tr(
$survey->td(
(map {
$survey->form(
{ name=>'survey', method => 'POST', action => 'survey.cgi' },
$survey->hidden(name=>"q$_", value => "currentQ[q$_]"),
$survey->submit(name=>'direction', value =>'Previous'),
);
} 0..$size-1),
$survey->form(
{ name=>'survey', method=>'POST', action=>'survey.pl.cgi' },
$survey->submit(name=>'direction', value =>'Next'),
),
),
);
's/$ survey-td/$ survey-> td /'? – mob 2011-05-25 15:11:53
«Tr»»⇒«tr'»。 «'-td'»⇒«' - > td'»。 «'$ i = 0;'»⇒«'我的$ i = 0;'»。 ('$ i = 0; $ i <$ size; $ i ++);''⇒«'爲我的$ i(0 .. $ size-1);'»。第一個'startform'需要在循環中。最後一個'startform'需要一個匹配的'endform'。 – ikegami 2011-05-25 19:10:50
請停止使用骯髒的舊CGI.pm.改用現代和乾淨的網絡引擎,例如[舞者](http://www.perldancer.org/)或[Mojolicious](http://mojolicious.org/)。 – dolmen 2012-08-31 10:22:04