2011-05-25 144 views
1

我試圖根據使用CGI的迭代打印表單中的字段。是否可以在CGI打印語句中嵌套一個for循環?我不斷收到一個語法錯誤的for循環線...在cgi中使用for循環打印

print $survey->Tr(
      $survey-td(
       $survey->startform(name=>'survey', method => 'POST', action => 'survey.cgi'), 
       for ($i=0; $i < $size; $i++){ 
        $survey->hidden(name=>"q$i", value => "currentQ[q$i]"), 
        $survey->submit(name=>'direction', value =>'Previous'), 
        $survey->endform(), 
       } 
       $survey->startform(name=>'survey', method=>'POST', action=>'survey.pl.cgi'), 
       $survey->submit(name=>'direction', value =>'Next'), 
      ), 
     ), 
+1

's/$ survey-td/$ survey-> td /'? – mob 2011-05-25 15:11:53

+0

«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

+0

請停止使用骯髒的舊CGI.pm.改用現代和乾淨的網絡引擎,例如[舞者](http://www.perldancer.org/)或[Mojolicious](http://mojolicious.org/)。 – dolmen 2012-08-31 10:22:04

回答

2

號相反,你應該推動所有項目上的數組(提前製作的父元素),然後通過數組作爲論據。

0

有點。您可以使用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在中間,你可以使用startXXXendXXX

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整齊地結合dofor

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'), 
    ), 
    ), 
);