Любопытное. Вы можете вставить for
используя do
:
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'),
),
),
);